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

XML::LibXML::XPathContext exists since XML::LibXML 1.61, and it's the appropriate way of working with namespaces in XPaths for XML::LibXML.

my $xpc = XML::LibXML::XPathContext->new( $doc ); $xpc->registerNs( ota => 'http://www.opentravel.org/OTA/2003/05' ); my @options = $xpc->findnodes('//ota:OriginDestinationOption');

How did one do the above before XML::LibXML 1.61?

# Doesn't work because OriginDestinationOption belong to # the http://www.opentravel.org/OTA/2003/05 NS, not the # null NS. my $root = $doc->documentElement(); my @opts = $root->findnodes("//OriginDestinationOption");

If there was a prefix in play, I believe I could specify it, but the elements are using the default namespace.

# Doesn't work because OriginDestinationOption element # appears as <OriginDestinationOption>, rather than # <ota:OriginDestinationOption> my $root = $doc->documentElement(); my @opts = $root->findnodes("//ota:OriginDestinationOption");

The only thing I've found that works is

my $root = $doc->documentElement(); my @opts = $root->findnodes("//*[name()="OriginDestinationOption"]');

yuck!

Replies are listed 'Best First'.
Re: XML::LibXML and namespaces, before 1.61
by grantm (Parson) on May 13, 2009 at 03:19 UTC
      The wording in Changes made me think that might be the case, but I couldn't find it. Thanks!