call-template vs. apply-template

原创 2005年03月01日 20:28:00

                                                   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/>&#xA0;&#xA0;&#xA0;
   <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>


Code Behind vs. Template Components

I think a lot of times we as developers get caught up in our own world, and we forget how all of the...

JavaScript中call与apply方法

  • 2014年03月11日 20:45
  • 18KB
  • 下载

Spring Template(三) ——ORM 设计与实现

通常情况下,ORM用的最多的是Hibernate。使用它,除了需要处理像Session、SessionFactory这些Hibernate类之外,还需要处理诸如事务处理、打开Session和关闭Ses...

FreeMarker使用Template生成可变参数的json(二)

FreeMarker使用Template生成可变参数的json

【java设计模式】之 模板方法(Template Method)模式

1. 模板方法的一个实例         这一节主要来学习一下设计模式中的模板方法模式。我们先来看一个例子:假如现在老板让你做一个汽车的模型,要求只要完成基本功能即可,不考虑扩展性,那你会怎么做呢?...
  • eson_15
  • eson_15
  • 2016年05月06日 08:57
  • 3571

h5学习笔记:weui text/template 使用

昨天开始尝试一下使用这个,之前使用weui的时候,产生一个列表。通过ajax 获取一个数组列表。这个时候,第一次做的时候采用字符串拼凑的方式将字符串拼凑起来,在当时而言这种做法凑效,但相当痛苦。Vue...

QT工程pro文件TEMPLATE变量说明

QT工程pro文件模板变量(TEMPLATE) 模板变量告诉qmake为这个应用程序生成哪种makefile。下面是可供使用的选择: app - 建立一个应用程序的makefile。这是默认值...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:call-template vs. apply-template
举报原因:
原因补充:

(最多只允许输入30个字)