ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: Getting text children of an element
Submitter: Paul Prescod (other recipes)
Last Updated: 2001/06/27
Version no: 1.0
Category: XPath Tricks

 

2 stars 1 vote(s)


Approved

Description:

There are a few ways to get the text from within an element.

Source: Text Source

<!-- Collect all of current node's text descendants into a nodeset -->
<xsl:variable name="mytext" select=".//text()"/>

<!-- Output all text contained by current node -->
<xsl:value-of select=".//text()"/>

<!-- Short form for same thing -->
<xsl:value select="."/>

<!-- Collect only my direct children -->
<xsl:variable name="mytext" select="text()"/>

<!-- Output only my direct children -->
<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



Highest rated recipes:

1. Search and Replace

2. Generating a newline

3. Internationalization ...

4. Restricting processing ...

5. Result Pagination with ...

6. Fetching information ...

7. Getting text children of ...

8. Creating empty elements




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.