这是有效的 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;
}
Is this xpath a valid XPath expression? (It does what it should ).
#!/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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。这是一个有效的 XPath 表达式。
如果你写成这样可能会更简单一些:
Yep. That's a valid XPath expression.
It could be a little simplier if you wrote it as:
是的,您可以在谓词内使用任何表达式,这意味着您可以嵌套它们。
参考文献
谓词 ::= "["表达式“]”
Yes, you can use any expression inside a predicate, which means you can nest them.
References
Predicate ::= "[" Expr "]"