Skip Menu |
 

This queue is for tickets about the XML-LibXML CPAN distribution.

Report information
The Basics
Id: 84726
Status: rejected
Priority: 0/
Queue: XML-LibXML

People
Owner: Nobody in particular
Requestors: sjf4 [...] uw.edu
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: findnodes behavior
Date: Thu, 18 Apr 2013 23:30:29 +0000
To: "bug-XML-LibXML [...] rt.cpan.org" <bug-XML-LibXML [...] rt.cpan.org>
From: Stephen Fralich <sjf4 [...] uw.edu>
Download (untitled) / with headers
text/plain 1.5k
In the below example, $book->findnodes searches the entire contents of $doc, rather than just $book. I would think "my @stuff = $book->findnodes('/library/book/title');" would return nothing, but instead it returns all the titles in $doc. getChildrenByTagName is probably what you really should be using, but one of the top results on google has the below example. use XML::LibXML; my $parser = XML::LibXML->new(); my $doc = $parser->parse_file($filename); foreach my $book ($doc->findnodes('/library/book')) { my($title) = $book->findnodes('./title'); print $title->to_literal, "\n" } <library> <book> <title>Perl Best Practices</title> <author>Damian Conway</author> <isbn>0596001738</isbn> <pages>542</pages> <image src="http://www.oreilly.com/catalog/covers/perlbp.s.gif" width="145" height="190" /> </book> <book> <title>Perl Cookbook, Second Edition</title> <author>Tom Christiansen</author> <author>Nathan Torkington</author> <isbn>0596003137</isbn> <pages>964</pages> <image src="http://www.oreilly.com/catalog/covers/perlckbk2.s.gi +f" width="145" height="190" /> </book> <book> <title>Guitar for Dummies</title> <author>Mark Phillips</author> <author>John Chappell</author> <isbn>076455106X</isbn> <pages>392</pages> <image src="http://media.wiley.com/product_data/coverImage/6X/07 +645510/076455106X.jpg" width="100" height="125" /> </book> </library>
Download (untitled) / with headers
text/plain 738b
On Thu Apr 18 19:32:22 2013, sjf4@uw.edu wrote: Show quoted text
> In the below example, $book->findnodes searches the entire contents of > $doc, rather than just $book. I would think "my @stuff = $book-
> >findnodes('/library/book/title');" would return nothing, but instead
> it returns all the titles in $doc.
That's expected since you start your XPath expression with a '/' meaning that the search should start from the root node. In this case the context node doesn't matter. To further clarify $doc->findnodes('/library/book/title') # All titles $book->findnodes('/library/book/title') # All titles $doc->findnodes('library/book/title') # All titles $book->findnodes('library/book/title') # Empty Hope this helps, Nick Nick
RESOLVEing as INVALID Then. A misunderstanding of how XPath works. Thanks to Neil for his analysis.


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.