分享
 
 
 

call-template vs. apply-template

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有