xsl-list
[Top] [All Lists]

RE: [xsl] sorting on decimal

2009-05-18 17:10:25
Without collations this is a XSLT 1.0 solution:
(restriction is that each single decimal number is less than 256)

$ cat data.xml
<data>
<elem number="3.9.10">a</elem>
<elem number="3.10.1">b</elem>
<elem number="3.9.11">c</elem>
<elem number="3.9.9">d</elem>>
</data>
$ java org.apache.xalan.xslt.Process -in data.xml -xsl dec-coll.xsl | tidy
-q -xml
<?xml version="1.0" encoding="utf-8"?>
<elem number="3.9.9">d</elem>
<elem number="3.9.10">a</elem>
<elem number="3.9.11">c</elem>
<elem number="3.10.1">b</elem>

$ cat dec-coll.xsl
<xsl:stylesheet version="1.0">
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:func="http://exslt.org/functions";
  xmlns:ns="namespace"

  <xsl:output method="xml" />

  <xsl:template match="/data">
    <xsl:for-each select="elem">
      <xsl:sort select="ns:dec-coll(@number)"/>
      <xsl:copy-of select="."/>
    </xsl:for-each>
  </xsl:template>

<!-- generates hex sequence from decimal numbering, eg. 03090B from 3.9.11
-->
  <func:function name="ns:dec-coll">
    <xsl:param name="str" />
    <xsl:choose>
      <xsl:when test="contains($str,'.')">
        <func:result select="concat(ns:hex(substring-before($str, '.')),
                             ns:dec-coll(substring-after($str, '.')))" />
      </xsl:when>
      <xsl:otherwise>
        <func:result select="ns:hex($str)" />
      </xsl:otherwise>
    </xsl:choose>
  </func:function>

<!-- works fine for $num &lt; 256 -->
  <func:function name="ns:hex">
    <xsl:param name="num" />
    <func:result select="substring
('000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF',
                                   2*$num +1,2)"/>
  </func:function>

</xsl:stylesheet>>

$


Mit besten Grüßen / Best wishes,

Hermann Stamm-Wilbrandt
Developer, XML Compiler
WebSphere DataPower SOA Appliances
----------------------------------------------------------------------
IBM Deutschland Research & Development GmbH
Vorsitzender des Aufsichtsrats: Martin Jetter
Geschäftsführung: Erich Baier
Sitz der Gesellschaft: Böblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294


                                                                       
             Don Smith                                                 
             <dsmith_lockesmit                                         
             h(_at_)yahoo(_dot_)com>                                            
   To
                                       
xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com 
             05/16/2009 01:26                                           cc
             AM                                                        
                                                                   Subject
                                       RE: [xsl] sorting on decimal    
             Please respond to                                         
             xsl-list(_at_)lists(_dot_)mu                                       
  
              lberrytech.com                                           
                                                                       
                                                                       
                                                                       





Yes, that did the trick.

Thanks very much.

Don

--- On Fri, 5/15/09, Michael Kay <mike(_at_)saxonica(_dot_)com> wrote:

From: Michael Kay <mike(_at_)saxonica(_dot_)com>
Subject: RE: [xsl] sorting on decimal
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Date: Friday, May 15, 2009, 5:12 PM

Yes, if you're using Saxon then

<xsl:sort select="@number"
collation="http://saxon.sf.net/collation?alphanumeric=yes"/>

should do the job.

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay

-----Original Message-----
From: David Carlisle [mailto:davidc(_at_)nag(_dot_)co(_dot_)uk]

Sent: 15 May 2009 20:52
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] sorting on decimal


you could use a collation that split up the decimal
ranges
(saxon has an alphanumeric collation that would
probably do the job)

http://www.saxonica.com/documentation/extensibility/collation.html

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.

______________________________________________________________
__________


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--






--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>