Re: [xsl] sorting by comparing two nodes

Subject: Re: [xsl] sorting by comparing two nodes
From: Ragulf Pickaxe <ragulf.pickaxe@xxxxxxxxx>
Date: Tue, 20 Dec 2005 08:39:56 +0100
> 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

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>

I hope this helps,
Ragulf Pickaxe :-)

Current Thread