Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

XML::LibXML::XPathContext findnodes() Question

by AbrahamB (Initiate)
on Dec 19, 2008 at 16:25 UTC ( #731578=perlquestion: print w/replies, xml ) Need Help??

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

test.xml:
<?xml version="1.0" encoding="UTF-8"?> <root> <element1>Element1 content</element1> <element2>Element2 content</element2> </root>
Assuming I am using XML::LibXML correctly, does anyone have an idea why this returns expected result :
my $xpath = ‘/root’; my $test = $doc->findnodes($xpath); print Dumper($test); $VAR1 = bless( [ bless( do{\(my $o = 13079328)}, 'XML::LibXML::Element +' ) ], 'XML::LibXML::NodeList' );
…But this returns empty array?
my $xpath = ’/root and /root/element1’; my $test = $doc->findnodes($xpath); print Dumper($test); $VAR1 = bless( [], 'XML::LibXML::NodeList' );

Replies are listed 'Best First'.
Re: XML::LibXML::XPathContext findnodes() Question
by Anonymous Monk on Dec 19, 2008 at 16:33 UTC
    Context
    #scalar my $test = $doc->findnodes($xpath); #list my @test = $doc->findnodes($xpath);
    wantarray
Re: XML::LibXML::XPathContext findnodes() Question
by runrig (Abbot) on Dec 19, 2008 at 19:34 UTC
    my $xpath = ’/root and /root/element1’;
    How can you be at both /root and /root/element1 at the same time (and I'm not even sure that 'and' can be used even that way)? Do you mean?:
    my $xpath = ’/root | /root/element1’;
    Anyway, in your first case, it returns a NodeList object (which will contain no nodes), and in the second case, it returns an empty list (if there were nodes, it would return a list of Node objects)...due to the difference in the scalar vs. list context behavior of findnodes(), as implied in the post above.
      I'm trying to test the existence of two separate elements.
      Like /root && /root/element1. I want true if both exist, false if one exists.

      The xpath can certainly be much better, but this is a simplified example from an xml-grep program I am working on.
      I'm trying to add the functionality to test for the existence of 2 nodes separated by ' and '. I may need to use find() or findvalue() for these types of xpaths.

      Thanks for your time!
        If /root/element1 exists, then /root certainly exists.

        You should probably use $doc->findvalue instead of findnodes because your XPath expression doesn't return a nodeset but boolean value.

        warn $doc->findvalue('/root and /root/element1'), "\n"; warn $doc->findvalue('/root and /root/nosuch'), "\n";

        yields:

        true false

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2022-01-24 21:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (65 votes). Check out past polls.

    Notices?