[Catalyst] Xml data to html

Xavier Caron xavier.caron at atmel.com
Tue Sep 16 07:22:25 BST 2008


> I expect you could understand me...

As already stated on the list, this is not Catalyst's work. You'll want to use
XML::LibXML, XML::LibXSLT or XML::Twig for this matter instead. Here is a little
sample of what you can do with XML::LibXSLT (by chance, I'm currently deep into
this at the office ;^):

  - libraries.xml (fixed for it wasn't valid)

<?xml version='1.0' encoding='ISO-8859-1'?>

    <library name="Wolf">
          <book name="The King"/>
          <book name="The Queen"/>
    <library name="Fox">
          <book name="The Castle"/>
          <book name="The Dragon"/>

  - xml_to_html.xsl

<?xml version='1.0' encoding='ISO-8859-1'?>

<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:output method='html' encoding='ISO-8859-1' indent='yes'/>

<xsl:template match='libraries'>
  <title>Pedro's Libraries</title>

  <meta name="generator" content="xml_to_html.xsl"/>
  <link rel="stylesheet" href="css/html.css" type="text/css"/>

  <h1>Pedro's Libraries</h1>

<xsl:template match='library'>
  <h2><xsl:value-of select='@name'/> Library</h2>

<xsl:template match='book'>
  <tr><td><xsl:value-of select='@name'/></td></tr>


  - xml_to_html


use strict;
use warnings;

use XML::LibXSLT;
use XML::LibXML;

my $xslted_dom = transform ( document_file => 'libraries.xml', stylesheet_file => 'xml_to_html.xsl' );

print $xslted_dom->toString ( 1 ); # or toFile ( 'libraries.html', 1 ) see perldoc XML::LibXML::Document

sub transform {
  my ( %h ) = @_;
  my $xml_parser  = XML::LibXML->new;
  my $xslt_parser = XML::LibXSLT->new;
  $xml_parser->validation   ( 0 ); # perldoc XML::LibXML::Parser
  $xml_parser->load_ext_dtd ( 0 );
  my $xml_dom    = $xml_parser->parse_file ( $h{document_file}   );
  my $xsl_dom    = $xml_parser->parse_file ( $h{stylesheet_file} );

  my $stylesheet = $xslt_parser->parse_stylesheet ( $xsl_dom );
  $stylesheet->transform ( $xml_dom, @{$h{parameter_list}} );

FYI, xml_to_html is Perl equivalent for xsltproc (libxslt) point-tool:

  % xsltproc xml_to_html.xsl libraries.xml

This should help you for this XSLT rather steep learning curve.



More information about the Catalyst mailing list