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?


In reply to xpath problem with XML::XPath and LibXML by woosley

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.