I need to select the text in a node, but not any child nodes. the xml looks like this
<a>
apples
<b><c/></b>
pears
</a>
If I select a/text()
, all I get is "apples". How would I retreive "apples pears" while omitting <b><c/></b>
Child nodes can be removed from a parent with removeChild(), and a node itself can be removed with remove(). Another method to remove all child of a node is to set it's innerHTML=”” property, it is an empty string which produces the same output. This method is not preferred to use. Example-1: Using “removeChild()”.
Text nodes cannot have child nodes because they represent content, not structure. Text nodes must be contained by element, attribute, document fragment, or entity reference nodes—they cannot be contained by the top-level document node, though the DOMDocument object is used to create text nodes.
The childNodes property is a property of Node in Javascript and is used to return a Nodelist of child nodes. Nodelist items are objects, not strings and they can be accessed using index numbers. The first childNode starts at index 0. Syntax.
Well the path a/text()
selects all text child nodes of the a
element so the path is correct in my view. Only if you use that path with e.g. XSLT 1.0 and <xsl:value-of select="a/text()"/>
it will output the string value of the first selected node. In XPath 2.0 and XQuery 1.0: string-join(a/text()/normalize-space(), ' ')
yields the string apples pears
so maybe that helps for your problem. If not then consider to explain in which context you use XPath or XQuery so that a/text()
only returns the (string?) value of the first selected node.
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