Having the following XML:
<node>Text1<subnode/>text2</node>
How do I select either the first or
the second text node via XPath?
Use:
/node/text()
This selects all text-node children of the top element (named "node") of the XML document.
/node/text()[1]
This selects the first text-node child of the top element (named "node") of the XML document.
/node/text()[2]
This selects the second text-node child of the top element (named "node") of the XML document.
/node/text()[someInteger]
This selects the someInteger-th text-node child of the top element (named "node") of the XML document. It is equivalent to the following XPath expression:
/node/text()[position() = someInteger]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…