XPath selecting multiple elements with predicates

2019-02-12 10:22发布

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']

... but this version is not valid 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?

Thanks

Richard

标签: xslt xpath
3条回答
不美不萌又怎样
2楼-- · 2019-02-12 10:49

First, this is valid XPath 2.0:

/header/authors/(surname|given-names)[./text() and @id='1'] 

Second, this XPath 1.0:

/header/authors/*[self::surname|self::given-names][text()][@id='1'] 
查看更多
祖国的老花朵
3楼-- · 2019-02-12 11:02

How about

/header/authors/*[(name() = "surname" or name()="given-names") and ./text() and @id='1'] 
查看更多
神经病院院长
4楼-- · 2019-02-12 11:05

In addition to Alejandro's and Seva's good answers, another alternative is

(/header/authors/surname | /header/authors/given-names)[text() and @id='1']

This is valid in XPath 1.0. It still is a bit redundant, but is more succinct than the long-winded version in your question.

A further alternative would be to declare a variable for surname elements and a variable for given-name elements, and then

($surnames | $given-names)[text() and @id='1']

That would probably be longer in total, but easier to read than the above.

查看更多
登录 后发表回答