XSL: select all text in a node except for certain node types

How can I output all the text in a node, including the text in my child nodes, excluding the text in the "a" nodes?

+1


a source to share


4 answers


Use the built-in template rule for text nodes to copy them into the result. Even for the new processing mode that you specify ("all-but-a" in the code below), the built-in rules will work: for elements (recursively) handle children; for text nodes, copy. You only need to override one of them, the element rule <a>

, hence this is an empty template rule that effectively removes 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 built-in template rules work, see the Built-in Rule Templates section of How XSLT Works on my website.

+8


a source


if you are currently processing your node.

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

      



should return all text content

+3


a source


<xsl:for-each select="//*[text() and name() != 'a']">
<xsl:value-of select="."/>
</xsl:for-each>

      

+1


a source


I believe this is what you are 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 the anchored tags.

+1


a source







All Articles