It's impossible to stop thinking about XSLT in procedural terms ... help with apply-templates

I know that XSLT does not work in procedural terms, but unfortunately I run procedural languages ​​for too long. Can someone help me by explaining in simple terms how such patterns work, and help the crowd, like me, understand it.

+4
source share
3 answers

What makes you think that procedural conditions are not applicable here? It is simply that the calling convention is somewhat more implicit than you traditionally expected, because there is an invisible context. All apply-templates can be expressed in procedural terms.

Basically, apply-templates is nothing more than a for-each loop. Starting from where you are in the document (context, think " this "), it iterates over the child nodes.

For each child, the processor selects the appropriate xsl:template with the highest priority (based on the corresponding match and priority attributes), sets the context for the child, and launches this template (think " function "). After returning the template, the context bounces back and the next child addresses it.

Even when things become recursive (which is difficult to avoid in XSLT), the whole process does not really get complicated. The pointer context moves and templates are called.

You can restrict the node set that iterates apply-templates using the select attribute:

 <!-- all children of the context node regardless --> <xsl:apply-templates /> <!-- all children of the context node being "data" with a @name of "Foo" --> <xsl:apply-templates select="data[@name='Foo']" /> 

You can sort node -set before iteration if you want:

 <!-- all children of the context node being "data" with a @name of "Foo", ordered by their respective "detail" count --> <xsl:apply-templates select="data[@name='Foo']"> <xsl:sort select="count(detail)" data-type="number" order="descending"/> </xsl:apply-templates> 

And you can pass parameters to your template if you need, just like with a regular function call:

 <!-- pass in some parameter --> <xsl:apply-templates select="data[@name='Foo']"> <xsl:with-param name="DateSetIcon" select="$img_src" /> </xsl:apply-templates> 

It is all about him.

EDIT:

I know the last comment is a bit provocative. This is very intense since the basic understanding of how apply-templates works is more or less. The consequences and opportunities that come from the fact that you do not determine which template to call, but rather allow the processor to choose the right one for you, are, of course, more than what sounds like an unprepared ear. The declarative / implicit approach of all this certainly takes some time to drown.

+5
source

I wrote a blog post a long time ago that shows a simple stylesheet written in a "procedural" style using xsl:for-each and equivalent using xsl:apply-templates . This is not a complete reference, but hopefully it will be useful.

+5
source

I completely agree with the answer and the Greg Beech blog.

For a more detailed comparison between <xsl:for-each> and <xsl:apply-templates> see my answer to the question "xsl: for-each vs. xsl: apply-templates" in the xsl list and enjoy the whole stream .

"xsl: apply-templates is much richer and deeper than xsl: for-each, even simply because we don’t know what code will be used on the nodes of the choice - in general, this code will be different for the different nodes of the node list. In addition The code that will be applied can be written after xsl: apply the templates, and people who do not know the original author .

Another difference from the procedural programming language is that the order in which the templates are applied is not predefined. There is no concept of “state” or “execution order” in the pure language of functional programming.

Neither XSLT 2.0 nor XSLT 1.0 defines any specific order of application of the selected template rules - only their results will be combined in accordance with the order of the nodes (in a sorted sequence, if there are <xsl:sort> or otherwise in the order of the nodes), which apply templates.

XSLT 2.0 Spec says:

"Each template rule that is evaluated creates a sequence of elements as the result. The resulting sequences (one for each node in the sorted sequence) are then combined to form one sequence. They are combined, preserving the order of the nodes in the sorted sequence. The final concatenated sequence generates the result of the instruction xsl: apply-templates "

XSLT 1.0 says:

"Implementations can freely process the source document in any way that produces the same result as if it were processed using this processing model."

It is even possible that an XSLT implementation can apply templates (or the body <xsl:for-each> ) in parallel.

+2
source

All Articles