在XPath中,运算符之间的优先级是什么? [英] What is the precedence among operators in XPath?

查看:149
本文介绍了在XPath中,运算符之间的优先级是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下XPath表达式://div[@id=myID]|p中,//运算符是否同时应用于联合运算符的两端?还是该表达式会简单地返回文档中所有具有id属性值myIDdiv元素以及所有作为上下文节点子元素的p元素?

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?

是否存在有关XPath运算符绑定和关联性的参考?

Is there a reference for XPath operator binding and associativity?

推荐答案

XPath运算符顺序优先级

XPath EBNF语法暗示了运算符之间的优先级(从最低到最高):

XPath Operator Order Precedence

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

来源: XML路径语言(XPath)2.0(第二版)

(另请参见: XML路径语言(XPath)3.0 )

由于//[]的优先级高于|(联合),因此您的XPath表达式

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

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

  • 使用@id属性值选择文档中的所有div元素 等于myID
  • 并选择所有上下文元素的子元素p
  • 并采用这两组元素的联合
  • 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).

这篇关于在XPath中,运算符之间的优先级是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆