Re: [xsl] Creating a tree variable and using it in later processing

Subject: Re: [xsl] Creating a tree variable and using it in later processing
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 20 Jul 2001 12:02:00 -0400
Alan,

The short answer, given that you stipulate "no extension functions" is that, no, you can't do it (the way you are describing). There are other approaches you could use to do the same thing, however.

If you just want them sorted alphabetically, that's not too bad. The trick is to avoid the step of "storing the sorted list in a variable" since, as you're discovering, that's where the trouble starts (since it then becomes, not a node-set but a result-tree-fragment). Rather, you want to access the nodes in the order you want straight off. (That is, what it's boiling down to is that currently you are sorting twice, and discovering you can't, because once the nodes are in an RTF you can't sort them again.)

Thus, it comes down to thinking carefully about how to achieve, with a single sort, what you are now doing with two sorts. For example, to "access alternate names in the sorted list", you could do

<xsl:for-each select="product">
  <xsl:sort select="name" data-type="text"/>
  <xsl:if test="position() mod 2">
  <!-- this test throws out the even-numbered ones -->
    <!-- do your thing -->
  </xsl:if>
</xsl:for-each>

then repeat with a different test for the others, etc.

Notice it's going to get pretty tricky, especially if we have to provide for grouping duplicates, etc.: soon you may have to resort to some sophisticated kinds of processing such as (1) nasty recursive templates to achieve the sorting by going through the nodes one at a time and evaluating their status based on parameters you pass, or (2) having a "lookup table" of known values in the order you want, and sorting by reference to that, and so on.

If I were you, however, I'd seriously consider using the extension function after all, for these reasons: (1) it'll make things *much* easier and cleaner; (2) among extension functions, node-set is among the most (perhaps *the* most) widely implemented; (3) early indications are (at least judging from the XSLT 1.1 WD, now shelved) that this among the very next capabilities to be provided in the language natively.

(An aside: wouldn't it be nice to have, not just an XSLT instruction, but a *function* for sorting?)

If you really want to do it without the node-set extension, maybe you should post again with more specifics about the sorting you need, and we can get out the heavy artillery (meaning Jeni, Dimitre, Mike ... you know who you are!).

Cheers,
Wendell

At 08:44 PM 7/19/01, you wrote:
Hi,

I have an input XML file in which the elements are not
sorted how I want. I need to

1. sort the elements and
2. display these sorted elements in a particular
order....


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================


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



Current Thread