XSL:选择节点中的所有文本,特定类型的节点除外 [英] XSL: Select all text in a node, except nodes of a certain type
问题描述
如何输出节点中的所有文本,包括其子节点中的文本,同时排除a"节点中的文本?
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?
推荐答案
利用文本节点的内置模板规则,将它们复制到结果中.即使对于您指定的新处理模式(下面代码中的all-but-a"),内置规则也将起作用:对于元素,(递归地)处理子元素;对于文本节点,复制.您只需要覆盖其中之一,即 <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">
<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>
有关内置模板规则如何工作的完整说明,请查看 "XSLT 的工作原理" 在我的网站上.
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.
这篇关于XSL:选择节点中的所有文本,特定类型的节点除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!