学习XSLT的知识积累

xsl:apply-templates

  <xsl:apply-templates> 元素用在模版内告诉XSL处理器把所提供的节点集合匹配其他模版,其语法如下所示:

<xsl:apply-templates
select="expression"
mode
="mode">
</xsl:apply-templates>

  在节点触发某个模版的情况下, XSLT通常会假定这个模版会专注该节点的所有内容而不去处理它们。模版内的<xsl:apply-templates> 元素则告诉XSLT处理器处理节点内容,在过程中执行任何有关的模版。

  <xsl:apply-templates>默认地处理所有的最近的子节点。选择属性可以让你指定特定的派生节点进行处理。它会让Xpath表达式管理当前模版的上下文环境,模式属性则只让具有指定模式的模版被执行。

  比如,假如你对/book 和 /book/chapter都建立了模版,你打算用/book模版中的<xsl:apply-templates>来激活/book/chapter :

<xsl:template match="/book">
This book is entitled "
<xsl:value-of select="title"/>"
<xsl:apply-templates/>
</xsl:template>


<xsl:template match="/book/chapter">
This is chapter 
<xsl:number/>, entitled "<xsl:value-of select="title"/>"
</xsl:template>


<xsl:param> 元素
--------------------------------------------------------------------------------
定义与用法
<xsl:param>元素被用来声明一个局部或者全局的参数。
说明:如果声明的是一个高级(top-level)的元素那么参数是全局的,如果在一个样规中声
明那么是局部的。
如:声明一个参数
1.默认值为空
<xsl:param name = "A" />
2.在声明过程中赋值
 <xsl:param name = "B" >111</xsl:param>
参数就像我们声明变量一样.
下面是参数赋值的用法
<xsl:with-param name = "A" >11</xsl:with-param
<xsl:with-param name = "B" >33</xsl:with-param>
或者
<xsl:with-param name = "A"  select="11" />
<xsl:with-param name = "B"  select="33"  />

例子:xslt文件
 <xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0" >
          <xsl:output method = "text" />

          <xsl:template match = "/" >
               <xsl:call-template name = "print" >
                    <xsl:with-param name = "A" >11</xsl:with-param>
                    <xsl:with-param name = "B" >33</xsl:with-param>
               </xsl:call-template>
               <xsl:call-template name = "print" >
                    <xsl:with-param name = "A" >55</xsl:with-param>
               </xsl:call-template>
          </xsl:template>

          <xsl:template name = "print" >
               <xsl:param name = "A" />
               <xsl:param name = "B" >111</xsl:param>
               <xsl:text >
      
      </xsl:text>
               <xsl:value-of select = "$A" />
               <xsl:text > + </xsl:text>
               <xsl:value-of select = "$B" />
               <xsl:text > = </xsl:text>
               <xsl:value-of select = "$A+$B" />
          </xsl:template>
     </xsl:stylesheet

    xml文件
<?xml version="1.0" encoding="UTF-8"?>
<AAA>
 <BBB>bbb </BBB>
 <CCC>ccc </CCC>
</AAA>
    输出结果
11 + 33 = 44

 55 + 111 = 166

<xsl:variable> 元素
--------------------------------------------------------------------------------
定义与用法
<xsl:variable>元素被用来声明一个局部或者全局的变量。
说明:当声明为高级(top-level)元素时,变量是全局的,当在一个template 中声明时,作
为局部变量。
说明:一旦您设置了一个变量的值,您就不能修改这个值!
提示:您可以通过增加<xsl:variable>的内容或者用select 属性来设置变量值!
原文地址:https://www.cnblogs.com/wuhuihui_dotnet/p/445466.html