Select text from a node and omit child nodes

元气小坏坏 提交于 2019-12-22 05:35:31

问题


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>


回答1:


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.




回答2:


To retrieve all the descendants I advise using the // notation. This will return all text descendants below an element. Below is an xquery snippet that gets all the descendant text nodes and formats it like Martin indicated.

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 could start by looping through each text element in the following xquery.

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



回答3:


If I select a/text(), all i get is "apples". How would i retreive "apples pears"

Just use:

normalize-space(/)

Explanation:

The string value of the root node (/) of the document is the concatenation of all its text-node descendents. Because there are white-space-only text nodes, we need to eliminate these unwanted text nodes.

Here is a small demonstration 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 transformation is applied on the provided XML document:

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

the wanted, correct result is produced:

  'apples pears'


来源:https://stackoverflow.com/questions/5182249/select-text-from-a-node-and-omit-child-nodes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!