in reply to XML::LibXML drives me to drinking
You used the proper namespace here:
$xml->findnodes('/x:ItemLookupResponse/x:Items/x:Item')
But you forgot here:
$item->findvalue('ASIN')
Change the latter to
$xml->findvalue('x:ASIN', $item)
Other issues:
Your variable names are awful! You call your XML $string while $xml is an XPathContext object for which $xpc is recommended, and you call your document $parser where $doc or $dom make more sense.
You call load_xml as an object method, but it's a static method like new.
XML::LibXML::XPathContext isn't loaded by use XML::LibXML; in older versions of XML::LibXML.
Fixed:
#!/usr/bin/perl use strict; use warnings qw( all ); use feature qw( say ); use XML::LibXML qw( ); use XML::LibXML::XPathContext qw( ); my $xml = <<'__EOS__'; <?xml version="1.0"?> <ItemLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceS +ervice/2013-08-01"> <Items> <Item> <ASIN>B01KI4JSQY</ASIN> </Item> </Items> </ItemLookupResponse> __EOS__ my $doc = XML::LibXML->load_xml(string => $xml, { no_blanks => 1 }); my $xpc = XML::LibXML::XPathContext->new(); $xpc->registerNs('x', 'http://webservices.amazon.com/AWSECommerceServi +ce/2013-08-01'); for my $item ($xpc->findnodes('/x:ItemLookupResponse/x:Items/x:Item', +$xml)) { say $item->firstChild->nodeName; say $item->firstChild->toString; say $xpc->findvalue('x:ASIN', $item); }
|
|---|