I'm having trouble understanding the difference between text()
and node()
. From what I understand, text()
would be whatever is in between the tags <item>apple</item>
which is apple in this case. Node would be whatever that node actually is, which would be item
But then I've been assigned some work where it asks me to "Select the text of all items under produce" and a separate question asks "Select all the manager nodes in all departments"
How is the output suppose to look text()
as opposed to node()
Snippet of XML:
<produce> <item>apple</item> <item>banana</item> <item>pepper</item> </produce> <department> <phone>123-456-7891</phone> <manager>John</manager> </department>
Of course, there are more departments and more managers, but this was just a snippet of code.
Any help would be much appreciated!
XPath text() function is a built-in function of the Selenium web driver that locates items based on their text. It aids in the identification of certain text elements as well as the location of those components within a set of text nodes. The elements that need to be found should be in string format.
Xpath Node is defined as a point where the path address initiates, as it follows a concept of nodes. In simple terms they are the individual elements of the Xpath hierarchical structure which are termed as a node and enable an XSL processing. Xpath expressions could be done with HTML and XML.
enter image description here The XPath text() function locates elements within a text node while dot (.) locate elements inside or outside a text node.
Do note that /html/text() doesn't select all text nodes in the document -- only the text nodes that are children (not descendents) of the top, html element. You probably want /html//text() . Some knowledge and understanding of XPath is typically required in order to construct XPath expressions.
text()
and node()
are node tests, in XPath terminology (compare).
Node tests operate on a set (on an axis, to be exact) of nodes and return the ones that are of a certain type. When no axis is mentioned, the child
axis is assumed by default.
There are all kinds of node tests:
node()
matches any node (the least specific node test of them all)text()
matches text nodes onlycomment()
matches comment nodes*
matches any element nodefoo
matches any element node named "foo"
processing-instruction()
matches PI nodes (they look like <?name value?>
).*
also matches attribute nodes, but only along the attribute
axis. @*
is a shorthand for attribute::*
. Attributes are not part of the child
axis, that's why a normal *
does not select them.This XML document:
<produce> <item>apple</item> <item>banana</item> <item>pepper</item> </produce>
represents the following DOM (simplified):
root node element node (name="produce") text node (value="\n ") element node (name="item") text node (value="apple") text node (value="\n ") element node (name="item") text node (value="banana") text node (value="\n ") element node (name="item") text node (value="pepper") text node (value="\n")
So with XPath:
/
selects the root node/produce
selects a child element of the root node if it has the name "produce"
(This is called the document element; it represents the document itself. Document element and root node are often confused, but they are not the same thing.) /produce/node()
selects any type of child node beneath /produce/
(i.e. all 7 children)/produce/text()
selects the 4 (!) whitespace-only text nodes/produce/item[1]
selects the first child element named "item"
/produce/item[1]/text()
selects all child text nodes (there's only one - "apple" - in this case)And so on.
So, your questions
/produce/item/text()
(3 nodes selected)//department/manager
(1 node selected)Notes
child
axis. You can change the axis by prefixing a different axis name. For example: //item/ancestor::produce
/produce/item[1]/text()
and string(/produce/item[1])
will be the same.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With