I've heard that most of the time it's usually possible (and better) to use apply-templates rather than for-each when writing an XSLT. Is this true? If so, what are the benefits of using apply-templates?
相关问题
- XML - XSLT - document() function inside count() fu
- Using XSLT to select after EACH instance in a stri
- XSLT open other xml files
- How to use MSbuild property in TransformXml task?
- Using asp objects in .NET - Max compatibility
相关文章
- xslt localization
- convert xml to soap request using xslt transformat
- XALAN register Extension Function like in SAXON
- How to group using XSLT
- How to reformat XML with group-adjacent (XSLT)
- AddExtensionObject - Performance
- Transforming HTML nodes with XSLT in chrome/webkit
- visual studio 2015 xsl debugging transformation fa
Using
<xsl:for-each>
is in no way harmful if one knows exactly how an<xsl:for-each>
is processed.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>
.One indispensable use of
<xsl:for-each>
in XSLT 1.0 is to change the current document -- this is often needed in order to be able to use thekey()
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.On the other side, using
<xsl:template>
and<xsl:apply-templates>
is much more powerful and elegant.Here are some of the most important differences between the two approaches:
xsl:apply-templates
is much richer and deeper thanxsl: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.The code that will be applied can be written way after the
xsl:apply template
s was written and by people that do not know the original author.The FXSL library's implementation of higher-order functions (HOF) in XSLT wouldn't be possible if XSLT didn't have the
<xsl:apply-templates>
instruction.Summary: Templates and the
<xsl:apply-templates>
instruction is how XSLT implements and deals with polymorphism.Reference: See this whole thread: http://www.stylusstudio.com/xsllist/200411/post60540.html