开发者

How can I extract the desired nodes from this XML file using Perl and XPath?

After executing a XPath expression for extracting all year and value elements associated with death rates from a XML DB file, I want to take each node from the node list and find the year node, print that, find the value node, and print that all separately. The problem is that the output does not show anything.

The XML content looks like this:

<dataset type="country" name="Afghanistan" total="222">
...
        <data>
             <country id="AFG">Afghanistan</country>
             <indicator id="SP.DYN.CDRT.IN">Death rate, crude (per 1,000 people)</indicator>
             <year>2006</year>
             <value>20.3410000</value>
           </data>
           <data>
             <country id="AFG">Afghanistan</country>
             <indicator id="SP.DYN.CDRT.IN">De开发者_运维问答ath rate, crude (per 1,000 people)</indicator>
             <year>2007</year>
             <value>19.9480000</value>
           </data>
           <data>
             <country id="AFG">Afghanistan</country>
             <indicator id="SP.DYN.CDRT.IN">Death rate, crude (per 1,000 people)</indicator>
             <year>2008</year>
             <value>19.5720000</value>
           </data>
           <data>
             <country id="AFG">Afghanistan</country>
             <indicator id="IC.EXP.DOCS">Documents to export (number)</indicator>
             <year>2005</year>
             <value>7.0000000</value>
           </data>
           <data>
             <country id="AFG">Afghanistan</country>
             <indicator id="IC.EXP.DOCS">Documents to export (number)</indicator>
             <year>2006</year>
             <value>12.0000000</value>
           </data>
           <data>
             <country id="AFG">Afghanistan</country>
             <indicator id="IC.EXP.DOCS">Documents to export (number)</indicator>
             <year>2007</year>
             <value>12.0000000</value>
           </data>
...
</dataset>

The Perl code looks like this:

#Use XML Xlib parser to find elements related to death rate

my $parser = XML::LibXML->new();
my $tree = $parser->parse_file($XML_DB);
my $root = XML::LibXML::XPathContext->new($tree->documentElement());
#print $nodeSet->to_literal(); 

foreach my $node ($root->findnodes("/*/data/indicator[\@id = 'SP.DYN.CDRT.IN']/following-sibling::*")) {
    #print $node->textContent() . "\n";
    #print $node->nodeName . "\n";
    print $node->find("year") . "\n";
}
exit;


The expression year in find("year") does not do work like you think it does because your complicated selector does not end up at the data node. Use Xacobeo to debug XPath expressions. This works:

foreach my $node ($root->findnodes(q{/*/data/indicator[@id = 'SP.DYN.CDRT.IN']/following-sibling::*})) {
    say $_->toString for $node->childNodes;
}

Output:

2006
20.3410000
2007
19.9480000
2008
19.5720000
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜