<xsl:apply-templates> vs <xsl:call-template/>

本文介绍了XSLT中&lt;xsl:apply-templates&gt;与&lt;xsl:call-template&gt;的区别及其使用场景。通过示例说明了&lt;xsl:apply-templates&gt;适用于自动处理节点,而&lt;xsl:call-template&gt;则提供更精细的控制,允许创建特殊模板以实现更复杂的功能。
摘要由CSDN通过智能技术生成

On a very basic level, you use <xsl:apply-templates> when you want to let the processor handle nodes automatically, and you use <xsl:call-template/> when you want finer control over the processing. So if you have:

<foo>
    <boo>World</boo>
    <bar>Hello</bar>
</foo>

And you have the following XSLT:

<xsl:template match="foo">
    <xsl:apply-templates/>
</xsl:template>

<xsl:template match="bar">
    <xsl:value-of select="."/>
</xsl:template>

<xsl:template match="boo">
    <xsl:value-of select="."/>
</xsl:template>

You will get the result WorldHello. Essentially, you've said "handle bar and boo this way" and then you've let the XSLT processor handle these nodes as it comes across them. In most cases, this is how you should do things in XSLT.

Sometimes, though, you want to do something fancier. In that case, you can create a special template that doesn't match any particular node. For example:

<xsl:template name="print-hello-world">
    <xsl:value-of select="concat( bar, ' ' , boo )" />
</xsl:template>

And you can then call this template while you're processing <foo> rather than automatically processing foo's child nodes:

<xsl:template match="foo">
    <xsl:call-template name="print-hello-world"/>
</xsl:template>

In this particular artificial example, you now get "Hello World" because you've overriden the default processing to do your own thing.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值