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

All --

I just started Perl last week kinda as a test for this project, so my syntax isn't the greatest -- not too mention I am forced to switch between .Net, C++, Java, and VBScript while doing it.


Basically the task is to test the speed at which Perl & Template Toolkit can parse the data. I've been somewhat successful with XML::Simple, but I'm lacking the idea on how to tell if there are any more nodes left.


The basic XML is:

<prices> <section name="blah"> <open>5</open> <ttype name="kid" price="50.00" /> <ttype ...> </section> </prices>

Using the following Perl, I call the template toolkit, using ActivePerl 5.8 on Win2K.

use strict; use Template; my $tt = Template->new(); $tt->process('config.tt', {}) or die $tt->error();

The toolkit template is what's messing me up.. I've originally done

[%- USE xml = XML.Simple('config.xml') %] [%- FOREACH a = xml.keys() %] [% a %]: [%- FOREACH b = xml.$a.keys() %] [% b %] [%- END %] [%- END %] [%- END %]

and so on... this was simply to dump the data to the screen in order to test the speed. However, the XML can change in depth, and I can't (or more accurately, don't know how) to test if the node has keys or not.

From what I've read, XML::Simple may not be the fastest way (I believe it uses SAX?) and am also trying to use the DOM -- albeit all through Template Toolkit -- of which there are very little examples. Additionally, it tends to ignore the ones with more than one attribute, so I figure try the DOM. Here's what I've tried in XML::Dom to no avail:

[%- USE xml = XML.DOM %] [% dom = xml.parse( 'config.xml' ) %] [% FOREACH product = dom.getElementsByTagName('price') %] [% product.getAttribute('ttype') %]: [% VIEW ttype%] [% BLOCK product %] [% item.content(view) %] [% END %] [%- END %] [%-END%] [% #USE Dumper %] [%# Dumper.dump(xml) %]

All this will print out is ":"

Before I give up, I figure I'd try it here.. .someone in the company told me to use XML::PerlSAX module, but I've seen a few reviews that says it's buggy at best.

Now, I guess the question is, how do I get the XML to parse inside the template toolkit template? It seems like it should be simple enough, but I can't quite get it going, or it only works 3/4ths of the way.
Thanks --
DesertGhost

Replies are listed 'Best First'.
Re: Template Toolkit and XML
by perrin (Chancellor) on Apr 20, 2004 at 22:10 UTC
    Why are you trying to jam this into the template? A better approach would be to parse the XML first and then hand what you want to print out to the template.

    I believe the current champ for speed is XML::LibXML, and there is a Template Toolkit plugin for it at Template-Plugin-XML-LibXML if you really want it. Also, if you install XML::LibXML, XML::Simple will use it, and that will speed things up.

      I've thought about parsing first, and then placing things into the template, but it seems the requirement is for the template to do it all. For instance, the Perl code should remain the same (I imagine it'd be called from a server, but that part of the project is unknown to me), and the template would be processed

      I'll check out that library... thanks. I guess this whole thing is to test out the power of Perl/TT before it gets implemented, or more accurately, to see if it's worth it to implement it.

      So you're saying the template can't handle that type of processing? I've been dinking around with the 'VIEWS' part of the toolkit, but can't seem to get anything to work correctly.

        Since you can use in-line Perl if you really want to, there is nothing you can do in Perl that you can't do in TT. However, the point of a templating system is to separate your code from your presentation. You want to do processing stuff in perl, where it is most easily expressed, and presentation stuff in TT, where it is most easily expressed.

        The requirement to "do it all" in the template is misguided, and I'd suggest you try to find out who came up with that and why. It will work, but it makes your template more complex and possibly too difficult for a non-programmer to modify.

        What are you trying to do? The template isn't doing the parsing. It is using XML::Simple or XML::DOM to do the parsing. And then doing something with the result.

        I think it makes much more sense to do the parsing outside the template. Then feed to the parsed data, or even the DOM tree, to the template to output.