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

前端之家收集整理的这篇文章主要介绍了这是一个有效的XPath表达式吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这个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

猜你在找的XML相关文章