woosley has asked for the wisdom of the Perl Monks concerning the following question:

hi all, Below is the xml I want to parse with XPath, however, I met some problems with both XML::XPath and XML::LibXML;

<?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>
Here is the XML::XPath code:

my $xml = XML::XPath->new( xml => $string); my $nodeset = $xml->findnodes('//entry'); foreach my $node ( $nodeset->get_nodelist){ print $node->findvalue('//title'),"\n"; }
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?

Then I try to use XML::LibXML instead, here is the code

my $xml = XML::LibXML->new->parse_string( $string); my @nodeset = $xml->find('//entry'); print Dumper @nodeset;
I got
$VAR1 = bless( [], 'XML::LibXML::NodeList' );
which means I got nothing, entry nodes for this xml are not found. Then some guy told me I have to register the namespace for this xml. so I used this code:

my $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]);
and it returned:
http://api.douban.com/people/1010394http://api.douban.com/people/1276180

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

    The XPath expression in your last line should be './/atom:id'. Note the extra period, with it the expression searches for entries under the second argument, otherwise it searches in the entire document.

    You probably have the same problem with XML::XPath, but I'd recommend sticking to XML::LibXML, it is more powerful, more efficient and better maintained.

Re: xpath problem with XML::XPath and LibXML
by ikegami (Patriarch) on Jun 21, 2009 at 17:05 UTC
    It's just like unix directory paths. If it starts with "/", it's an absolute path (i.e. relative to the root).
    //atom:entry

    is short for

    /descendant::atom:entry

    which requests all descendants of "/" (root) which are "atom:entry" elements. You want all descendants of the current node, so you want one of the following:

    descendant::atom:entry # No leading "/" = rel to current node ./descendant::atom:entry # "." = current node .//atom:entry # Shortcut