xsl-list
[Top] [All Lists]

Re: [xsl] dynamic sort using attributes

2006-12-05 03:09:40
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 réinvente le mail ! Découvrez le nouveau Yahoo! Mail et son 
interface révolutionnaire.
http://fr.mail.yahoo.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>