XPath: select text node

XmlXpathSelection

Xml Problem Overview


Having the following XML:

<node>Text1<subnode/>text2</node>

How do I select either the first or the second text node via XPath?

Something like this:

/node/text()[2]

of course doesn't work because it's the merged result of every text inside the node.

Xml Solutions


Solution 1 - Xml

> Having the following XML: > > Text1text2 > > 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]

Solution 2 - Xml

your xpath should work . i have tested your xpath and mine in both MarkLogic and Zorba Xquery/ Xpath implementation.

Both should work.

/node/child::text()[1] - should return Text1
/node/child::text()[2] - should return text2


/node/text()[1] - should return Text1
/node/text()[2] - should return text2

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkernelView Question on Stackoverflow
Solution 1 - XmlDimitre NovatchevView Answer on Stackoverflow
Solution 2 - XmlkadalamittaiView Answer on Stackoverflow