in reply to XML::LibXML drives me to drinking
What you're wanting is $xpathcontext->findnodes( $xpathstring, $startnode )
I much prefer my version $node->F($xpathstr, 'prefix'=>'http...')
#!/usr/bin/perl -- use strict; use XML::LibXML; my $string = qq~<?xml version="1.0"?> <ItemLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceS +ervice/2013-08-01"> <Items> <Item> <ASIN>B01KI4JSQY</ASIN> </Item> </Items> </ItemLookupResponse> ~; my $dom = XML::LibXML->new(qw/ recover 2 / )->load_xml(string => $str +ing, {no_blanks => 1}); # *register* namespace $dom->F( '/', 'x', 'http://webservices.amazon.com/AWSECommerceService/2013-08-01 +' ); foreach my $item ( $dom->F('/x:ItemLookupResponse/x:Items/x:Item' )){ print $item->firstChild->nodeName, "\n"; print $item->firstChild->toString, "\n"; print $item->F('x:ASIN')->shift()->string_value, "\n"; print $item->F('./x:ASIN/text()'), "\n"; print $item->F('./x:ASIN')->[0]->textContent, "\n"; } 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 ); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: XML::LibXML drives me to drinking
by tunafish (Beadle) on Oct 23, 2016 at 03:59 UTC | |
by Anonymous Monk on Oct 23, 2016 at 05:00 UTC |
In Section
Seekers of Perl Wisdom