假设我有一个如下所示的 XML 文件:
<books>
<book>
<title>John is alive</title>
<abstract>
A man is found alive after having disappeared for 10 years.
</abstract>
<description>
<en> John disappeared 10 years ago. Lorem ipsum dolor sit amet ...</en>
<fr> Il y a 10 ans, John disparaissait. Lorem ipsum dolor sit amet ...</fr>
</description>
<notes>First book in the series, where the character is introduced</notes>
</book>
<book>
<title>The disappearance of John</title>
<abstract>
A prequel to the book "John is alive".
</abstract>
<description>
<en> He lead an ordinary life, but then ... lorem ipsum dolor sit amet ...</en>
<fr> Sa vie était tout à fait ordinaire, mais ... lorem ipsum dolor sit amet ...</fr>
</description>
<notes>Second book in the "John" series, but first in chronological order</notes>
</book>
</books>
我的问题很简单:我如何使用 XPATH 获取包含该单词的所有节点的集合John
?
显然,我可以指定一系列节点并且效果很好:
(//title | //abstract | //description/* | //notes)[contains(lower-case(text()),"john")]
但是,如果我的 XML 增长(而且它会增长!),并且在结构的各个级别添加新元素,我不想经常返回并调整我的 XPATH。
我不明白的是为什么像这样的通用声明
//*[contains(lower-case(text()),"john")]
失败并显示此错误消息Required cardinality of first argument of lower-case() is one or zero
.
然而,并非所有带星号的语句都会失败。
例如:
//books/book/*[contains(lower-case(text()),"john")]
失败并显示上述错误消息
while
//books/book/*/*[contains(lower-case(text()),"john")]
成功并检索到<en>
and <fr>
从第一个节点开始<description>
element
如果不可能,好吧,我将列出 XPATH 中的所有元素,但我仍然想清楚地了解*
a 上下文中的选择器contains()
手术。