What's wrong with xsl: for-each?

I hear again and again about how you should avoid using XSLT for everyone. That this is your internal imperative daemon programmer who must be exiled.

Why so bad?

Does it really depend on the size of the XML (e.g. 100 vs 10,000 nodes)?

+6
xslt
source share
5 answers

The significant difference between <xsl:apply-templates> and <xsl:-for-each> , which no one mentioned :

xsl:apply-templates much richer and deeper than xsl:for-each , even simply because we don’t know which code will be used on the nodes of the choice - in general, this code will be different for different nodes of the node -list .

In addition, the code that will be applied can be written after xsl: apply templates, and people who do not know the original author .


_2. On the other hand, using <xsl:for-each> is in no way harmful if you know exactly how <xsl:for-each> handled .

The problem is that many XSLT newbies with experience in imperative programming accept <xsl:for-each> as a replacement for the "loop" in their favorite PL and consider that it allows them to accomplish the impossible, such as increasing the counter or whatever modification of an already defined <xsl:variable> .

One of the indispensable uses of <xsl:for-each> is to modify the current document - this is often necessary in order to be able to use the key () in a document different from the current source XML document , for example, to effectively access the lookup table located in native XML document.

+6
source share

Templates tend to share code better. In addition, for each loop, the fact that people often come to them with the idea that they work the same way as the loops work in the main programming languages ​​suffers.

+5
source share

Quick answer: XSLT is mostly functional in nature, and imperative loops are not very functional.

In general, based on the best use of XSLT, it is to maximize the use of pattern matching ( xsl:apply-template , not loops, ifs and call-template 1 ).

In the end, it’s all about style, and little will change in the short XSLT snippets, but a more functional one will help with longer / more complex problems.

1 In addition to creating functions that return values, not change output.

+2
source share

Using for-each to invoke patterns that are not recommended, and not for widespread use of for-each , as such. Even the Muenchian grouping method relies on xsl:key constructs with xsl:for-each loops.

The idea of ​​writing a good XSLT is that your XML structure should determine which templates are matched and then applied. Therefore, whenever possible, use apply-templates to select the nodes, and not to apply the selection with for-each .

+2
source share

Using apply-templates , possibly with mode , makes it easy to include the appropriate transformations from more kinds of elements that may be of interest in this place.

For example, if you have XML content that contains the contents of libraries, and you continue to use books for each one at your place of choice, then you start burning DVDs, CDs, cookies with Ginger cookies, etc., you will have to study everything for each section include more than just books. If you used application templates, you can simply create new matches for new elements.

+1
source share

All Articles