XPath: Default to 'Node A', select 'Node B' instead if 'Node B' is not empty

隐身守侯 提交于 2019-12-04 00:42:44

This XPath expression returns NodeB if it exists (and has text content) and NodeA in the other case:

//my:NodeB[text()] | //my:NodeA[text() and not(//my:NodeB[text()])]

If you want to get all sub-elements you can append /* after the selected node, like this

//my:NodeB[text()]/* | //my:NodeA[text() and not(//my:NodeB[text()])]/*

A correct XPath expression is:

(//my:NodeB[node()] | //my:NodeA[not(//my:NodeB/node())])/node()

As the conditions in the predicates are mutually exclusive, only one of them can be true() and this guarantees that only one of the two nodes is selected by the expression within the brackets.

So, the expression above selects any node that is a child of: my:NodeB if it has children, or my:NodeA -- otherwize.

Here we assume as given that at most one element named my:NodeA and at most one element named my:NodeB exist in the XML document.

Another assumption is that the namespace to which the prefix my is bound has been "registered" with the XPath expression evaluator (the specific XPath implementation you are using).

Do note that in the provided XML document neither of the elements my:NodeA and my:NodeB has any element children (they both have just a text node child) -- so I assume that by "element" you actually mean "node".

If it is safe to rely on the fact that any NodeA's will come before NodeB in document order (as implied by your sample), then a simpler and much more efficient XPATH expression to select the required element is...

(//my:NodeA[text()]|//my:NodeB)[1]

The above selects the element. If you want to select the text node of the element, then use instead...

(//my:NodeA[text()]|//my:NodeB)[1]/text()

If there is no positional relationship between NodeA and NodeB (they can come in any relative order), and you are using XPATH 2.0, then the following expression will select the required text node..

(//my:NodeA[text()],//my:NodeB)[1]/text()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!