xsl-list
[Top] [All Lists]

RE: generic sort based on attribute names

2005-01-23 03:19:45
hi Joris, 
thanks for the response. But the collective string lengths are not the same. 
In fact I just gave an example here. I have no a priori knowledge of what the 
XML looks like. All I know is that the 'concatenated name-value pairs for all 
attributes sorted' needs to be the key for sorting. 
Thanks 


-----Original Message-----
From: Joris Gillis [mailto:roac(_at_)pandora(_dot_)be]
Sent: Sun 1/23/2005 1:42 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] generic sort based on attribute names
 
Tempore 04:23:21, die 01/23/2005 AD, hinc in  
xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com scripsit Chaitanya Desai  
<cdesai(_at_)syncata(_dot_)com>:

Suppose
<root>
 <e b="bb" y="yy"/>
 <e z="zz" a="aa"/>
</root>
is the XML I want to sort.
The result of the sort should be
<root>
  <e a="aa" z="zz" />
  <e b="bb" y="yy" />
</root>
Thus the attributes within an element are sorted and then the key used
for sorting elements would be:
'az' and 'by' respectively (thus 'az' < 'by').
Hi,

Changes are rather tiny, but if all attribute names happen to have the  
same collective string length (like in the example:  
length('az')=length('by')), you could use something like this:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";  
version="1.0">

<xsl:output method="xml" indent="yes"/>

<xsl:template match="root">
        <xsl:variable name="sortkey">
                <xsl:apply-templates select="e" mode="sortkey"/>
        </xsl:variable>
        <xsl:copy>
                <xsl:apply-templates select="e" >
                        <xsl:sort select="substring($sortkey,position()*2 - 1, 
2)"/>
                </xsl:apply-templates>
        </xsl:copy>
</xsl:template>

<xsl:template match="e" mode="sortkey">
        <xsl:apply-templates select="@*" mode="sortkey">
                <xsl:sort select="." data-type="string"/>
        </xsl:apply-templates>
</xsl:template>

<xsl:template match="@*" mode="sortkey">
        <xsl:value-of select="local-name()"/>
</xsl:template>

<xsl:template match="e">
        <xsl:copy>
                <xsl:apply-templates select="@*" >
                        <xsl:sort select="." data-type="string"/>
                </xsl:apply-templates>
        </xsl:copy>
</xsl:template>

<xsl:template match="@*">
        <xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>


regards,
-- 
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
Deserta faciunt et innovationem appelant

--~------------------------------------------------------------------
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>
--~--