[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Sort XML based on Tokenized String of sort by fiel

Subject: Re: Sort XML based on Tokenized String of sort by fields
From: David Carlisle <davidc@xxxxxxxxx>
Date: Thu, 22 May 2008 09:09:10 +0100
Re:  Sort XML based on Tokenized String of sort by fiel
> <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="."/>


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.


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


Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
First Name
Last Name
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.