What is the precedence among operators in XPath?

假如想象 提交于 2019-12-21 21:14:55

问题


In this XPath expression: //div[@id=”myID”]|p, does the // operator get applied to both sides of the union operator? Or would this expression simply return all div elements in the document that have an id attribute value of myID and all p elements that are children of the context node?

Is there a reference for XPath operator binding and associativity?


回答1:


XPath Operator Order Precedence

The XPath EBNF grammar implies the following precedence among operators (lowest to highest):

Source: XML Path Language (XPath) 2.0 (Second Edition)

(See also: XML Path Language (XPath) 3.0)


Since // and [] are of higher precedence than | (union), your XPath expression

//div[@id=”myID”]|p

says

  • select all div elements in the document with @id attribute value equal to myID,
  • and select all p elements that are children of the context element,
  • and take the union of those two sets of elements

to produce the final result (as you anticipated in your second interpretation).



来源:https://stackoverflow.com/questions/36829449/what-is-the-precedence-among-operators-in-xpath

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