<copy-of> 和有什么不一样?和<应用模板>? [英] What is the difference between <copy-of> and <apply-templates>?

查看:16
本文介绍了<copy-of> 和有什么不一样?和<应用模板>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我什么时候应该使用 而不是 ?

When should I use <copy-of> instead of <apply-templates>?

他们的独特作用是什么?大多数情况下,用 替换 会给出奇怪的输出.这是为什么?

What is their unique role? Most of the time replacing <apply-templates> with <copy-of> gives out weird output. Why is that?

推荐答案

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

    • xsl:copy-of is an exact copy of the matched input xml element. No xslt processing takes place and the output from that element will be exactly the same as the input.

      xsl:apply-templates 告诉 xslt 引擎处理与所选元素匹配的模板.xsl:apply-templates 是什么赋予了 xslt 覆盖能力,因为您创建的匹配元素的模板可以具有不同的优先级,并且将执行具有最高优先级的模板.

      xsl:apply-templates tells the xslt engine to process templates that match the selected elements. xsl:apply-templates is what gives xslt its overriding capability, since the templates you create with match on elements can have different priorities, and the template with the highest priority will be executed.

      输入:

      <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>
      

      这篇关于&lt;copy-of&gt; 和有什么不一样?和&lt;应用模板&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆