Re: [xsl] Sort XML based on Tokenized String of sort by fields

Subject: Re: [xsl] Sort XML based on Tokenized String of sort by fields
From: David Carlisle <davidc@xxxxxxxxx>
Date: Thu, 22 May 2008 09:09:10 +0100
> <xsl:variable name="ORDER_BY_TOKEN" select="tokenize($ORDER_BY,',')"/>


I'd use a regexp of '\s*,\s*' here then you lose the white space (and
don't need to use normalize-space later to remove it)

     <xsl:sort select="saxon:evaluate(normalize-space($ORDER_BY_TOKEN[1]))"/>         
you don't need to use an extension function here you can use
     <xsl:sort select="*[local-name()=$ORDER_BY_TOKEN[1]]"/>         

Unless the number of sort keys is really unbounded, personally I'd
probably just make those two changes, add say 10 of these lines to cover
all(?) cases in practice and call it done, but....


>     <xsl:for-each select="1 to count($ORDER_BY_TOKEN)">
>       <xsl:sort select="saxon:evaluate(normalize-space($ORDER_BY_TOKEN[.]))"/> 
>       <xsl:copy-of 



The way to do this in xslt is to generate a stylesheet, you do it in two
passes, first generaate a stylesheet then execute it you can do that in
one call to saxon, if you wish with the saxon-transform extension, which
means that the generated stylesheet can just be ina variable and never
actually serialised.

<xsl:element name="xsl:for-each>
<xsl:attribute name="select" select="'*'"/>
<xsl:for-each select="$ORDER_BY_TOKEN">
  <xsl:element name="xsl:sort">
     <xsl:attribute name="select" select="."/>
  </xsl:element>


...

You might want to do this in any case as it will mean that you can get
rid of other uses of local-name too, which probbaly makes the generated
code rather more efficient, such as


> <xsl:template match="REPORT/*">
> 
> <xsl:choose>
> 
>  <xsl:when test="local-name() = $PATH">
 
which could be replaced by something  generated by

  <xsl:element name="xsl:template">
     <xsl:attribute name="match" select="concat('REPPORT/',$PATH)"/>



On the other hand it isn't clear to me that you need multiple sort keys
at all. You appear to be doing string as opposed to numeric sorting in
all cases and so doesn't a single

<xsl:sort select="string-join($ORDER_BY_TOKEN,',')"/> 

do the job replacing ',' by some other separator if there is any
possibility of , appearing in any of the fields.

David

________________________________________________________________________
The Numerical Algorithms Group Ltd is a company registered in England
and Wales with company number 1249803. The registered office is:
Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.

This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs. 
________________________________________________________________________

Current Thread