note
mirod
<p>A few comments:</p>
<p>You seem to think that in an XPath expression '//' denotes the top of the tree. It doesn't. The path you should be using is <tt>/Books/Book</tt>. '//' is more like a wildcard: <tt>//book</tt> will find all the book nodes in the document. Using '//' in your case forces the XPath engine to test basically all nodes in the document, while <tt>/Books/Book</tt> is much more efficient, and tests only the root and first-level children. For a good XPath tutorial have a look at <a href="http://zvon.org/xxl/XPathTutorial/General/examples.html">zvon.org</a>.</p>
<p>A couple of minor stylistic quibbles: I don't think you need to write <tt>foreach my $book ($xp->find('/Books/Book')->get_nodelist)</tt>, as <tt>find</tt> in list context will return an array, so you can just write <tt>foreach my $book ($xp->find('/Books/Book'))</tt>; you could also replace <tt>$book->find('author')->string_value</tt> by simply <tt>$book->findvalue('author')</tt>, which, besides being shorter, brings also the added benefit that it won't die if for some reason the <tt>author</tt> element is not present.</p>
<p>Finally, you wrote: <i>the XPath Perl module which is part of the XML module</i>. Not quite, [cpan://XML::XPath] is a module in the XML namespace, just like [XML::Parser], [XML::Simple] or any other XML:: module.</p>
421936
421936