in reply to Picking an XML Module

This sounds like a good candidate for XML::LibXML and XML::LibXSLT.

UPDATE:
First off, i really think you should change the structure of your XML to something like: def.xml

<book> <chapter id="1"> <part id="1"> <info>infoinfostuffhere</info> <def word="someword">some definition of the word</def> <extra>more info and notes</extra> </part> <part id="2"> <info>infoinfostuffhere</info> <def word="someword">some definition of the word</def> <extra>more info and notes</extra> </part> </chapter> </book>
This will make parsing much easier ... having a bunch of <section id="foo"> tags is just too general. Also, be sure and wrap everything you can. Now that we have some, IMHO, better XML to work with, we can define a stylesheet: def.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Tr +ansform"> <xsl:template match = "/book" > <xsl:for-each select = "chapter[@id]" > <h1> Chapter <xsl:value-of select="@id"/> </h1> <xsl:for-each select = "part[@id]" > <h3> Part <xsl:value-of select="@id"/> </h3> <i><xsl:value-of select="info"/></i><br/> <xsl:for-each select = "def[@word]" > <b><u><xsl:value-of select="@word"/></u></b>:<br/> </xsl:for-each> <xsl:value-of select="def"/><br/> <xsl:value-of select="extra"/><br/> </xsl:for-each> </xsl:for-each> </xsl:template> </xsl:stylesheet>
And finally, the script to transform all of this into 'HTML'
use strict; use warnings; use XML::LibXML; use XML::LibXSLT; my $xml = XML::LibXML->new(); my $xslt = XML::LibXSLT->new(); my $source = $xml->parse_file('def.xml'); my $style_doc = $xml->parse_file('def.xsl'); my $stylesheet = $xslt->parse_stylesheet($style_doc); my $results = $stylesheet->transform($source); print $stylesheet->output_string($results);

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re: (jeffa) Re: Picking an XML Module
by mirod (Canon) on Aug 03, 2003 at 16:42 UTC
    First off, i really think you should change the structure of your XML to something like: def.xml

    Why? Why would you pervert a perfectly logical, not to mention practical, document structure, in order for your code to be easier to write? The original format makes sense, why add extra tags everywhere to avoid having to deal with mixed content? Mixed content exists, it's there for a good reason: that's how you write documents.

    What happens if you have more than one definition in the section? Would you have this:

    <part id="1"> <info>infoinfostuffhere</info> <def word="someword">some definition of the word</def> <extra>more info and notes</extra> <extradef word="someword">some definition of an other word</extr +adef> <doubleextra>even more info and notes</doubleextra> </part>

    I don't think it would make sense either!

      Because i am still a newbie at XML. :P

      Seriously, because i didn't know any better ... i see now why the mixed content is OK to have. ninja-joe ... my apologies. If you do take my advice, feel free to ask more questions ... i personally find XSLT and XPath to be somewhat hard to work with until you get the hang of them. While i was developing hacking out the above code, i found it 'easier' (falsely, of course) to wrap everything instead of dealing with mixed content. mirod++ yet again. ;)

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)