Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select text from a node and omit child nodes

Tags:

xpath

xquery

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>

like image 490
Dave Avatar asked Mar 03 '11 14:03

Dave


People also ask

How to remove child node?

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()”.

Can a text node have child nodes?

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.

What is childNode?

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.


1 Answers

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.

like image 198
Martin Honnen Avatar answered Sep 27 '22 22:09

Martin Honnen