【问题标题】:PERL XPath Parser HelpPERL XPath 解析器帮助
【发布时间】:2010-05-12 03:10:05
【问题描述】:

我想使用 XML::XPath 解析器从 Worldbank 站点的 XML DB 文件中提取数据。问题是我在输出中没有看到任何结果。我一定在代码中遗漏了一些东西。理想情况下,我只想从每个国家 XML DB 中提取死亡率统计数据(年份和价值)。我将其用作输入的一部分:

http://data.worldbank.org/sites/default/files/countries/en/afghanistan_en.xml

use strict;
use LWP 5.64;
use HTML::ContentExtractor;
use XML::XPath;

my $agent1 = LWP::UserAgent->new;
my $extractor = HTML::ContentExtractor->new();

#Retrieve main Worldbank country site
my $mainlink = "http://data.worldbank.org/country/";
my $page = $agent1->get("$mainlink");
my $fulltext = $page->decoded_content();

#Match to just all available countries in Worldbank
my $country = "";
my @countryList;
if (@countryList = $fulltext =~ m/(http:\/\/data\.worldbank\.org\/country\/.*?")/gi){
    foreach $country(@countryList){
        #Remove " at the end of link
        $country=~s/\"//gi;
        print "\n" . $country;

        #Retrieve each country profile's XML DB file
        my $page = $agent1->get("$country");
        my $fulltext = $page->decoded_content();
        my $XML_DB = "";
        my @countryXMLDBList;

        if (@countryXMLDBList = $fulltext =~ m/(http:\/\/data\.worldbank\.org\/sites\/default\/files\/countries\/en\/.*?\.xml)/gi){
            foreach $XML_DB(@countryXMLDBList){

                my $page = $agent1->get("$XML_DB");
                my $fulltext = $page->decoded_content();
                #print $fulltext; 
                #Use XML XPath parser to find elements related to death rate
                my $xp = XML::XPath->new($fulltext); #my $xp = XML::XPath->new("afghanistan_en.xml"); 
                my $nodeSet = $xp->find("//*");
                if (!$nodeSet->isa('XML::XPath::NodeSet') || $nodeSet->size() == 0) {
                    #No match found
                    print "\nMatch not found!";
                    exit;
                } else {
                    foreach my $node ($nodeSet->get_nodelist){
                        print "\n" . $node->find('country')->string_value;
                        print "\n" . $node->find('indicator')->string_value;
                        print "\n" . $node->find('year')->string_value;
                        print "\n" . $node->find('value')->string_value;
                        exit;
                    }
                }
            }
            #Build line graph based on death rate statistics and output some image file format
        }
    }
}

我也在研究使用 xpath 表达式“following-sibling”,但不确定如何正确使用它。例如,我有以下一组 XML 数据,我只对在死亡率数据指标之后直接拉同胞感兴趣。

<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>

任何帮助将不胜感激!!!

【问题讨论】:

  • 您需要指定您的问题。你有什么问题?请编辑您的问题以包含它。
  • 对此感到抱歉...现在我已经更详细地概述了这个问题!
  • 您可能不想使用 XML::XPath - 模块陈旧、缓慢且不再积极维护。我建议您切换到 XML::LibXML。 API 几乎相同,但速度更快,支持更好。
  • 尝试将my $nodeSet = $xp-&gt;find("//*"); 替换为:my $nodeSet = $xp-&gt;find("/*/data"); my $nodeSet = $xp-&gt;find("/*");

标签: xml perl xpath


【解决方案1】:

我不明白问题的第一部分——它说

我没有看到任何结果 输出。我一定是错过了什么 代码。

不过,这根本不是问题。尤其是没有提供输入数据,也没有“结果”的定义时。

第二部分

我也在考虑使用 xpath 表达“following-sibling”,但是 不知道如何正确使用它。为了 例如,我有以下一组 我只感兴趣的 XML 数据 后直接拉兄弟姐妹 只是死亡率数据的指标。

使用以下 XPath 表达式(假设 data 元素是 XML 文档顶部元素的子元素:

/*/data/indicator[@id = 'SP.DYN.CDRT.IN']/following-sibling::*

【讨论】:

  • 谢谢!我会试试这个!顺便说一句,我已经为您更新了问题的第一部分。
猜你喜欢
  • 2023-03-26
  • 2020-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-11
  • 2011-03-24
相关资源
最近更新 更多