何时应该使用<copy-of>
而不是<apply-templates>
?XSLT:<copy-of>和<apply-templates>有什么区别?
他们独特的作用是什么?大多数时候用<copy-of>
代替<apply-templates>
会给出奇怪的输出。这是为什么?
何时应该使用<copy-of>
而不是<apply-templates>
?XSLT:<copy-of>和<apply-templates>有什么区别?
他们独特的作用是什么?大多数时候用<copy-of>
代替<apply-templates>
会给出奇怪的输出。这是为什么?
xsl:copy-of
是匹配的输入XML元素的精确副本。不会发生xslt处理,并且该元素的输出将与输入完全相同。
xsl:apply-templates
告诉XSLT引擎来处理匹配所选元素的模板。 xsl:apply-templates
是xslt的首要功能,因为您使用匹配元素创建的模板可以具有不同的优先级,并且具有最高优先级的模板将被执行。
输入:
<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>
copy-of
将简单地返回你的XML转储中提供的节点集合,另一方面
apply-templates
将适用于适用于节点集合传递任何模板。