【发布时间】:2011-10-28 09:11:22
【问题描述】:
据我了解,通常在 XSL 中,每个节点可能只受一个模板的影响。一旦一个节点受到模板的影响,它 - 并且至关重要的是,它的子/后代 - 不会受到任何其他模板的进一步影响。
但有时,您希望使用一个模板影响外部节点,然后使用另一个模板影响其子/后代。以下是解决此问题的可取方法吗?其目的是为每个节点添加属性“attr”。
源 XML: 你好! 孙子>
XSL:
<xsl:template match='node()'>
<xsl:copy>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match='child'>
<child>
<xsl:attribute name='attr'>val</xsl:attribute>
<xsl:apply-templates select='./*' />
</child>
</xsl:template>
<xsl:template match='greatgrandchild'>
<greatgrandchild>
<xsl:attribute name='attr'>val</xsl:attribute>
<xsl:value-of select='.' />
</greatgrandchild>
</xsl:template>
</xsl:stylesheet>
我在正确的路线上吗?
提前致谢。
【问题讨论】: