[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] sorting by comparing two nodes


Subject: [xsl] sorting by comparing two nodes
From: Claus Nagel <claus.nagel@xxxxxxxxx>
Date: Mon, 19 Dec 2005 22:05:03 +0100

Hi,
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>

This is a comma-separated list of 2d points. Each point consists of a
x-coordinate and a y-coordinate, parted by a white-space.

And I have a reference point, eg.

<refpoint>3 2</refpoint>

Now I want to sort the list of points. To do so I have to compare two
points and compute the following:
result = (P1.x - P0.x)*(P2.y - P0.y) - (P2.x - P0.x)*(P1.y - P0.y)

(P0 -> reference point,
 P1 -> one point of the list,
 P2 -> another point of the list)

According to the value of "result", P1 and P2 shall be sorted as follows:

 result < 0: P1 goes before P2
 result > 0: P2 goes before P1
 result = 0: no sorting required

Assume the following template:
<xsl:template name="sort">
  <xsl:with-param name="refpoint"/>
  <xsl:with-param name="$points"/>
    <xsl:for-each select="tokenize($points,',')">
      <xsl:sort select="."/>
      ......
    </xsl:for-each>
</xsl:template>

I can't compare two points within <xsl:sort/> the way I have to, can
I? How can I solve my sorting problem using xslt?

Thanks for any ideas,
Claus


Current Thread