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 ?


In reply to how to get XML::LibXML perfect xpath query ? by Kanishka.black0

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.