Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Template Toolkit && XPath plugin

by holygrail (Scribe)
on Jun 26, 2001 at 20:22 UTC ( [id://91645]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I really don't have a clue what is going on. I have a very simple XML file (see below), and an even simpler template. When I try to run this through tpage template.html it barfs out with the following error:
bash-2.03$ tpage template.html undef error - junk after document element at line 18, column 0, byte 221: </section> </site> <site language="fr"> ^ <section name="section 1"> <intro> at /usr/local/lib/site_perl/i386-linux/XML/Parser.pm line 185
So here is the XML file (called test.xml):
<?xml version="1.0"?> <site language="en"> <section name="section 1"> <intro> My darling </intro> <maintext> I love you </maintext> </section> <section name="section 2"> <maintext> The Bla </maintext> </section> </site> <site language="fr"> <section name="section 1"> <intro> mon amour </intro> <maintext> Je t'aime </maintext> </section> <section name="section 2"> <maintext> bla </maintext> </section> </site>
and here is the template (called template.html):
[% USE xpath = XML.XPath(file => "./test.xml") %] [% FOREACH site = xpath.findnodes('site') %] <h1>[% site.getAttribute('language') %]</h1> [% FOREACH section = xpath.findnodes('site/section') %] <h2>[% section.getAttribute('name') %]</h2> [% maintext = section.findnodes('maintext') %] [% maintext.string_value %] [% END %] [% END %]
when I remove the "french" part of the XML page (everything after the first </site>), it works like it should

Anyone?
--HolyGrail

Replies are listed 'Best First'.
Re: Template Toolkit && XPath plugin
by davorg (Chancellor) on Jun 26, 2001 at 20:32 UTC

    Your XML is not well-formed. A well-formed XML file has only one top-level element. When you remove the French data you end up with just one top-level element (<site>), which is why it then works.

    To make it well-formed, you could put a new <sites> ... </sites> element around the whole thing.

    --
    <http://www.dave.org.uk>

    Perl Training in the UK <http://www.iterative-software.com>

Re: Template Toolkit && XPath plugin
by Anonymous Monk on Jun 26, 2001 at 20:31 UTC
    A xml-File as parsed by Expat could not have more than one Node. You had two! Shame!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://91645]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-18 07:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found