Select text from node and omit child nodes

I need to select text in node, but not child nodes. xml looks like this:

<a> apples <b><c/></b> pears </a> 

If I select a/text() , all I get is apples . How would I step back from the " pear apples ", omitting <b><c/></b>

+6
xquery xpath
source share
3 answers

Well, the a/text() path selects all the text children of the a element, so the path is correct in my view. Only if you use this path, for example. XSLT 1.0 and <xsl:value-of select="a/text()"/> it will display the string value of the first selected node. In XPath 2.0 and XQuery 1.0: string-join(a/text()/normalize-space(), ' ') displays the string apples pears , so maybe this helps with your problem. If not, then consider the context in which you use XPath or XQuery so that a/text() returns only the value (string?) Of the first selected node.

+4
source share

To get all descendants, I suggest using // notation. This will return all descendants of the text below the element. The following is an xquery snippet that gets all the text nodes of the descendants and formats it as indicated by Martin.

 xquery version "1.0"; let $a := <a> apples <b><c/></b> pears </a> return normalize-space(string-join($a//text(), " ")) 

Or, if you have your own formatting requirements, you can start by looping through each text element in the next xquery.

 xquery version "1.0"; let $a := <a> apples <b><c/></b> pears </a> for $txt in $a//text() return $txt 
0
source share

If I select / text (), all I get is apples. How would I track pear apples

Just use :

 normalize-space(/) 

Explanation

The string value of the root node ( / ) of the document is the concatenation of all its descendants text-node. Since text nodes exist only for spaces, we need to eliminate these unwanted text nodes.

Here is a short demonstration of how this solution works and what it produces:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="/"> '<xsl:value-of select="normalize-space()"/>' </xsl:template> </xsl:stylesheet> 

when this conversion is applied to the provided XML document:

 <a> apples <b><c/></b> pears </a> 

the desired, correct result is output:

  'apples pears' 
0
source share

All Articles