What's so bad about xsl:for-each?

后端 未结 5 2006
春和景丽
春和景丽 2021-01-12 23:35

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.

What\'s

5条回答
  •  無奈伤痛
    2021-01-12 23:53

    Essential difference between and that nobody has pointed out:

    1. is really something much more than a nicer, more elegant equivalent of :

    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.

    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. On the other side, using is in no way harmful if one knows exactly how an is processed.

    The trouble is that a lot of newcomers to XSLT that have experience in imperative programming take 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 .

    One indispensable use of 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.

提交回复
热议问题