A few comments:
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 /Books/Book. '//' is more like a wildcard: //book 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 /Books/Book is much more efficient, and tests only the root and first-level children. For a good XPath tutorial have a look at zvon.org.
A couple of minor stylistic quibbles: I don't think you need to write foreach my $book ($xp->find('/Books/Book')->get_nodelist), as find in list context will return an array, so you can just write foreach my $book ($xp->find('/Books/Book')); you could also replace $book->find('author')->string_value by simply $book->findvalue('author'), which, besides being shorter, brings also the added benefit that it won't die if for some reason the author element is not present.
|Replies are listed 'Best First'.|
Re^2: Using Perl XPath for converting Infopath XML files to Word Documents
by Anonymous Monk on Sep 22, 2005 at 09:59 UTC
by mirod (Canon) on Sep 22, 2005 at 10:28 UTC