XPath 选择多个标签 [英] XPath to select multiple tags
本文介绍了XPath 选择多个标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
鉴于这种简化的数据格式:
Given this simplified data format:
<a>
<b>
<c>C1</c>
<d>D1</d>
<e>E1</e>
<f>don't select this one</f>
</b>
<b>
<c>C2</c>
<d>D2</d>
<e>E1</e>
<g>don't select me</g>
</b>
<c>not this one</c>
<d>nor this one</d>
<e>definitely not this one</e>
</a>
您将如何选择作为 B
子代的所有 C
、D
和 E
元素?
How would you select all the C
s, D
s and E
s that are children of B
elements?
基本上,类似于:
a/b/(c|d|e)
在我自己的情况下,不仅仅是 a/b/
,查询导致选择那些 C
、D
、E
节点实际上非常复杂,所以我想避免这样做:
In my own situation, instead of just a/b/
, the query leading up to selecting those C
, D
, E
nodes is actually quite complex so I'd like to avoid doing this:
a/b/c|a/b/d|a/b/e
这可能吗?
推荐答案
一个正确答案是:
/a/b/*[self::c or self::d or self::e]
请注意这一点
a/b/*[local-name()='c' or local-name()='d' or local-name()='e']
既太长又不正确.此 XPath 表达式将选择如下节点:
is both too-long and incorrect. This XPath expression will select nodes like:
OhMy:c
NotWanted:d
QuiteDifferent:e
这篇关于XPath 选择多个标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文