473,395 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Custom Sorting

Is there a way to sort by element names in XSLT, but not in ascending or
descenting order but by elements that I specify?

<root>
<cat />
<dog />
<rabbit />
</root>

the sorted result tree

<root>
<rabbit />
<cat />
<dog />
</root>
Jul 20 '05 #1
4 2883
"Mike King" <em*****@excite.com> wrote in message news:<10*************@corp.supernews.com>...
Is there a way to sort by element names in XSLT, but not in ascending or
descenting order but by elements that I specify?

<root>
<cat />
<dog />
<rabbit />
</root>

the sorted result tree

<root>
<rabbit />
<cat />
<dog />
</root>


If I understand what you're trying to do, I believe you can do it in
XSLT 1.0 if your processor supports exsl:node-set() or a similar
extension (see www.exslt.org):

<xsl:variable name="my-sort-order">
<rabbit/>
<cat/>
<dog/>
</xsl:variable>

<xsl:template match="root">
<xsl:for-each select="*">

<xsl:sort select="count(exsl:node-set($my-sort-order)/*[name() =
name(current())]/preceding-sibling::*)" data-type="number"
order="ascending"/>

<xsl:copy-of select="."/>

</xsl:for-each>
</xsl:template>

--
Robin Johnson
rj at robinjohnson dot f9 dot co dot uk -
http://www.robinjohnson.f9.co.uk
Jul 20 '05 #2
> If I understand what you're trying to do, I believe you can do it in
XSLT 1.0 if your processor supports exsl:node-set() or a similar
extension (see www.exslt.org):

<xsl:variable name="my-sort-order">
<rabbit/>
<cat/>
<dog/>
</xsl:variable>

<xsl:template match="root">
<xsl:for-each select="*">

<xsl:sort select="count(exsl:node-set($my-sort-order)/*[name() =
name(current())]/preceding-sibling::*)" data-type="number"
order="ascending"/>

<xsl:copy-of select="."/>

</xsl:for-each>
</xsl:template>

--
Robin Johnson
rj at robinjohnson dot f9 dot co dot uk -
http://www.robinjohnson.f9.co.uk


It worked! Thanks for your help.

How did you come up with this solution? I'm having a difficult time with
XSLT and I'm wondering how did you come up with this interesting solution.
Does one need to read the XSLT spec or is there a real good book that
explains XSLT. I consider myself a very technical person (I know several
programming languages) but XSLT just kicks my butt. If no one answered my
question, I was going to re-write my XSLT in C# or C++ to get the job the
done. Thanks again for your help.
Jul 20 '05 #3
"Mike King" <em*****@excite.com> wrote in message news:<10*************@corp.supernews.com>...

[programmer-defined sort order in XSLT 1.0 using exsl:node-set()]

It worked! Thanks for your help.
Glad to hear it.
How did you come up with this solution? I'm having a difficult time with
XSLT and I'm wondering how did you come up with this interesting solution.
A diseased mind and a lot of drugs. :-)

Tip 1: use exsl:node-set(). It makes XSLT *much* more powerful. In particular,
it's the only way you can 'sweep' twice over the same bit of source XML, i.e.
apply one template to a piece of the source, then apply a second template to
the result of that first template (the tree-shaped result, not just its
string-value.) The decision in the XSLT 1.0 spec not to allow XPath operations
on a tree-fragment variable "to allow for this to be implemented in future
versions" strikes me as truly bizarre. Thankfully XSLT 2.0 will do away with
that. (XSLT 2.0 also has a programmer-defineable sort order construct, I
believe. Michael Kay's Saxon at www.saxonica.com implements the current
working draft of 2.0, but I'm shy of working drafts so I'm sticking with 1.0
for the time being and using exsl:node-set a lot.)

Tip 2: don't be afraid to do clever things that look inefficient. If your data
is XML rather than strings, XSLT is *fast*. The only real exception I have
found to this is that in both the implementations I have used (MSXML and
Saxon), sweeping down the following-sibling:: axis is much faster than using
preceding-sibling:: (e.g. when you're trying to find the first unique value of
something - finding the last will be faster.)
Does one need to read the XSLT spec or is there a real good book that
explains XSLT.
The lovely Michael Kay strikes again: "XSLT Programmer's Reference", in the
"Programmer to Programmer" series by Wrox Press. The documentation on his site
is also very helpful.
I consider myself a very technical person (I know several
programming languages) but XSLT just kicks my butt.


"Assignment statement considered harmful."

--
Robin Johnson
rj at robinjohnson dot f9 dot co dot uk
http://www.robinjohnson.f9.co.uk

Only joking about the drugs.
Jul 20 '05 #4
Robin Johnson wrote:
"Mike King" <em*****@excite.com> wrote in message news:<10*************@corp.supernews.com>...

How did you come up with this solution? I'm having a difficult time with
XSLT and I'm wondering how did you come up with this interesting solution.


A diseased mind and a lot of drugs. :-)


Fortunately, there's not (yet) a software patent on that technique.
Does one need to read the XSLT spec or is there a real good book that
explains XSLT.


The lovely Michael Kay strikes again: "XSLT Programmer's Reference", in the
"Programmer to Programmer" series by Wrox Press. The documentation on his site
is also very helpful.


At the risk of posting a worthless "me too" post, I would absolutely
second this. IMHO, it is one of the very few XSLT books worth the
price. (I haven't yet found another, but my statement allows that one
might exist.)
I consider myself a very technical person (I know several
programming languages) but XSLT just kicks my butt.


"Assignment statement considered harmful."


Kay's corollary: "for-each usually a mistake"

Ed

Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Marri Suliez | last post by:
Has anyone found some documentation on how to properly subclass a ListBox control and provide custom sorting (when the list items come from a DataSource)? The only way I can figure out how to do...
0
by: Chris Mayers | last post by:
I have a Windows Forms DataGrid that has a DataView as a datasource. My problem is that I want the datagrid to exhibit some special sorting properties when the header rows are clicked on. From...
1
by: melanieab | last post by:
Hi, I found an article that explains how to do the custom sorting. Here is what it says: .... Perhaps you want some custom sorting method to take effect. To do that, DataGrid provides you with...
3
by: Brian Henry | last post by:
How would you go about doing this I have a tree view which where anything is a parent it is a folder (folder icon, and the tag for the object is a string that says "FOLDER:{name of folder...
0
by: Roy | last post by:
Hey all, I must be losing my touch. I have made many pages in the 1.1 framework that utilize custom bidirectional paging in datagrids. We've converted over to 2.0 and I've been trying to use the...
4
by: Ambica Jain | last post by:
Hi, I want custom sorting on some of the columns in the datagrid. And i am able to do the same by overriding MouseDown event. However, i need to rebind my datatable to reflect the changes in...
0
by: Kiran_Juikar | last post by:
I want custom sorting on some of the columns in the datagrid. And i am able to do the same by overriding MouseDown event. Sorting image (little triangle on the column header) goes away as I am not...
1
by: WebMatrix | last post by:
Hi, I started a new web application which so far has several grdiviews displaying data. I find myself reimplementing the same logic (copy/pasting really) in grid's event ItemDataBound and...
2
by: Aamir Ghanchi | last post by:
hi, I have a gridview which is bound to ObjectDataSource which in turn reads from a business objects methods for custom paged data. When PageIndex is set to any value other than 0 and one of the...
0
by: =?Utf-8?B?QVZM?= | last post by:
Hi, I've a requirement in which I want to use the custom sorting.. I mean I've a dropdown with sortable column names and when the user selects the particular column from dropdown , I need to sort...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.