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> |
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>