Re: [xsl] Creating a padded sort key: easier from elt sequence or string?

Subject: Re: [xsl] Creating a padded sort key: easier from elt sequence or string?
From: Florent Georges <darkman_spam@xxxxxxxx>
Date: Wed, 26 Jul 2006 14:52:35 +0200 (CEST)
Yves Forkl wrote:

  Hi

> - As my dotted sequence can be of variable length, I need to
>    determine the number of sort statements dynamically.

  As you said the numbers are < 1000, you can easily create a
normalized sort string:

    ~/xslt/tests> cat sort-keys.xsl
    <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                   xmlns:xs="http://www.w3.org/2001/XMLSchema";
                   xmlns:my="my:sort-keys.xsl"
                   version="2.0">

      <xsl:output method="text"/>

      <xsl:function name="my:normalise-sort-keys" as="xs:string">
        <xsl:param name="keys" as="element()+"/>
        <xsl:value-of separator="." select="
            for $s in $keys/string(.) return
              if ( matches($s, '[0-9]+') ) then
                format-number(number($s), '000')
              else
                $s
          "/>
      </xsl:function>

      <xsl:template match="/">
        <xsl:for-each select="root/elt">
          <xsl:sort select="my:normalise-sort-keys(sort_key_elt)"/>
          <xsl:value-of select="@id"/>
          <xsl:text>&#10;</xsl:text>
        </xsl:for-each>
      </xsl:template>

    </xsl:transform>

    ~/xslt/tests> cat sort-keys.xml
    <root>
      <elt id="1">
        <sort_key_elt>A</sort_key_elt>
        <sort_key_elt>12</sort_key_elt>
        <sort_key_elt>z</sort_key_elt>
      </elt>
      <elt id="2">
        <sort_key_elt>A</sort_key_elt>
        <sort_key_elt>4</sort_key_elt>
        <sort_key_elt>z</sort_key_elt>
      </elt>
    </root>

    ~/xslt/tests> saxon sort-keys.xml sort-keys.xsl
    2
    1

  If you choose a string instead of several elements as your keys, you
can adapt this with tokenize().

  Regards,

--drkm






























	

	
		
___________________________________________________________________________ 
Dicouvrez un nouveau moyen de poser toutes vos questions quelque soit le sujet ! 
Yahoo! Questions/Riponses pour partager vos connaissances, vos opinions et vos expiriences. 
http://fr.answers.yahoo.com 

Current Thread