libxml can be quite a PITAPITCT - Pain In The Carpal Tunnel :)
#!/usr/bin/perl --
use strict; use warnings;
use XML::LibXML;
my $doc = XML::LibXML->new()->parse_string(
q{<?xml version='1.0' ?>
<roshambo xmlns="http://example.com/roshambo">
<sham>
<bo name="40" />
<bo name="2" />
</sham>
<sham>
<bo name="forty" />
<bo name="two" />
</sham>
</roshambo>
}
);
for my $node ( $doc->F( '//x:sham' ) ) {
print "@{[ $node->nodePath ]}\n";
for my $name ( $node->F( 'x:bo/@name' ) ) {
print "@{[ $name->nodePath ]} @{[$name->nodeValue]}\n";
}
print "\n\n";
}
$::xpc->registerNs( 'y', 'http://example.com/roshambo' );
print $doc->F('y:roshambo');
exit( 0 );
BEGIN {
$::xpc = XML::LibXML::XPathContext->new( );
$::xpc->registerNs( 'x', 'http://example.com/roshambo' );
sub XML::LibXML::Node::F {
my( $self, $xpath, $context ) = @_;
$::xpc->findnodes( $xpath, $context || $self );
}
}
__END__
Also if you're interested in a fancier nodePath, see XPATH DOM traverse html/xml
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|