Re: [xsl] Rearrange order of processed nodes

Subject: Re: [xsl] Rearrange order of processed nodes
From: "Bauman, Syd s.bauman@xxxxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 11 Mar 2025 16:23:18 -0000
I am wondering if anyone else thinks simplicity has its up side:

  <xsl:apply-templates select="elem1|elem2|elem3|elem4|elem5|elem6"/>
  <xsl:apply-templates select="node() except
(elem1|elem2|elem3|elem4|elem5|elem6)"/>

Or, if you have lots of elements in the 1st set use a variable as suggested in
the OP, or the function suggested by Michael Kay:

  <xsl:apply-templates select="*[ not( f:is-ordinary(.) ) ]"/>
  <xsl:apply-templates select="node()[ f:is-ordinary(.) ]"/>

Or am I missing something, and those do not produce the same results as the
previous suggestions,\xB9 or are they somehow woefully inefficient? (Because
they certainly seem more readable to me. Although if I were doing this I would
probably use the reverse of the function M. Kay suggested, that is a
f:do-me-first() function. But it should make no difference.)

Note
\xB9 For example, does an extra text node of white space get inserted between
the do-first set and the rest?
________________________________
MMH> Yes, I could collect the special elements in a variable and use this.
MMH>
MMH> <xsl:variable name="specialElems"
select="(elem1|elem2|elem3|elem4|elem5|elem6)" as="element()+"/>
MMH>
MMH> <xsl:apply-templates select="$specialElems, node() except
$specialElems"/>
MMH>
GI> That's the way I typically tackle this.
GI> Sometimes, if a more complicated sort order is necessary, typically due to
schema requirements, I write a sorting function that returns an integer value
for each element and then I use xsl:apply-templates/xsl:sort. This sort
function might apply itself recursively (with cache="yes") to the preceding
sibling of the element in question. Or it might transform its argument in a
specific mode so that you don't need to overwrite the whole function if you
want to tweak things in an importing stylesheet (the mhhm approach I presented
at Balisage 2020 [1]).

MK> You could have a function with a boolean result to test if an element is
special or ordinary:
MK> <xsl:function name="f:is-ordinary" as="xs:boolean">
MK>   <xsl:param name="element" as="element()"/>
MK>   <xsl:sequence select="not(self::(elem1 | elem2 | elem3 ...))"/>  <!--
4.0 syntax -->
MK> </xsl:function>
MK> and then use this as a sort key:
MK> <xsl:apply-templates select="sort(*, (), f:is-ordinary#1)"/>
MK> etc. Note false sorts before true.

Current Thread