xsl:for-each 有什么不好? [英] What's so bad about xsl:for-each?

查看:34
本文介绍了xsl:for-each 有什么不好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一次又一次地听到您应该如何避免使用 XSLT for-each.应该驱逐的是你内心的命令式编程恶魔.

I hear time and time again about how you should avoid the use of XSLT for-each. That it's your inner imperative programming demon that should be banished.

这有什么不好?

此最佳实践是否取决于 XML 的大小(即 100 与 10,000 个节点)?

Does this best practice matter depending on the size of XML (i.e 100 vs 10,000 nodes)?

推荐答案

的本质区别; 没有人指出:

Essential difference between <xsl:apply-templates> and <xsl:-for-each> that nobody has pointed out:

  1. 实际上不仅仅是一个更好、更优雅的 :

xsl:apply-templatesxsl:for-each 更加丰富和深入,甚至仅仅是因为我们不知道将在节点上应用什么代码选择 - 在一般情况下,此代码将不同节点列表的不同节点.

xsl:apply-templates is much richer and deeper than xsl:for-each, even simply because we don't know what code will be applied on the nodes of the selection -- in the general case this code will be different for different nodes of the node-list.

此外,将应用的代码可以在编写 xsl:apply 模板之后编写,并且通过不知道原作者的人.

Also, the code that will be applied can be written way after the xsl:apply templates was written and by people that do not know the original author.

_2.另一方面,使用; 如果您确切地知道 是如何处理的,那么它绝不会有害.

_2. On the other side, using <xsl:for-each> is in no way harmful if one knows exactly how an <xsl:for-each> is processed.

问题在于,许多有命令式编程经验的 XSLT 新手将 <xsl:for-each> 用作他们最喜欢的 PL 中的循环"的替代品,并认为它允许他们执行不可能的事情——比如增加一个计数器或对已经定义的 进行任何其他修改.

The trouble is that a lot of newcomers to XSLT that have experience in imperative programming take <xsl:for-each> as a substitute of a "loop" in their favorite PL and think that it allows them to perform the impossible -- like incrementing a counter or any other modification of an already defined <xsl:variable>.

<xsl:for-each> 的一个不可或缺的用途是更改当前文档——这通常是需要的 为了能够在文档上使用key()函数,与当前源XML文档不同a>,例如有效访问驻留在其自己的 xml 文档中的查找表.

One indispensable use of <xsl:for-each> is to change the current document -- this is often needed in order to be able to use the key() function on a document, different from the current source XML document, for example to efficiently access lookup-table that resides in its own xml document.

这篇关于xsl:for-each 有什么不好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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