2008年9月13日星期六

xsl:call-template,XSLT里的函数调用

Through xsl:call-template, XSLT can mimic call another template like normal programming language. and the caller can provide named parameter(s) through xsl:with-param, and the callee can declare param througn xsl:param element. the callee access these parameter(s) through a $ symbol plus the parameter's name. 



通过xsl:call-template功能,XSLT可以实现一定的函数调用功能。如下例所示,一个模板可以调用另一个模板,并提供相应的参数。被调用的模板可以使用$paramname访问参数,同时也可以访问当前处理的XML文件。

<!--the main template主叫模板-->

<xsl:template name="maintemplate"> 

...........

<xsl:call-template name="linklist">

<xsl:with-param name="start" select="1"/>

<xsl:with-param name="end" select="//totalpage"/>

</xsl:call-template> 

........

</xsl:template>







<!--a template been called被调用的模板-->

<xsl:template name="subtemplate">

<xsl:param name="start"/>

<xsl:param name="end"/>

<xsl:choose>

<xsl:when test="$start=1">

........

</xsl:when>

<xsl:otherwise> 

......

</xsl:otherwise>

</xsl:choose>

</xsl:if>

</xsl:template>


没有评论: