woosley has asked for the wisdom of the Perl Monks concerning the following question:
Here is the XML::XPath code:<?xml version="1.0" encoding="UTF-8"?> <feed xmlns="http://www.w3.org/2005/Atom" xmlns:db="http://www.douban. +com/xmlns/" xmlns:gd="http://schemas.google.com/g/2005" xmlns:opensearch="http://a9.com/-/spec/opensearchrss/1.0/"> <title>friends</title> <author> <link href="http://api.douban.com/people/ahbei" rel="self"/> <link href="http://www.douban.com/people/ahbei/" rel="alternate"/> <link href="http://t.douban.com/icon/u1000001-14.jpg" rel="icon"/> <name>ahbei</name> <uri>http://api.douban.com/people/1000001</uri> </author> <opensearch:startIndex>1</opensearch:startIndex> <opensearch:itemsPerPage>2</opensearch:itemsPerPage> <opensearch:totalResults>114</opensearch:totalResults> <entry> <id>http://api.douban.com/people/1010394</id> <title>Engo</title> <link href="http://api.douban.com/people/1010394" rel="self"/> <link href="http://www.douban.com/people/akin/" rel="alternate"/> <link href="http://t.douban.com/icon/u1010394-10.jpg" rel="icon"/> <link href="http://www.halfull.cn" rel="homepage"/> <content> http://www.douban.com/group/topic/3750385/ </content> <db:location id="beijing">beijing</db:location> <db:uid>akin</db:uid> </entry> <entry> <id>http://api.douban.com/people/1276180</id> <title>wo</title> <link href="http://api.douban.com/people/1276180" rel="self"/> <link href="http://www.douban.com/people/movie007wn/" rel="alternate"/ +> <link href="http://t.douban.com/icon/u1276180-40.jpg" rel="icon"/> <content> ??????????????? </content> <db:location id="beijing">beijing</db:location> <db:uid>movie007wn</db:uid> </entry> </feed>
I got friendsEngowo friendsEngowo which are all the title nodes of this xml, it seems like each $node has all the information of this XML. Dose any one knows what the problem is?my $xml = XML::XPath->new( xml => $string); my $nodeset = $xml->findnodes('//entry'); foreach my $node ( $nodeset->get_nodelist){ print $node->findvalue('//title'),"\n"; }
Then I try to use XML::LibXML instead, here is the code
I gotmy $xml = XML::LibXML->new->parse_string( $string); my @nodeset = $xml->find('//entry'); print Dumper @nodeset;
and it returned: http://api.douban.com/people/1010394http://api.douban.com/people/1276180my $node = XML::LibXML->new->parse_string($string); my $xml = XML::LibXML::XPathContext->new( $node); $xml->registerNs('atom','http://www.w3.org/2005/Atom'); my @nodeset = $xml->findnodes('//atom:entry'); print $xml->findvalue('//atom:id',$nodeset[0]);
which have both two ID tags for the first entry node. It really drives me crazy. Can anybody help me?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: xpath problem with XML::XPath and LibXML
by mirod (Canon) on Jun 21, 2009 at 12:07 UTC | |
|
Re: xpath problem with XML::XPath and LibXML
by ikegami (Patriarch) on Jun 21, 2009 at 17:05 UTC |