http://qs321.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.

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
    Will this scritp will also run in UNIX operating system? As I donot have any XML pareser which can be loaded for UNIX.

      Did you try?

      It will work if you have XML::XPath (and XML::Parser) installed. You also need expat, the XML parsing library, which comes installed on a lot of systems, or can be compiled from sources (just make sure you are using the same compiler you compiled Perl with)