Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: [xsl] dynamic sort using attributes

From: Florent Georges <darkman_spam@-------->
To:
Date: 12/5/2006 10:10:00 AM
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


transparent
Print
Mail
Digg
delicious
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent