Re: [xsl] Generic XSLT Sorting Template

Subject: Re: [xsl] Generic XSLT Sorting Template
From: Joerg Heinicke <joerg.heinicke@xxxxxx>
Date: Sat, 16 Nov 2002 04:08:06 +0100


MoMad wrote:
I am trying to find a way to create a generic xsl sorting function that
takes 5 parameters, which are the names of the nodes to sort.

I dont understand how to pass parameters to an xsl stylesheet, but basically
i just want a simple sorting function that sorts the xml and returns a
sorted version of the xml using xsl:copy.

So far this is what i got:


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; >

 <xsl:template name="GenericSort">
    <!-- The sort fields -->
    <xsl:param name="SortField0" select="" />

You should get an error here -------------^^ ("missing argument") Change it to select="''", same for the rest.

    <xsl:param name="SortField1" select="" />
    <xsl:param name="SortField2" select="" />
    <xsl:param name="SortField3" select="" />
    <xsl:param name="SortField4" select="" />

<xsl:copy >

In my opinion absolutely useless. This prevents any "generic", because you will always copy a node from input to output.


      <xsl:apply-templates>
        <xsl:sort select="$SortField0" />

Change it all sort selects to "*[name() = $SortFieldX]" (X = 0 to 4).


        <xsl:sort select="$SortField1" />
        <xsl:sort select="$SortField2" />
        <xsl:sort select="$SortField3" />
        <xsl:sort select="$SortField4" />
      </xsl:apply-templates>
    </xsl:copy>
 </xsl:template>

</xsl:stylesheet>

Then there remains the question, whether is useful in general. In my opinion not. Because you can't select special elements, you always apply templates on all nodes. What about modes on <xsl:apply-templates/>?


Maybe you only have a wrong understanding of named templates? They do not work like functions in procedural languages! So you can't pass an unsorted node set and the template returns the sorted one. Storing the result in a variable would help you a bit, but needs an processor specific extension function.

i want the first sort field
(sortfield0) to sort on the global node-set (ancestor::*[next-sibling()]) ??

This I don't understand ...


Regards,

Joerg


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



Current Thread