Jump to content

numbering of sorted nodes


Guest ironman

Recommended Posts

Guest ironman

Hi,Here my problem.... I have a given XML document which is of this form:<root> <item1> <year> </year> ... </item1> <item1> <year> </year> ... </item1> <item1> <year> </year> ... </item1> <item2> <year> </year> ... </item2> <item1> <year> </year> ... </item1></root>The document is not sorted. I have to write an XSLT file which creates a list of a set of the items which is sorted by year and I need an index which starts at count(items) an goes down to 1. I already tried several things (count(followers), <xsl:number>, position). These thing all doesn't work, because they workon document order. After that I tried it recursive with a counter, but it didn't really work. Does anyone have an other idea?

Link to comment
Share on other sites

Here is a sample that might help, the stylesheet

<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0">    <xsl:output method="xml" indent="yes"/>  <xsl:strip-space elements="*"/>    <xsl:template match="@* | node()">	<xsl:copy>	  <xsl:apply-templates select="@* | node()"/>	</xsl:copy>  </xsl:template>    <xsl:template match="root">	<xsl:copy>	  <xsl:apply-templates select="item">		<xsl:sort select="year" data-type="number"/>	  </xsl:apply-templates>	</xsl:copy>  </xsl:template>    <xsl:template match="item">	<xsl:copy>	  <xsl:attribute name="index"><xsl:value-of select="last() - (position() - 1)"/></xsl:attribute>	  <xsl:apply-templates select="@* | node()"/>	</xsl:copy>  </xsl:template></xsl:stylesheet>

transforms the input

<root>  <item>	<year>2000</year>  </item>  <item>	<year>2005</year>  </item>  <item>	<year>1999</year>  </item>  <item>	<year>2002</year>  </item></root>

into the result

<root>   <item index="4">	  <year>1999</year>   </item>   <item index="3">	  <year>2000</year>   </item>   <item index="2">	  <year>2002</year>   </item>   <item index="1">	  <year>2005</year>   </item></root>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...