什么时候应该使用<copy-of>而不是<apply-templates>
他们的独特作用是什么?大多数情况下,用<copy-of>替换<apply-templates>会产生奇怪的输出。为什么会这样呢?
发布于 2009-12-07 18:45:47
xsl:copy-of是匹配的input xml元素的精确副本。不会进行任何xslt处理,该元素的输出将与input.xsl:apply-templates告诉xslt引擎处理与所选元素匹配的模板时完全相同。xsl:apply-templates赋予了xslt覆盖能力,因为您使用match on elements创建的模板可以具有不同的优先级,具有最高优先级的模板将被执行。输入:
<a>
<b>asdf</b>
<b title="asdf">asdf</b>
</a>Xslt 1:
<xsl:stylesheet ... >
<xsl:template match="a">
<xsl:copy-of select="b" />
</xsl:template>
</xsl:stylesheet>Xml输出1:
<b>asdf</b>
<b title="asdf">asdf</b>Xslt 2:
<xsl:stylesheet ... >
<xsl:template match="a">
<xsl:apply-templates select="b" />
</xsl:template>
<xsl:template match="b" priority="0">
<b><xsl:value-of select="." /></b>
<c><xsl:value-of select="." /></c>
</xsl:template>
<xsl:template match="b[@title='asdf']" priority="1">
<b title="{@title}"><xsl:value-of select="@title" /></b>
</xsl:template>
</xsl:stylesheet>Xml输出2:
<b>asdf</b>
<c>asdf</c>
<b title="asdf">asdf</b>发布于 2009-12-07 18:36:46
copy-of 将只返回所提供的node-set中的XML转储
apply-templates另一方面,将应用任何适用于传递给它的节点集的模板。
https://stackoverflow.com/questions/1859166
复制相似问题