http://qs1969.pair.com?node_id=421956


in reply to Using Perl XPath for converting Infopath XML files to Word Documents

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.

Finally, you wrote: the XPath Perl module which is part of the XML module. Not quite, XML::XPath is a module in the XML namespace, just like XML::Parser, XML::Simple or any other XML:: module.