xsl:apply-template和xsl:call-template的区别

简单说apply是应用,call是调用。  
  用apply时,引擎自动搜索与当前select指定xpath的匹配节点相匹配的template(该template必须有属性match)并使用该template进行处理,此时需要指定的是select的path。  
  用call时就跟其它语言调用函数一样,必须指定name属性,相应的,该template必须有name属性,当然,也可以在这时with-param(当然相应的模板中有对应的param才行,不过这个不强制要求)。这里是两个例子:  
  <?xml   version="1.0"   encoding="UTF-8"?>  
  <xsl:stylesheet   version="1.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  
  <xsl:template   match="/">  
  <xsl:apply-template   select="."/>  
  <xsl:call-template   name="Name">  
  <xsl:with-parm   name="param1"   select="."/>  
  </xsl:call-template>  
  </xsl:template>  
  <!--使用apply调用的模板,   其中的上下文节点是与之匹配的节点-->  
  <xsl:template   match="Name">  
  <xsl:value-of   select="."/>  
  </xsl:template>  
  <!--使用call调用的模板,   它没有上下文节点,参数通过param进行传递-->  
  <xsl:template   name="Name">  
  <xsl:with-param   name="param1"/>  
  <!--这个参数有缺省值,如果调用时没有传入值,则使用默认值-->  
  <xsl:param   name="param2"   select="'Hello'"/>  
  <xsl:value-of   select="$param2"/>  
  <xsl:value-of   select="$param1"/>  
  </xsl:template>  
  </xsl:stylesheet>  
posted on   blue  阅读(1930)  评论(0编辑  收藏  举报
努力加载评论中...

点击右上角即可分享
微信分享提示