http://www.perlmonks.org?node_id=844371

Kanishka.black0 has asked for the wisdom of the Perl Monks concerning the following question:

i have tried to use the LibXML modules to xpath a document but its getting complicated .......... i have seen this example over forum here .........

use XML::LibXML ; use strict; use warnings; { my $xml = <<'XML'; <?xml version="1.0" standalone="yes"?> <sdnList> <sdnEntry> <lastName>Hello world!</lastName> </sdnEntry> </sdnList> XML my $parser = XML::LibXML->new; my $doc = $parser->parse_string($xml); my $result = $doc->findvalue('//lastName'); print $result; } { my $xml = <<'XML'; <?xml version="1.0" standalone="yes"?> <sdnList xmlns="http://tempuri.org/sdnList.xsd"> <sdnEntry> <lastName>Hello world!</lastName> </sdnEntry> </sdnList> XML my $parser = XML::LibXML->new; my $doc = $parser->parse_string($xml); my $result = $doc->findvalue('//lastName'); print $result ; }
/ One of the member posted this solution ... /
use XML::LibXML; use XML::LibXML::XPathContext; { my $xml = <<'XML'; <?xml version="1.0" standalone="yes"?> <sdnList xmlns="http://tempuri.org/sdnList.xsd"> <sdnEntry> <lastName>Hello world!</lastName> </sdnEntry> </sdnList> XML my $parser = XML::LibXML->new; my $doc = $parser->parse_string($xml); my $xc = XML::LibXML::XPathContext->new($doc); $xc->registerNs('sdnList', 'http://tempuri.org/sdnList.xsd'); my $result = $xc->findvalue('//sdnList:lastName'); is( $result, "Hello world!", "Namespace" ); }

But every time we can't get same namespace ... if i add any API to the xml input ..... its extremely difficult to handle it ......

Is there any way to get nodes and attr and like that stuff in XML::LibXML easily ??? or any other modules excpet xml::xpath..

UPDATE ...... i think i have not conveyed the message properly ... about my last part .. .

if the XML page varies every time ... like this ....

lets take one example ... Flickr API http://www.flickr.com/services/api/explore/?method=flickr.photos.getSizes .... here we go .... if give the correct Photo ID i will get this result ...

<rsp stat="ok"> <sizes canblog="1" canprint="0" candownload="0"> <size label="Square" width="75" height="75" source="http://farm5.stati +c.flickr.com/4007/4673769513_02826f0775_s.jpg" url="http://www.flickr +.com/photos/dhushor-jen/4673769513/sizes/sq/" media="photo"/> <size label="Thumbnail" width="100" height="67" source="http://farm5.s +tatic.flickr.com/4007/4673769513_02826f0775_t.jpg" url="http://www.fl +ickr.com/photos/dhushor-jen/4673769513/sizes/t/" media="photo"/> <size label="Small" width="240" height="160" source="http://farm5.stat +ic.flickr.com/4007/4673769513_02826f0775_m.jpg" url="http://www.flick +r.com/photos/dhushor-jen/4673769513/sizes/s/" media="photo"/> <size label="Medium" width="500" height="333" source="http://farm5.sta +tic.flickr.com/4007/4673769513_02826f0775.jpg" url="http://www.flickr +.com/photos/dhushor-jen/4673769513/sizes/m/" media="photo"/> <size label="Large" width="1024" height="683" source="http://farm5.sta +tic.flickr.com/4007/4673769513_02826f0775_b.jpg" url="http://www.flic +kr.com/photos/dhushor-jen/4673769513/sizes/l/" media="photo"/> </sizes> </rsp>

what if i give incorrect photo id in the above code ....

<rsp stat="fail"> <err code="1" msg="Photo not found"/> </rsp>

Can any one tell me how can write xpath this kind of things ?