XPATH - 为什么 text() 函数中的括号

XPATH - Why the parentheses in text() function

这听起来像是一个非常简单的问题,但我无法在任何地方找到答案。 在 XPATH 中使用 text() 函数时括号有什么意义?

为什么我不能简单地使用 //div[text='myText'] 而不是 //div[文本()='myText'] ?

是否可以通过某种方式将某种参数传递给函数?

谢谢

text 不带括号的是名称测试匹配 text 元素。例如,像

这样的元素
<div>
    <text>myText</text>
</div>

将与 //div[text='myText'] 匹配。所以需要parens来消除歧义。 text()是一个节点类型测试匹配文本节点。它不是一个函数。例如,您也可以将它与 descendant::text().

之类的轴一起使用

https://www.w3.org/TR/1999/REC-xpath-19991116/#dt-string-value

浏览器仅支持 xpath 1.0。

根据本文档,有 7 种节点类型:

  1. The root node comes from the document information item. The children of the root node come from the children and children - comments properties.

  2. An element node comes from an element information item. The children of an element node come from the children and children - comments properties. The attributes of an element node come from the attributes property. The namespaces of an element node come from the in-scope namespaces property. The local part of the expanded-name of the element node comes from the local name property. The namespace URI of the expanded-name of the element node comes from the namespace URI property. The unique ID of the element node comes from the children property of the attribute information item in the attributes property that has an attribute type property equal to ID.

  3. An attribute node comes from an attribute information item. The local part of the expanded-name of the attribute node comes from the local name property. The namespace URI of the expanded-name of the attribute node comes from the namespace URI property. The string-value of the node comes from concatenating the character code property of each member of the children property.

  4. A text node comes from a sequence of one or more consecutive character information items. The string-value of the node comes from concatenating the character code property of each of the character information items.

  5. A processing instruction node comes from a processing instruction information item. The local part of the expanded-name of the node comes from the target property. (The namespace URI part of the expanded-name of the node is null.) The string-value of the node comes from the content property. There are no processing instruction nodes for processing instruction items that are children of document type declaration information item.

  6. A comment node comes from a comment information item. The string-value of the node comes from the content property. There are no comment nodes for comment information items that are children of document type declaration information item.

  7. A namespace node comes from a namespace declaration information item. The local part of the expanded-name of the node comes from the prefix property. (The namespace URI part of the expanded-name of the node is null.) The string-value of the node comes from the namespace URI property.

在版本 1 中,所有这些节点都期望属性节点被访问为 nodetype()=value

例如 comment()=value、text()=value 等

对于属性节点类型,值被访问为@attribute=value

在 Xpath 3.0 中连 attrubute 节点都更改为 attribute() ,但由于浏览器已停止支持 xpath 并专注于 css ,所有浏览器仅支持 xpath 1.0.

https://www.w3.org/TR/2017/REC-xpath-31-20170321/#node-tests

node() matches any node.

text() matches any text node.

comment() matches any comment node.

namespace-node() matches any namespace node.

element() matches any element node.

schema-element(person) matches any element node whose name is person (or is in the substitution group headed by person), and whose type annotation is the same as (or is derived from) the declared type of the person element in the in-scope element declarations.

element(person) matches any element node whose name is person, regardless of its type annotation.

element(person, surgeon) matches any non-nilled element node whose name is person, and whose type annotation is surgeon or is derived from surgeon.

element(*, surgeon) matches any non-nilled element node whose type annotation is surgeon (or is derived from surgeon), regardless of its name.

attribute() matches any attribute node.

attribute(price) matches any attribute whose name is price, regardless of its type annotation.

attribute(*, xs:decimal) matches any attribute whose type annotation is xs:decimal (or is derived from xs:decimal), regardless of its name.

document-node() matches any document node.

document-node(element(book)) matches any document node whose content consists of a single element node that satisfies the kind test element(book), interleaved with zero or more comments and processing instructions.