call-template vs apply-template对比
Element: xsl:call-template
Calling a template by name. Causes no context switch (change of context
node) as apply-templates and for-each do. The template you call by name will
still be processing the same context node as your current template. This el
ement can be used to reuse the same functionality in several templates.
Element: xsl:apply-templates
Used to pass the context on to another template. The select attribute
specifies which nodes should be transformed now, the processor decides
which templates will be used.
一. 调用函数call-template
The <xsl:call-template> enables you to invoke a named template—that is, an <xsl:template> element—that has an assigned name attribute. If an <xsl:template> element has a name attribute, it might, but need not, also have a match attribute. An <xsl:call-template> element invokes a template by name; it has a required name attribute that identifies the template to be invoked. Unlike <xsl:apply-templates>, <xsl:call-template> does not change the current node or the current node-list.
An error occurs if a style sheet contains more than one template with the same name and with the same import precedence.
An <xsl:call-template> element can contain any number of <xsl:with-param> elements. However, it cannot contain other XSLT elements.
for example:
调用函数一样call-template,通过with-param传入参数.
<xsl:call-template name="topic_title">
<xsl:with-param name="editable" select="$editable"/>
<xsl:with-param name="value" select="@title"/>
</xsl:call-template>
其中$editable为全局变量 , @title为节点属性
注意:
1.
已知
<xsl:template name="numbered-block">
<xsl:param name="format">1. </xsl:param>
<fo:block>
<xsl:number format="{$format}"/>
<xsl:apply-templates/>
</fo:block>
</xsl:template>
a.则<xsl:call-template name="numbered-block"/> 使用默认参数调用函数,即1,2,3..
则<xsl:call-template name="numbered-block">
<xsl:with-param name="format">a. </xsl:with-param>
</xsl:call-template> 使用给定参数调用函数,即a,b,c...
apply-template即为把节点中的值写入html.
b.可以这样给函数指定参数:(value-of select)从节点获得
<xsl:with-param name="value">
<xsl:value-of select="."/>
</xsl:with-param>
c.也可以这样给函数指定参数:(直接写好true)
<xsl:with-param name="editable">true</xsl:with-param>
d.<xsl:param name="value"/>无默认值的函数参数
即
<xsl:template name="para_title">
<xsl:param name="value"/>
<xsl:param name="editable"/>
...操作
</xsl:template>
2.template(模板)匹配的方法:
<xsl:template match="ol//ol/li">
<br/>   
<xsl:call-template name="numbered-block">
<xsl:with-param name="format">a. </xsl:with-param>
</xsl:call-template>
</xsl:template>
每次遇到ol//ol/li节点则调用其间操作.
同理,
<xsl:template match="ol/li">
<br/>
<xsl:call-template name="numbered-block"/>
</xsl:template>
每次遇到ol/li节点则调用其间操作.
3.
$读取参数或变量值
@读取节点
设置参数变量 <xsl:param name="editable" select="true"/>值为true
二. 函数apply-template
<xsl:apply-templates
select = Expression
mode = QName>
</xsl:apply-templates>
Directs the XSLT processor to find the appropriate template to apply, based on the type and context of each selected node
The <xsl:apply-templates> element first selects a set of nodes using the expression specified in the select attribute. If this attribute is left unspecified, all children of the current node are selected. For each of the selected nodes, <xsl:apply-templates> directs the XSLT processor to find an appropriate <xsl:template> to apply. Templates are tested for applicability by comparing the node to the XPath expression specified in the template's match attribute. If more than one template satisfies the match pattern, the one appearing with the highest priority is chosen. If several templates have the same priority, the last in the style sheet is chosen.
例子:
XML File (customers.xml)
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="applyt.xsl" ?>
<customers>
<customer>
<name>John Smith</name>
<address>123 Oak St.</address>
<state>WA</state>
<phone>(206) 123-4567</phone>
</customer>
<customer>
<name>Zack Zwyker</name>
<address>368 Elm St.</address>
<state>WA</state>
<phone>(206) 423-4537</phone>
</customer>
<customer>
<name>Albert Aikens</name>
<address>368 Elm St.</address>
<state>WA</state>
<phone>(206) 423-4537</phone>
</customer>
<customer>
<name>Albert Gandy</name>
<address>6984 4th St.</address>
<state>WA</state>
<phone>(206) 433-4547</phone>
</customer>
<customer>
<name>Peter Furst</name>
<address>456 Pine Av.</address>
<state>CA</state>
<phone>(209) 765-4321</phone>
</customer>
<customer>
<name>Dan Russell</name>
<address>9876 Main St.</address>
<state>PA</state>
<phone>(323) 321-7654</phone>
</customer>
</customers>
XSLT File (applyt.xsl)
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/">
<HTML>
<BODY>
<TABLE border="1" cellspacing="0" cellpadding="2">
<xsl:apply-templates select="customers/customer">
<xsl:sort select="state"/>
<xsl:sort select="name"/>
</xsl:apply-templates>
</TABLE>
</BODY>
</HTML>
</xsl:template>
<xsl:template match="customer">
<TR>
<xsl:apply-templates select="name" />
<xsl:apply-templates select="address" />
<xsl:apply-templates select="state" />
<xsl:apply-templates select="phone" />
<xsl:apply-templates select="phone" mode="accountNumber"/>
</TR>
</xsl:template>
<xsl:template match="name">
<TD STYLE="font-size:14pt font-family:serif">
<xsl:apply-templates />
</TD>
</xsl:template>
<xsl:template match="address">
<TD> <xsl:apply-templates /> </TD>
</xsl:template>
<xsl:template match="state">
<TD> <xsl:apply-templates /> </TD>
</xsl:template>
<xsl:template match="phone">
<TD> <xsl:apply-templates /> </TD>
</xsl:template>
<xsl:template match="phone" mode="accountNumber">
<TD STYLE="font-style:italic">
1-<xsl:value-of select="."/>-001
</TD>
</xsl:template>
</xsl:stylesheet>
Output:
This is the processor output:
<HTML>
<BODY>
<TABLE border="1" cellspacing="0" cellpadding="2">
<TR>
<TD STYLE="font-size:14pt font-family:serif">Peter Furst</TD>
<TD>456 Pine Av.</TD>
<TD>CA</TD>
<TD>(209) 765-4321</TD>
<TD STYLE="font-style:italic">
1-(209) 765-4321-001
</TD>
</TR>
<TR>
<TD STYLE="font-size:14pt font-family:serif">Dan Russell</TD>
<TD>9876 Main St.</TD>
...
</TR>
</TABLE>
</BODY>
</HTML>