[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: sorting by comparing two nodes

Subject: Re: sorting by comparing two nodes
From: Ragulf Pickaxe <ragulf.pickaxe@xxxxxxxxx>
Date: Tue, 20 Dec 2005 08:39:56 +0100
javascript sort
> I'm stuck with a sorting problem and hope to find some help here :-)
>
> I have the following input:
>
> <points>10 13,20 34,2 10,40 67</points>

I am not all that clear on how you would sort these.
As far as I know, even in XSLT 2.0, you can only sort on a nodeset.

It seems to me that you want to sort on the comma separed list in the
points element.

My scetch of a solution is an XSLT 1.0, which is what I know. This may
inspire you to a 2.0 solution.

In a two-phase transformation, I would transform your vectors to a new XML
with:
<points>
  <point><x>10</x><y>13</y>
  <point><x>20</x><y>34</y>
  <point><x>2</x><y>10</y>
  <point><x>40</x><y>67</y>
</points>

This I would then transform to the output that you want.

Your expression seems more like the javaScript way of making a sort
algorithm. This is not necessary here.

You will need an expression that computes the distance from a given
point to the reference. The sorting function of XSLT will do the
comparison between different nodes:
expr:= sqrt((x-$ref/x)^2+(y-$ref/y)^2)

I do not know whether these functions (sqr and sqrt) are included in
XSLT 2.0. If not, you may want to look up FXSL to help you (also the
case if you use 1.0).

(I have also split the reference point into its X an Y parts)

Then you can have a template which gives:
<xsl:for-each select="point">
  <xsl:sort select="expr" data-type="number"/> <!-- Put the expression here
-->
  <xsl:value-of select="x"/><xsl:text> </xsl:text><xsl:value-of select="y"/>
  <xsl:if test="position()!=last()">,</xsl:if>
</xsl:for-each>

I hope this helps,
Ragulf Pickaxe :-)

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.