xsl-list
[Top] [All Lists]

[xsl] Re:dynamic sort using attributes

2006-12-05 06:41:37
I feel stupid for asking..I think I kind of figured this one out.
<!--- xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<items>
<sortBy>description</sortBy>
<item nm="b1" description="zzzzz" type="1"/>
<item nm="c1" description="yyyyy" type="1"/>
<item nm="b1" description="ababab" type="0"/>
<item nm="c1" description="abxccd" type="1"/>
</items>

<!--  xsl --->

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:fo="http://www.w3.org/1999/XSL/Format";
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:template match="/">
<xsl:variable name="dummy" select="string(*//sortBy)"/>
<xsl:value-of select="$dummy"/>
<xsl:for-each select="*//item">
<xsl:sort select="@*[name()=$dummy]" data-type="text"/>
<li><xsl:value-of select="@description"/></li>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

The above can be extended to pass in a data-type as well.

---------Original Post--------------
Date: Mon, 4 Dec 2006 23:51:53 -0600
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
From: "L P" <listaction(_at_)gmail(_dot_)com>
Subject: dynamic sort using attributes
Message-ID: 
<5b8815450612042151n4e3ffcfn903ec18556df8ee5(_at_)mail(_dot_)gmail(_dot_)com>

Folks,

I've tried searching the lists, but could not find a definitive /
clear answer to this question, so here it goes:

How can I sort an xml list based on attributes dynamically ( I do not
know which attribute before hand)?

For instance, I have the following xml:
<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.

So, assume that i have an xsl:param with the value of the attribute
that I would like the results to be sorted by.
I know that this can be accomplished if I change the xml structure and
make the attributes child elements, but that is not an option for me.

Is there any work around that can accomplish this without changing the
XSL file (before complie time)?

Also, I'm targetting user agents such as IE / Firefox that support XSL 1.0.

Thanks in advance

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