|
|
 |
|
Title: Getting text children of an element
Submitter: Paul Prescod
(other recipes)
Last Updated: 2001/06/27
Version no: 1.0
Category:
XPath Tricks
|
|
1 vote(s)
|
|
|
|
Description:
There are a few ways to get the text from within an element.
Source: Text Source
<xsl:variable name="mytext" select=".//text()"/>
<xsl:value-of select=".//text()"/>
<xsl:value select="."/>
<xsl:variable name="mytext" select="text()"/>
<xsl:value-of select="."/>
The license for this recipe is available here.
Discussion:
|
|
Add comment
|
|
Number of comments: 1
Some corrections, Evan Lenz, 2001/09/19
Above, we read:
<!-- Output all text contained by current node -->
<xsl:value-of select=".//text()"/>
xsl:value-of outputs the string value of only the first node in the node-set, so the above is not quite right. Instead, it should say:
<xsl:copy-of select=".//text()"/>
xsl:copy-of, unlike xsl:value-of, operates on every node in the node-set (not just the first), adding a deep copy of each node to the result tree.
xsl:value-of gets the string value of the (first) node, which is defined as the concatenation of the string values of all descendant text nodes. This means that <xsl:value-of select="."/> is indeed short for <xsl:copy-of select=".//text()"/>.
However, we later read:
<!-- Output only my direct children -->
<xsl:value-of select="."/>
This too is a mistake, in that the string value is comprised of the string values of *all* descendant text nodes, not just the immediate children. Instead, what it should say is:
<!-- Output only my direct children -->
<xsl:copy-of select="text()"/>
Evan Lenz
Add comment
|
|
|
|
|
 |
|