stevensun has asked for the wisdom of the Perl Monks concerning the following question:

I've got an XML file and need to parse out the measurements for a particular product. The measurement type is determined by <c093> tag (01- height 02-width 03-length 04-weight). I try to query it with this code but the Xpath query keeps saying "error in xpath expression" even though it works in my XML editor.
the code is:
use strict; use XML::Twig; use XML::XPath; use XML::XPath::XMLParser; my $twig= new XML::Twig( TwigHandlers => { product => \&product } ); + $twig->parsefile("file.xml"); my @height = $twig->get_xpath ('//measure[string(c093)="01"]'); #if i + want to get the length
****************************************************** the xml file is:
<message> <product> <measure> <c093>01</c093> <c094>229.0</c094> <c095>mm</c095> </measure> <measure> <c093>02</c093> <c094>152.0</c094> <c095>mm</c095> </measure> <measure> <c093>03</c093> <c094>15.0</c094> <c095>mm</c095> </measure> <measure> <c093>08</c093> <c094>24</c094> <c095>oz</c095> </measure> </product>

20060311 Janitored by Corion: Added code tags around code and data

Replies are listed 'Best First'.
Re: question on XML Twig and XPath query
by Aristotle (Chancellor) on Mar 11, 2006 at 07:08 UTC

    The XML::Twig documentation says this:

    XML::Twig implements a subset of XPath through the get_xpath method.

    If you want to use the whole XPath power, then you can use XML::Twig::XPath instead. In this case XML::Twig uses XML::XPath to execute XPath queries. You will of course need XML::XPath installed to be able to use XML::Twig::XPath.

    so do that.

    Makeshifts last the longest.

      Aristotle, thanks for your help! Corion, thanks for cleaning up my XML!