in reply to XML::LibXML with namespace with no prefix

You can write xpaths like this

my $firstWeightXpath = qq{ //*:data[ attribute:key="$weightKey" ] [0] };

I use this helper , allows you to use it on dom and nodes, like

$dom->F( '//what:ever', 'what' => 'urn:yeahwhatever', 'that' => 'urn:thatalso', )->[0]->F('//what:else/that:there'); sub XML::LibXML::Node::F { my $self = shift; my $xpath = shift; my %prefix = @_; our $XPATHCONTEXT; $XPATHCONTEXT ||= XML::LibXML::XPathContext->new(); while( my( $p, $u ) = each %prefix ){ $XPATHCONTEXT->registerNs( $p, $u ); } $XPATHCONTEXT->findnodes( $xpath, $self ); }

Example of usage at Re^2: XML namespace question

I also use xpather.pl

Replies are listed 'Best First'.
Re^2: XML::LibXML with namespace with no prefix
by dallen16 (Sexton) on Dec 04, 2015 at 01:13 UTC

    Thank you. If I understand, your suggested XPath uses a wildcard for the namespace prefix, the "*" in "//*:data". Will give this a try. I think I follow the helper logic and will give it a try as well.

      . If I understand, your suggested XPath uses a wildcard for the namespace prefix, the

      Correct, thats what I said, but I got confused and I made a mistake

      This will work for sure

      //*[ local-name() = 'data' and attribute:key="$weightKey" ] [0]