加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

这是一个有效的XPath表达式吗?

发布时间:2020-12-16 23:22:45 所属栏目:百科 来源:网络整理
导读:这个xpath是一个有效的XPath表达式吗? (它做它应该做的). #!/usr/bin/env perluse strict; use warnings; use 5.012;use XML::LibXML;my $string =EOS;result cd artists artist class="1"Pumkinsingers/artist artist class="2"Max and Moritz/artist /art
这个xpath是一个有效的XPath表达式吗? (它做它应该做的).

#!/usr/bin/env perl
use strict; use warnings; use 5.012;
use XML::LibXML;

my $string =<<EOS;
<result>
    <cd>
    <artists>
        <artist class="1">Pumkinsingers</artist>
        <artist class="2">Max and Moritz</artist>
    </artists>
    <title>Hello,Hello</title>
    </cd>
    <cd>
    <artists>
        <artist class="3">Green Trees</artist>
        <artist class="4">The Leons</artist>
    </artists>
    <title>The Shield</title>
    </cd>
</result>
EOS
#/
my $parser = XML::LibXML->new();
my $doc = $parser->load_xml( string => $string );
my $root = $doc->documentElement;

my $xpath = '/result/cd[artists[artist[@class="2"]]]/title';

my @nodes = $root->findnodes( $xpath );
for my $node ( @nodes ) {
    say $node->textContent;
}

解决方法

是的.这是一个有效的XPath表达式.

如果你把它写成:它可能会有点简单:

/result/cd[artists/artist[@class="2"]]/title

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读