#!/usr/bin/perl -- use strict; use warnings; use XML::LibXML; use Data::Dump qw/ dd pp /; Main( @ARGV ); exit( 0 ); sub ROOTNS { my( $self ) = @_; $::xpcroot = ROOT( $self ); $::xpc = XML::LibXML::XPathContext->new( ); for my $node ( $::xpcroot->F('//*') ){ for my $att( $node->attributes() ){ if( $att->isa( "XML::LibXML::Namespace" ) ){ my $suffix = $att->localname; my $value = $att->value; print '# autoRegisterNs( ', pp( $suffix ), ' => ', pp( $value ) , " );\n"; $::xpc->registerNs( $suffix => $value ); } } } } sub ROOT { my( $self ) = @_; my $parent = $self; while( my $newparent = $parent->getParentNode ){ $parent = $newparent; } return $parent; } BEGIN { $::xpc = XML::LibXML::XPathContext->new(); sub XML::LibXML::Node::F { my( $self, $xpath, $context ) = @_; unless( $::xpcroot and $::xpcroot == ROOT( $self ) ){ ROOTNS( $self ); } $::xpc->findnodes( $xpath, $context || $self ); } } sub Main { my $dom = XML::LibXML->new( qw/ recover 2 / )->load_xml( string => q{ }, ); print $_->nodePath,"\n" for $dom->F(q{//book:locator/@xlink:href}); } __END__ # autoRegisterNs( "book" => "http://generated.ns/book" ); # autoRegisterNs( "xlink" => "http://www.w3.org/1999/xlink" ); /book/book:chapter/book:locator[1]/@xlink:href /book/book:chapter/book:locator[2]/@xlink:href /book/book:chapter/book:locator[3]/@xlink:href