tags:

views:

186

answers:

1

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">Death 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;
+2  A: 

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
daxim
Thanks for your great help!
daxim, do you have any sample code that uses Xacobeo?
WTF, Xacobeo is a GUI application - just install and run it. Also, you are supposed to __accept answers__, see http://stackoverflow.com/faq#When%20you%20have%20decided%20which%20answer%20is%20the%20most%20helpful%20to%20you
daxim