之间有什么区别?

2024-04-02

我应该什么时候使用<copy-of>代替<apply-templates>?

他们的独特作用是什么?大部分时间都在更换<apply-templates> with <copy-of>给出奇怪的输出。这是为什么?


  • xsl:copy-of是匹配的输入 xml 元素的精确副本。不进行 xslt 处理,该元素的输出将与输入完全相同。

  • xsl:apply-templates告诉 xslt 引擎处理与所选元素匹配的模板。xsl:apply-templates正是 xslt 具有压倒一切的能力,因为您使用元素匹配创建的模板可以具有不同的优先级,并且将执行具有最高优先级的模板。

Input:

<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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

之间有什么区别? 的相关文章

随机推荐