How can I output all of the text in a node, including the text in its children nodes. But I want to exclude the text in "a" nodes.
Thanks!
How can I output all of the text in a node, including the text in its children nodes. But I want to exclude the text in "a" nodes.
Thanks!
<xsl:for-each select="//*[text() and name() != 'a']">
<xsl:value-of select="."/>
</xsl:for-each>
if you're currently processing your node.
<xsl:value-of select="."/>
should return all the textual content
Make use of the built-in template rule for text nodes, which is to copy them to the result. Even for a new processing mode that you specify ("all-but-a" in the code below), the built-in rules will work: for elements, (recursively) process children; for text nodes, copy. You only need to override one of them, the rule for <a>
elements, hence the empty template rule, which effectively strips out the text.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="myNode">
<!-- Process children -->
<xsl:apply-templates mode="all-but-a"/>
</xsl:template>
<!-- Don't process <a> elements -->
<xsl:template mode="all-but-a" match="a"/>
</xsl:stylesheet>
For a complete description of how the built-in template rules work, check out the "Built-in Template Rules" section of "How XSLT Works" on my website.
I believe this is what you're looking for:
<xsl:for-each select="//text()[not(ancestor::a)]">
<xsl:value-of select="."/>
</xsl:for-each>
It selects all text nodes that are not children of anchor tags.