[xsl] Re: Order which templates apply

Subject: [xsl] Re: Order which templates apply
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Sat, 8 Mar 2003 19:22:05 +0100
"Karl Stubsjoen" <karl@xxxxxxxxxxxxx> wrote in message
news:01d001c2e4cd$7018b050$71d76844@xxxxxxx
> Hello,
>
> I know this question has been asked... but I still don't get it.  More
> importantly, I don't understand how to change the order which template
rules
> are applied.  I have multiple template matches which match different parts
> of my xml object.  I'd like to control which template is applied first.  I
> have this:
>
>      <form name='asdf' action='dbxmladmin.asp' method='POST'>
>            <xsl:apply-templates/>
>       </form>
>
>
> >From this, I build a set of form elements, and then some hidden form
> elements.  The hidden form elements, which appear higher up in the xml
> object are applied first, however I'd like this template to be applied
last.
> Was I too vague, do I need to give more information?

Yes, for a general question the answer is also general.

The order in which templates are applied to nodes generally cannot be
controlled, because XSLT is a functional language and when a sequence of
(siblings) independent xslt instructions are executed, there's no way to be
sure in what order they will be executed (e.g. we could have a parallel
implementation of an XSLT processor, or a clever XSLT processor could decide
to perform some optimisations, ..., etc.).

The only thing that is guaranteed is that their *output* will be the same as
in the case when they were executed sequentially -- and it seems to me that
this is what you actually care about.

This is what the XSLT spec
(http://www.w3.org/TR/xslt#section-Processing-Model) says:

"A list of source nodes is processed to create a result tree fragment. The
result tree is constructed by processing a list containing just the root
node. A list of source nodes is processed by appending the result tree
structure created by processing each of the members of the list in order. A
node is processed by finding all the template rules with patterns that match
the node, and choosing the best amongst them; the chosen rule's template is
then instantiated with the node as the current node and with the list of
source nodes as the current node list. A template typically contains
instructions that select an additional list of source nodes for processing.
The process of matching, instantiation and selection is continued
recursively until no new source nodes are selected for processing.
Implementations are free to process the source document in any way that
produces the same result as if it were processed using this processing
model."



So, if you care just about the ordering of the output, you can explicitly
control it by specifying a sequence of (let's say) xsl:apply elements, each
with a specific nodeset (subset of the nodes to be processed) specified as
the value of the "select" attribute. In your case you coud have:



  <xsl:apply-templates select="hiddenFormProducingElementsExpression"/>

  <xsl:apply-templates select="otherElementsExpression"/>



In this way in the resulting output the hidden form elements will be before
the other elements.



=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL





 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread