[Catalyst] Xml data to html

Robin Berjon robin at berjon.com
Tue Sep 16 17:00:53 BST 2008

On Sep 15, 2008, at 21:53 , Pedro Guevara wrote:
> Well I thought to make a script but It's going to take me a lot of  
> time.
> Because is a complex tree of data, so I supossed that some module of  
> Catalyst would do it for me.
> It's like
> <libraries>
>     <library name="Wolf">
>           <book name="The King">
>           <book name="The Queen">
>     </library>
>     <library name="Fox">
>           <book name="The Castle">
>           <book name="The Dragon">
>     </library>
> <libraries>
> And it retrieves: like html
> Libraries
> <table>
>    <tr>
>         <td>Library :</td><td> Wolf</td>
>         <td>Books:</td><td>      The King,    The Queen</td>
>    </tr>
>     <tr>
>         <td>Library :</td><td> Fox</td>
>         <td> Books:</td><td>      The Castle,    The Dragon</td>
>     </tr>
> </table>

Like everyone else I can only say that this has nothing to do with  
Catalyst. But what the heck. It's a trivial conversion, so simple that  
you don't even need XSLT (though you can still use it, I probably  
would). Old school approach:

use XML::LibXML;
my $doc = XML::LibXML->new->parse_file('libraries.xml');
print "<table>\n";
for my $lib ($doc->getElementsByTagNameNS(undef, 'library')) {
     print "  <tr>\n    <td>Library:</td><td>" .
           $lib->getAttributeNS(undef, 'name') .
           "</td>\n    <td>Books:</td><td>" .
           join(', ', map { $_->getAttributeNS(undef, 'name') } $lib- 
 >getElementsByTagNameNS(undef, 'book')) .
           "</td>\n  </tr>\n";
print "</table>\n";

I haven't tested the above, but if it doesn't work something a lot  
like it will.

Robin Berjon - http://berjon.com/
     Feel like hiring me? Go to http://robineko.com/

More information about the Catalyst mailing list