tags:

views:

661

answers:

4

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!

+1  A: 
<xsl:for-each select="//*[text() and name() != 'a']">
<xsl:value-of select="."/>
</xsl:for-each>
PQW
Thanks! Can I exclude <a> tags?
joe
It doesn't skip <a> tags
Izabela
This selects every node that contains text that is not an anchor tag. He wanted to select the TEXT of all nodes that weren't in A tags.
fearphage
A: 

if you're currently processing your node.

<xsl:value-of select="."/>

should return all the textual content

Pierre
Thanks! Can I exclude <a> tags?
joe
+2  A: 

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"&gt;

  <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.

Evan Lenz
A: 

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.

fearphage