XPath 使用谓词选择多个元素 [英] XPath selecting multiple elements with predicates
问题描述
我有一个 XPath 查询,它正在执行我想要的操作,即选择姓氏"和带有给定谓词的given-names"(实际上是我需要的,但联合工作正常):
I have an XPath query which is doing what I want, namely selecting a union of 'surname' & 'given-names' with the given predicates (It's actually either/or that I need, but the union works fine):
/header/authors/surname[./text() and @id='1']
|
/header/authors/given-names[./text() and @id='1']
然而,这对我来说似乎过于冗长,我觉得应该可以做一些更简洁的事情,例如:
However this seems overly long-winded to me and I feel that it should be possible to do something more succinct such as:
/header/authors/(surname|given-names)[./text() and @id='1']
...但是这个版本不是有效的 XPath.
... but this version is not valid XPath.
谁能告诉我编写原始 XPath 表达式的更简洁的方法,它不需要将完整路径写两次?
Can anyone tell me of a neater way of writing the original XPath expression which doesn't require the full path to be written twice?
谢谢
理查德
推荐答案
首先,这是有效的 XPath 2.0:
First, this is valid XPath 2.0:
/header/authors/(surname|given-names)[./text() and @id='1']
其次,这个XPath 1.0:
Second, this XPath 1.0:
/header/authors/*[self::surname|self::given-names][text()][@id='1']
这篇关于XPath 使用谓词选择多个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!