BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:

I hesitated for a long time before asking this question as it may be considered to far "off topic" or be voted down for any number of other reasons, but after much research and trial by error, I've decided to risk it here where I have a high degree of respect for the accumulated wealth of experience rather than in 'some other forum'!

So here goes: (disables the flushing sound in XPFixation)

The scenario is this. I have a collection of XML files describing various "things". These are stored in a heirarchy of subdirs, loosely grouped by type or subject. The application is to allow users to navigate around the structure and to select the thing of interest and have the details presented on screen using a browser.

With the help of many good monks (and a few bad ones:), I now have the navigation part written in Perl/CGI and am quite pleased with it despite how long it took me to get it to run. I wanted to evaluate Perl for myself anyway.

My problem now is the best way to present the contents of the XML on screen. There seem to be various ways of doing this:

  1. Using XML::Simple and CGI or HTML::Templates or even Jeurd's PLP package which I found very easy and intuative to work with. The problem with this is that the contents of the XML files are not wholey consistant. Some have pictures, some do not. Some have details in attributes that need to be displayed, others have the same details as the contents of nested tags. I do not control these files unfortunately. This approach means re-coding for each new format that comes along.

  2. Basically, I just need to display this stuff on screen. I had thought that I could simply attach a suitable .css file to each xml file and lo, it would take care of itself but that proves to be problematic in several areas.

    • css1 does not provide a mechanism for marking an abitrary tag as a link. This is a "must have" facility.
    • Many browsers do not respond well, if at all to css1 positioning attributes.
  3. I could use the more advanced features of css2.

    • This provides the "arbitrary tag as a link" facility.
    • I don't quite see how to handle the "details as attributes" problem yet.
    • I have discovered that even browsers that claim css2 compatibility (Opera, IE6 etc) still baulk or fall down in lots of areas. I have a list of open support issues raised with Opera on this.
  4. The latest possibility is the use of XSLT & XPATH. I brought the book. And it certainly could be used to acheive my goal. The downsides of this are:

    • It requires another tool to perform the transform to XHTML. There are several of these available both commercial and freeware. I haven't discovered (or yet looked) for one written in Perl. I am sure that there will be one soon if not already.
    • It would require learning Yet Another Syntax in order to write the XSL stylesheets for each variation of the input XML. This may or may not be easier than doing the transform in with Perl/XML::Simple?

What I'm hoping from this post is that one or two peope here have already been down this road and will share their experience, choices and reasoning with me so as I can short-cut a little of the redundant learning that will result from me investigating each of them myself.

Thanks for your time in reading this.

Replies are listed 'Best First'.
Re: Request for advice. Presenting XML.
by rjray (Chaplain) on Jul 06, 2002 at 06:52 UTC

    I recommend considering the XSLT approach, and tuen to using XML::LibXML and XML::LibXSLT, possibly with XML::SAX as the parsing API layer.

    None of these will address the issue of having to work around inconsistent content and syntax. That's an albatross you're just stuck with. But the underlying parser libraries (libxml2 and libxslt) are quite fast and designed to work in harmony together.

    --rjray

Re: Request for advice. Presenting XML.
by mirod (Canon) on Jul 06, 2002 at 14:11 UTC

    You could also use a 2-step process: use a Perl/XML module, probably SAX based, to turn the initial XML into something that can be dislayed properly using CSS1. The initial step could be quite simple, you just have to convert you "arbitrary tags" to a, and maybe a couple more, if you pass the name of the tags as parameters to the transformation process you could even write something generic. Depending on how close the output of the transformation is to the structure of XHTML the CSS could turn out to be pretty simple too.

    Note that if your XML includes mixed content you cannot use XML::Simple to process it.

      Note that if your XML includes mixed content you cannot use XML::Simple to process it.

      What do you mean by "mixed content"?

      So far, all my files work fine with XML::Simple, but I'd like to know what to be looking out for.

        Mixed content is elements with both text and markup children:

        <p>This is <b>mixed</b> content/<p>

        XML::Simple does not keep the order of the children, in this case the 2 contents ('This is ' and ' content') and the b.

        You can try with this:

        perl -MXML::Simple -e'$xml=XMLin( "<p>This is <b>mixed</b> content</p>"); print XMLout( $xml);'

        The output is:

        <opt b="mixed"> <content>This is </content> <content> content</content> </opt>

        Even if you use the noattr option you will still get the 2 contents and the b element in the wrong order.

Re: Request for advice. Presenting XML.
by Matts (Deacon) on Jul 08, 2002 at 13:20 UTC
    A few comments:
    1. HTML::Template is another language - exactly the same "issue" as you have with XSLT. The only difference is that XSLT is more powerful, and perhaps more cleanly designed, having learned from 20 years of development of DSSSL, which XSLT is based on.
    2. There are some pretty good perl modules for doing XSLT. Notably XML::Sablotron, XML::LibXSLT and XML::Xalan.
    3. Basic XSLT is incredibly simple. To style a particular tag (say <foo>) as bold, you just use:
      <xsl:template match="foo"> <b><xsl:apply-templates/></b> </xsl:template>
      Yes, this is slightly verbose, but saving on typing isn't everything. And that's 90% of what you need to achieve in XSLT.
    I hope this helps in your decision process.