xml--call-template

来源:互联网 发布:js类库 编辑:程序博客网 时间:2024/06/04 18:25
xsl:variablexsl:param
<xsl:call-template name="footer">
<xsl:with-param name="date" select="@lastupdate"/>
</xsl:call-template>
<xsl:template name="footer">
<xsl:param name="date">today</xsl:param>
<hr/>
<xsl:text>Last update: </xsl:text>
<xsl:value-of select="$date"/>
</xsl:template>
对xml模板 来说,name属性是很关键的 call-template /apply-template 的name必须要和模板的name
相对应。模板相当于一个函数,可以暂时这么看。而name相当于函数名称把。
在call-template中 使用xsl:with-param 相当于函数参数输入
而参数声明相当就是在xsl:template的 xsl:param
说到xsl:variable
可以用<xsl:variable name="ShowDepth"><计算的值></xsl:variable>来声明
相当于c中的  const 因为变量一旦声明就无法再被改变。
对于xsl:param和xsl:variable 都可以用 name 来直接选择比如
<xsl:value-of select="$date"/>  就是选择date变量或者参数
变量和参数,都是有声明范围的 这点和语言中的道理一样。
最后最最重要一点 :xslvariable是常量不能再改变
不要被它的名称迷惑、

简单说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>  

------------------------------------------------------------------------

PS:<xsl:template>就像类似于编程语言中定义的子程序,而<xsl:apply-templates>好比去调用这个子程序。如果我们把XML中的节点都定义成模版(template),一旦xml的结构或者节点发生改变,那么我们只要对改变了节点的模版进行修改就可以了,而如果用<xsl:for-each>的话很有可能对包在<xsl:for-each>中的代码有比较大的改动,还有就是如果xml文件中的节点不是很有规律的话,用<xsl:for-each>显然就不如用<xsl:template>了。

<?xml version="1.0" encoding="gb2312"?>
<NewDataSet >
<Table >
<Code >ABC </Code >
<Desc >Good </Desc >
</Table >

<Table >
<Code >BCD </Code >
<Desc >New </Desc >
</Table >

<Table1 >
<cClass >Standard </cClass >
<Code >ABC </Code >
<Date >2004-08-06 </Date >
<Time >12:50:33-04:00 </Time >
<Agent >Ray </Agent >
<Stock >300 </Stock >

</Table1 >
<Table1 >
<cClass >Standard </cClass >
<Code >BCD </Code >
<Date >2005-08-06 </Date >
<Time >12:50:33-04:00 </Time >
<Agent >Mike </Agent >
<Stock >100 </Stock >

</Table1 >
</NewDataSet >
<?xml version="1.0" encoding="GB2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match= "/ ">

<xsl:for-each select= "NewDataSet/Table ">
<xsl:variable name= "temp">xulei</xsl:variable>
<xsl:value-of select="$temp"/>
<table >
<xsl:call-template name= "Product"> 调用product过程
<xsl:with-param name="date" select="Desc"/> 实参
</xsl:call-template >
</table >
</xsl:for-each>
</xsl:template>

<xsl:template name= "Product">
<xsl:param name="date"></xsl:param> 声明参数
<tr>
<td>
<p style= "font-weight:bold ">
<xsl:value-of select="$date"/>
<xsl:value-of select= "Code"/>
<xsl:variable name= "para" select= "Code "/>
</p>
</td>
</tr>
<xsl:for-each select= "/NewDataSet/Table1[Code=$para] ">
<xsl:call-template name= "Detail">
</xsl:call-template>
</xsl:for-each>
</xsl:template>

<xsl:template name= "Detail">
<TR>
<TD>
</TD>
<TD>
<xsl:value-of select= "substring-before(Date,'T') "/>
</TD>
<TD class= "SearchResultItem " align= "center ">
<xsl:value-of select= "cClasseDesc_En " />
</TD>
<TD>
<xsl:value-of select= "Stock_Before " />
</TD>
<TD>
<xsl:value-of select= "StockQty " />
</TD>
<TD>
<xsl:value-of select= "Agent " />
</TD>
<TD>
<xsl:value-of select= "substring-before(ModifTime,'T') "/>
</TD>
</TR>
</xsl:template >


</xsl:stylesheet >
 
<persons>
  
<person name="Tarzan" id="050676"/>
  
<person name="Donald" id="070754"/>

  
<person name="Dolly" id="231256"/>
</persons>

查找XSL文件中id=="050676"的人:
<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="preg" match="person" use="@id"/>
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="key('preg','050676')">
<p>

Id: <xsl:value-of select="@id"/><br />
Name: <xsl:value-of select="@name"/>
</p>
</xsl:for-each>

</body>
</html>
</xsl:template>
</xsl:stylesheet>