Probably ought to store the context in the ROOT, oh well #!/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{<?xml version="1.0" ?>
<book xmlns:book="http://generated.ns/book" xmlns:xlink="http://www.w3
+.org/1999/xlink">
<book:chapter id="bk444444ch1" type="CHAPTER">
<book:locator xlink:href="/book/isbn/979-0-4444-1000-17/book-part/
+chapter/bk444444ch1?releaseStatus=RELEASED" xlink:title="Photonic cry
+stal light-emitting sources" xlink:type="locator"></book:locator>
<book:locator xlink:href="/book/isbn/979-0-4444-1000-17/book-part/
+chapter/bk444444ch1?releaseStatus=RELEASED&format=pdf" xlink:titl
+e="Photonic crystal light-emitting sources" xlink:type="locator"></bo
+ok:locator>
<book:locator xlink:href="/book/isbn/979-0-4444-1000-17/book-part/
+chapter/bk444444ch1?releaseStatus=RELEASED&format=epub" xlink:tit
+le="Photonic crystal light-emitting sources" xlink:type="locator"></b
+ook:locator>
</book:chapter>
</book>},
);
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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|