all groups > dotnet xml > march 2004 >
You're in the

dotnet xml

group:

Custom sorting through xslt extension object


Custom sorting through xslt extension object Jiho Han
3/29/2004 11:41:26 AM
dotnet xml: I am trying to find information/example on how to do a custom sorting via
adding an extension object to the transform.
The requirement for the custom sorting is based on my assumption that nodes
that contain values such as datetime value will not sort correctly by the
facilities provided by the stanard <xsl:sort>. Let me know if that is not
the case as well.

Thanks!
Jiho

Re: Custom sorting through xslt extension object Jiho Han
3/29/2004 4:15:48 PM
I don't have a source xml but let me give you a scenario.
Let's say we have an xml like:

<persons>
<person>
<lastname>Smith</lastname>
<firstname>John</firstname>
<favoritecolor>red</favoritecolor>
</person>
<person>
<lastname>Doe</lastname>
<firstname>Jane</firstname>
<favoritecolor>blue</favoritecolor>
</person>
<person>
<lastname>Cooke</lastname>
<firstname>Robin</firstname>
<favoritecolor>yellow</favoritecolor>
</person>
</persons>

Then say we want to sort the above by <favoritecolor> element values.
However, the sorting mechanism won't be alphabetical. For <favoritecolor>,
proper ascending order is from warmest color to the coolest color: red,
orange, yellow, green, blue, indigo, purple, etc. As you can see, I came up
with the sorting order - based on rainbow colors. Really, the desired
sorting order may be defined by the application arbitrarily. I am guessing
that this kind of sorting can only be achieved by extension objects. Let me
know if there is an example similar to the given scenario or any other
resources that may be usuful for achieving the disired effects.

Thanks
Jiho

[quoted text, click to view]

Re: Custom sorting through xslt extension object Dimitre Novatchev [MVP XML]
3/29/2004 8:40:56 PM
So where is your source xml document?

What is the desired result?

What are the desired properties of the transformation?


Please, be specific.



Cheers,

Dimitre Novatchev [XML MVP],
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html


[quoted text, click to view]

Re: Custom sorting through xslt extension object Dimitre Novatchev [MVP XML]
3/30/2004 9:22:26 AM

[quoted text, click to view]

Yes, see for example "Date Sorting in pure XSLT"
at: http://www.topxml.com/code/default.asp?p=3&id=v20021212030100

As for generic sort in XSLT, see "Sorting with unknown number of sort keys "
at:

http://www.biglist.com/lists/xsl-list/archives/200303/msg00007.html


Cheers,

Dimitre Novatchev [XML MVP],
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html

Re: Custom sorting through xslt extension object Jiho Han
3/30/2004 9:55:01 AM
Thank you Dimitre.
Those solutions are quite nice and the second, quite complex, to be honest,
for me. However, I still fail to see how they would solve scenarios where
you cannot control the format or the nature of the sort keys. For example,
in your first solution, you know you're going to be processing the data
containing a certain datetime format. However, in many cases, this is not
possible. There are possibly mutiples of tens of possibilities in how a
datetime object may be formatted and doing something like "substring(str, 5,
3)" won't be adequate.

Even if a problem could be resolved with such a solution as your second
example, it might be cleaner in my opinion, to wrap the sorting mechanism in
an extension object, epecially when you know you will only be providing a
solution on a certain platform - in this case, Microsoft. Do you know of an
example where an extension object is used instead?

In any case, you have been most helpful and I can't appreciate enough how
skillful you are with xpath/xslt.
Thank you very much.

Jiho

[quoted text, click to view]

Re: Custom sorting through xslt extension object Dimitre Novatchev [MVP XML]
3/30/2004 6:21:31 PM

[quoted text, click to view]


Hi Jiho,

I am glad my reply has been helpful.

It seems to me that your probem is that you do not have a clear definition
of your problem.

Such "problems" are not defined correctly and, due to this, usually do not
have a solution.

One very generic solution is to use Higher Order Functions as in the second
example I provided.

Higher Order Functions are functions, which accept functions as their
parameters. In this way one can have a generic sort that will sort any set
of items of some totally ordered type. The caller of the generic sort knows
what is the type of the items to be sorted. The caller passes as a parameter
a function, which can correctly "compare" any two items and return 1 if
item1 > item2.

The generic sort uses this passed-as-parameter-compare-function and the work
is done successfully.

This approach, once understood, is not difficult to follow and is very
powerful.

For more information on the implemetation of HOF in XSLT I recommend the
following articles:

http://fxsl.sourceforge.net/articles/FuncProg/Functional%20Programming.html

and

http://www.idealliance.org/papers/extreme03/xslfo-pdf/2003/Novatchev01/EML2003Novatchev01.pdf


As for using an extension for sorting, I would not reccomend it without
paying special attention to the dangers of possible side-effects.

Trying to avoid side effects is the main reason why EXSLT doesn't specify
any sort functions.


Cheers,

Dimitre Novatchev [XML MVP],
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html


AddThis Social Bookmark Button