Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: how to get XML::LibXML perfect xpath query ?

by Kanishka.black0 (Scribe)
on Jun 13, 2010 at 05:04 UTC ( #844403=note: print w/ replies, xml ) Need Help??


in reply to Re: how to get XML::LibXML perfect xpath query ?
in thread how to get XML::LibXML perfect xpath query ?

likely i want the source to an array ... in second example .. /rsp/sizes/size (source&&url) in two separate arrays


Comment on Re^2: how to get XML::LibXML perfect xpath query ?
Replies are listed 'Best First'.
Re^3: how to get XML::LibXML perfect xpath query ?
by ikegami (Pope) on Jun 13, 2010 at 05:24 UTC
    my @images; for my $node ($doc->findnodes('/rsp/sizes/size')) { my $source = $node->getAttribute('source'); my $url = $node->getAttribute('url'); push @images, [ $source, $url ]; }
      when i tried to use the same xpath expression ...
      use XML::LibXML; my $parser = XML::LibXML->new(); my $doc = $parser->parse_file('yahoo.xml'); my @images; for my $node ($doc->findnodes('/ysearchresponse/resultset_web/result/u +rl')) { my $source = $node->getValue; push @images, $source; print $source; }
      <ysearchresponse xmlns="http://www.inktomi.com/" responsecode="200"> <nextpage><![CDATA[/ysearch/web/v1/asin?format=xml&count=10&appid=sa +dfasdfaASDFASDFasdfasFASDFAsdfLwkYbXsNROPqpep8-&start=10]]></nextpage +> <resultset_web count="10" start="0" totalhits="12642427" deephits="8 +6800000"> <result> <abstract><![CDATA[Includes <b>Asin</b> photos, <b>Asin</b> goss +ip, <b>Asin</b> biography, <b>Asin</b> wallpapers, <b>Asin</b> filmog +raphy, <b>Asin</b> videos, <b>Asin</b> pictures, <b>Asin</b> awards, +<b>Asin</b> fan clubs]]></abstract> <clickurl>http://lrd.yahooapis.com/_ylc=X3oDMTU4cnBla3ZvBF9TAzIw +MjMxNTI3MDIEYXBwaWQDUXpyMzVLelYzNEgxYzNVZUhMZ3lQck15NG0ySXBpT296QkRhT +WJ4Z2l1NGlFVmlMd2tZYlhzTlJPUHFwZXA4LQRjbGllbnQDYm9zcwRzZXJ2aWNlA0JPU1 +MEc2xrA3RpdGxlBHNyY3B2aWQDU0puLkNXS0ljcnFUcHZka2ZxVVhkWVFRZFdGNVlrd1V +nVjRBQWh5UQ--/SIG=11r57fk7t/**http%3A//movies.sulekha.com/stargallery +/asin/default.htm</clickurl> <date>2010/06/11</date> <dispurl><![CDATA[<b>movies.sulekha.com</b>/<wbr>stargallery/<b> +asin</b>/default.htm]]></dispurl> <size>77035</size> <title><![CDATA[<b>Asin</b>, <b>Asin</b> Photo Gallery, <b>Asin< +/b> Wallpapers, <b>Asin</b> Videos, Fans <b>...</b>]]></title> <url>http://movies.sulekha.com/stargallery/asin/default.htm</url +></result> <result> <abstract><![CDATA[<b>Asin</b> was born in Cochin, Kerala, India + on the 26th of October 1981 in the Malayalam <b>...</b> <b>Asin</b> +starred in the original Telugu version, so she was the obvious <b>... +</b>]]></abstract> <clickurl>http://lrd.yahooapis.com/_ylc=X3oDMTU4cnBla3ZvBF9TAzIw +MjMxNTI3MDIEYXBwaWQDUXpyMzVLelYzNEgxYzNVZUhMZ3lQck15NG0ySXBpT296QkRhT +WJ4Z2l1NGlFVmlMd2tZYlhzTlJPUHFwZXA4LQRjbGllbnQDYm9zcwRzZXJ2aWNlA0JPU1 +MEc2xrA3RpdGxlBHNyY3B2aWQDU0puLkNXS0ljcnFUcHZka2ZxVVhkWVFRZFdGNVlrd1V +nVjRBQWh5UQ--/SIG=11bj69fq1/**http%3A//www.imdb.com/name/nm1821791/bi +o</clickurl> <date>2010/05/09</date> <dispurl><![CDATA[www.<b>imdb.com</b>/name/nm1821791/<wbr>bio]]> +</dispurl> <size>32925</size> <title><![CDATA[<b>Asin</b> (I) - Biography]]></title> <url>http://www.imdb.com/name/nm1821791/bio</url> </result> </resultset_web> </ysearchresponse>

      I am getting empty array's .....

        See doc for findnodes, you need to bind a prefix to the namespace (xmlns), so
        my $xpc = XML::LibXML::XPathContext->new; $xpc->registerNs( 'x', 'http://www.inktomi.com/' ); $xpc->findnodes( '/x:ysearchresponse/x:resultset_web/x:result/x:url', +$doc ) $xpc->findnodes( '*/*/x:result/x:url', $doc )

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://844403]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2015-07-28 12:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (254 votes), past polls