Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

XML::Twig Question

by prasadbabu (Prior)
on Jul 29, 2006 at 12:31 UTC ( [id://564517]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

Thanks for the replies given for my yesterday post XML::Twig 'cut' and 'paste' Question and it helped me to identify the problem. To get around that problem we came up with new idea by filtering in XPath expression itself. So instead of filtering that in loop we decided to filter using XPath Expressions like these '/article//fig[label and caption]//label', '/article/fig[label and caption]/label'. It is a valid XPath Expressiong. When i tested the XPath Expressions using XSLT it is working fine but through Twig it fails.

use strict; use XML::Twig; my $twig = new XML::Twig( ); $twig->parsefile('1.xml'); my $count = $twig->get_xpath('/article/fig[label and caption]/label'); + $twig->print; print "$count";
Error: ------ error in xpath expression /article/fig[label and caption]/label at lab +el and c aption at C:/Perl/site/lib/XML/Twig.pm line 2784

When i tried that by using the above code, it is giving error as shown above. Is it not possible to use that XPath expression in Twig. Why the error?

Thanks in advance

Prasad

Replies are listed 'Best First'.
Re: XML::Twig Question
by Tanktalus (Canon) on Jul 29, 2006 at 14:40 UTC

    Reading the docs for XML::Twig, I see that "get_xpath" actually:

    Return a list of elements satisfying the $xpath. $xpath is +an XPATH-like expression.
    Note where it says "XPATH-like". It goes on to say that only a subset of the XPATH abbreviated syntax is covered. It appears that checking for sub-elements is not part of the [] syntax that XML::Twig supports.

    All is not (yet) lost, as it does say at the bottom of the entry for get_xpath that "you can use XML::Twig::XPath to get "findnodes" to use XML::XPath as the XPath engine, with full coverage of the spec." Thus, you may need to install a few more modules to get this to work (e.g., ensure XML::XPath is installed - XML::Twig::XPath is part of XML::Twig).

    In my answer to your previous question, I worked around this by looking for both elements, and skipping the current fig element if it didn't have both, and then using the label element for the work. I'm not sure how that is not acceptable to you.

Re: XML::Twig Question
by Ieronim (Friar) on Jul 29, 2006 at 19:59 UTC
    Consider the following code (it works as expected):
    #!/usr/bin/perl use warnings; use strict; use XML::Twig::XPath; my $twig = XML::Twig::XPath->new( pretty_print => 'indented', ); $twig->parse(<<'XML'); <?xml version="1.0"?> <article> <label>here label for testing</label> <fig id="fig001" position="float"><label>Fig. 1.</label><caption><p>fi +rst</p></caption></fig> <fig id="fig002" position="float"><label>Fig. 2.</label><caption><p>se +cond</p></caption></fig> <fig id="fig003" position="float"><label>Fig. 3.</label><caption><p>th +ird</p></caption></fig> <fig id="fig004" position="float"><label>Fig. 4.</label><caption><p>fo +urth</p></caption></fig> <fig id="fig006" position="float"><caption><p>sixth</p></caption></fig +> <fig id="fig005" position="float"><label>Fig. 5.</label><caption><p>fi +fth</p></caption></fig> <fig id="fig006" position="float"><label>Fig. 6.</label><caption><p>si +xth</p></caption></fig> <fig id="fig006" position="float"><label>Fig. 6.</label></fig> </article> XML my @labels = $twig->findnodes('/article/fig[label and caption]/label') +; my @captions = $twig->findnodes('/article/fig[label and caption]/capti +on'); for my $i (0..$#labels) { $labels[$i]->move('first_child', $captions[$i]); } $twig->print;

         s;;Just-me-not-h-Ni-m-P-Ni-lm-I-ar-O-Ni;;tr?IerONim-?HAcker ?d;print

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-24 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found