mojo-jojo has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to find tags in an xml file which do not have a particular child. I am using the following code but it gives me an error Use of uninitialized value $local in substitution (s///) at /usr/local/lib/perl5/site_perl/5.10.0/XML/XPath/Node/Element.pm line 138.

my $xp = XML::XPath->new( filename => $_[0] ); my $nodeset = $xp->find('//descendant-or-self::node()[local-name(. +) = dependency and not(child::version)] '); # find all authors foreach my $node ( $nodeset->get_nodelist ) { print XML::XPath::XMLParser::as_string($node)."\n\n"; }
A snippet of xml file is given below
<project> <dependency> <!-- find all such dependency tags --> <groupId>com.solarmetric</groupId> <artifactId>kodo-jdo-runtime</artifactId> </dependency> <dependency> <groupId>javax.jdo</groupId> <artifactId>jdo</artifactId> <version>1.0.2</version> </dependency> </project>

Replies are listed 'Best First'.
Re: xpath query
by Anonymous Monk on Dec 27, 2011 at 11:02 UTC
    You're doing it wrong.
    use XML::LibXML qw(); XML::LibXML ->load_xml(string => <<'XML')->find('//dependency[not(version)]'); <project> <dependency> <!-- find all such dependency tags --> <groupId>com.solarmetric</groupId> <artifactId>kodo-jdo-runtime</artifactId> </dependency> <dependency> <groupId>javax.jdo</groupId> <artifactId>jdo</artifactId> <version>1.0.2</version> </dependency> </project> XML
    The expression returns a XML::LibXML::NodeList referencing a single XML::LibXML::Element representing the first dependency element.find(
      His XPath searched for "dependency" elements regardless of their namespace. Your XPath searches for "dependency" elements in the null namespace.
      why cant I find with the xpath query I have and the code I am using, cant I change the query to make it work?
        Found the solution by using the following code
        my $xp = XML::XPath->new( filename => $_[0] ); my $nodeset = $xp->find('//dependency[not(child::version)] '); +# find all authors foreach my $node ( $nodeset->get_nodelist ) { print XML::XPath::XMLParser::as_string($node)."\n\n"; }
Re: xpath query
by choroba (Cardinal) on Dec 28, 2011 at 14:28 UTC
    Strings must be quoted in XPath, i.e.
    local-name(.)="dependency"