Re: [xsl] dynamic sort using attributes

Subject: Re: [xsl] dynamic sort using attributes
From: Florent Georges <darkman_spam@xxxxxxxx>
Date: Tue, 5 Dec 2006 11:09:24 +0100 (CET)
L P wrote:

  Hi

>   <reminders>
>     <reminder category="Test" nm="XXX" description="foobar"/>
>   <reminder category="Test12" nm="yyy" description="barfoo"/>
>    </reminders>

> I would like to sort the xml file based on the attribute
> which will be supplied as a parameter.

  If I understand correctly, the user give the name of an attribute as
parameter, and this name is used to identify the attribute to use to
sort.  For example:

  If it is correct, you can look at the following.  Be aware this is
not namespace-aware!  Depending on your requirements and how you launch
the transformation, you can use a xs:QName instead of a xs:string.

    (drkm)[119] ~/xslt/tests$ cat lp.xsl
    <xsl:stylesheet
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
        xmlns:xs="http://www.w3.org/2001/XMLSchema";
        version="2.0">

      <xsl:output omit-xml-declaration="yes" indent="yes"/>

      <xsl:param name="sort.attribute.name" as="xs:string"
                 select="'description'"/>

      <xsl:template match="node()|@*">
        <xsl:copy>
          <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
      </xsl:template>

      <xsl:template match="reminders">
        <xsl:copy>
          <xsl:apply-templates select="reminder">
            <xsl:sort select="
                @*[local-name() eq $sort.attribute.name]"/>
          </xsl:apply-templates>
        </xsl:copy>
      </xsl:template>

    </xsl:stylesheet>

    (drkm)[120] ~/xslt/tests$ cat lp.xml
    <reminders>
      <reminder category="Test" nm="XXX" description="foobar"/>
      <reminder category="Test12" nm="yyy" description="barfoo"/>
      <reminder category="01Test" nm="xxx" description="foofoo"/>
    </reminders>

    (drkm)[121] ~/xslt/tests$ saxon lp.xml lp.xsl
    <reminders>
       <reminder category="Test12" nm="yyy" description="barfoo"/>
       <reminder category="Test" nm="XXX" description="foobar"/>
       <reminder category="01Test" nm="xxx" description="foofoo"/>
    </reminders>

    (drkm)[122] ~/xslt/tests$ saxon lp.xml lp.xsl \
                                sort.attribute.name=nm
    <reminders>
       <reminder category="Test" nm="XXX" description="foobar"/>
       <reminder category="01Test" nm="xxx" description="foofoo"/>
       <reminder category="Test12" nm="yyy" description="barfoo"/>
    </reminders>

    (drkm)[123] ~/xslt/tests$ saxon lp.xml lp.xsl \
                                sort.attribute.name=category
    <reminders>
       <reminder category="01Test" nm="xxx" description="foofoo"/>
       <reminder category="Test" nm="XXX" description="foobar"/>
       <reminder category="Test12" nm="yyy" description="barfoo"/>
    </reminders>

    (drkm)[124] ~/xslt/tests$

  Regards,

--drkm
























	

	
		
___________________________________________________________________________ 
Yahoo! Mail riinvente le mail ! Dicouvrez le nouveau Yahoo! Mail et son interface rivolutionnaire.
http://fr.mail.yahoo.com

Current Thread