Reputation: 37
This might sound like a pretty simple question but I wasn't able to find answers anywhere. What's the point of the parentheses when using the text() function in XPATHs ?
Why can't I simply use //div[text='myText'] instead of //div[text()='myText'] ?
Is it possible to pass some kind of parameters to the function somehow ?
Thanks
Upvotes: 2
Views: 306
Reputation: 19979
https://www.w3.org/TR/1999/REC-xpath-19991116/#dt-string-value
Browsers support only xpath 1.0.
So as per this documentation, there are 7 node types:
The root node comes from the document information item. The children of the root node come from the children and children - comments properties.
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.
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.
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.
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.
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.
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.
in version 1 all this nodes expect attrubutes node are accessed as nodetype()=value
eg comment()=value, text()=value etc.
For attribute node type the value is accessed as @attribute=value
In Xpath 3.0 even attrubute node is changed to attribute() , but as browsers have stopped supporting xpath and is concentrating on css , all browsers supports only 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.
Upvotes: 0
Reputation: 33658
text
without parentheses is a name test matching text
elements. For example, an element like
<div>
<text>myText</text>
</div>
would be matched by //div[text='myText']
. So the parens are needed for disambiguation. text()
is a node type test matching text nodes. It is not a function. For example, you can also use it with an axis like descendant::text()
.
Upvotes: 3