XSL: Select all text in a node, except nodes of a

2019-06-02 04:57发布

How can I output all of the text in a node, including the text in its children nodes while excluding the text in "a" nodes?

标签: xslt
4条回答
啃猪蹄的小仙女
2楼-- · 2019-06-02 05:19

if you're currently processing your node.

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

should return all the textual content

查看更多
贼婆χ
3楼-- · 2019-06-02 05:21
<xsl:for-each select="//*[text() and name() != 'a']">
<xsl:value-of select="."/>
</xsl:for-each>
查看更多
虎瘦雄心在
4楼-- · 2019-06-02 05:28

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.

查看更多
贪生不怕死
5楼-- · 2019-06-02 05:32

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.

查看更多
登录 后发表回答