性能:调用模板与应用模板 [英] Performance: call-template vs apply-template

查看:25
本文介绍了性能:调用模板与应用模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 XSLT 处理中,apply-template 和 call-template 之间有性能差异吗?在我的样式表中,我可以使用许多实例,哪个是最佳选择?

in XSLT processing, is there a performance difference between apply-template and call-template? In my stylesheets there are many instances where I can use either, which is the best choice?

推荐答案

与所有性能问题一样,答案将取决于您的特定配置(尤其是您使用的 XSLT 处理器)和您使用的处理类型正在做.

As with all performance questions, the answer will depend on your particular configuration (in particular the XSLT processor you're using) and the kind of processing that you're doing.

<xsl:apply-templates> 获取一系列节点并逐个遍历它们.对于每个,它定位与节点匹配的具有最高优先级的模板,并调用它.所以 就像一个 里面有一个 ,但更加模块化.

<xsl:apply-templates> takes a sequence of nodes and goes through them one by one. For each, it locates the template with the highest priority that matches the node, and invokes it. So <xsl:apply-templates> is like a <xsl:for-each> with an <xsl:choose> inside, but more modular.

相反, 按名称调用模板.上下文节点没有变化(没有 ),也没有选择使用哪个模板.

In contrast, <xsl:call-template> invokes a template by name. There's no change to the context node (no <xsl:for-each>) and no choice about which template to use.

因此,在完全相同的情况下,您可能会认为 会更快,因为它所做的工作更少.但是,如果您处于可以使用 <xsl:apply-templates><xsl:call-template> 的情况下,您可能会自己在 XSLT 中执行 ,而不是处理器在幕后为您做这件事.所以最后我猜它可能会平衡.但正如我所说,这在很大程度上取决于您的处理器已实施的优化类型以及您正在执行的处理.测量一下看看.

So with exactly the same circumstances, you might imagine that <xsl:call-template> will be faster because it's doing less work. But if you're in a situation where either <xsl:apply-templates> or <xsl:call-template> could be used, you're probably going to be doing the <xsl:for-each> and <xsl:choose> yourself, in XSLT, rather than the processor doing it for you, behind the scenes. So in the end my guess it that it will probably balance out. But as I say it depends a lot on the kind of optimisation your processor has put into place and exactly what processing you're doing. Measure it and see.

我关于何时使用匹配模板以及何时使用命名模板的经验法则是:

My rules of thumb about when to use matching templates and when to use named templates are:

  • 如果要处理单个节点以创建结果,请使用 和匹配模板;如果需要以多种不同方式处理特定节点(例如在目录中与文档正文中),请使用模式
  • 如果您正在处理除单个节点之外的其他内容(例如字符串或数字或节点集),请使用 和命名模板
  • (在 XSLT 2.0 中)如果要返回原子值或现有节点,请使用
  • use <xsl:apply-templates> and matching templates if you're processing individual nodes to create a result; use modes if a particular node needs to be processed in several different ways (such as in the table of contents vs the body of a document)
  • use <xsl:call-template> and a named template if you're processing something other than an individual node, such as strings or numbers or sets of nodes
  • (in XSLT 2.0) use <xsl:function> if you're returning an atomic value or an existing node

这篇关于性能:调用模板与应用模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆