Jump to content

Xslt Sort


sajesh

Recommended Posts

I have an XML record to sort according to the latest date (of format mm/dd/yy) and I got the results with the following code<xsl:for-each select="...."> <xsl:sort select="substring(dateValue,7,2)" order="descending"/> <xsl:sort select="substring(dateValue,3,2)" order="descending"/> <xsl:sort select="substring(dateValue,1,2)" order="descending"/> <xsl:value-of select="dateValue"/></xsl:for-each>But its required to filter the results further in a way only top 10 results from the above sort needed on the html page. It would be a great help if I can get some advise to go forward. Thanks in advance.

Link to comment
Share on other sites

  • 2 weeks later...

It's actually very difficult to accomplish what you want to do using exclusively XSLT directives. What you can do is use a little bit of JavaScript, though, to make sure that you are only grabbing the top ten results.

<script type="text/javascript">	 var i=0;	 var array=new Array();					 <xsl:for-each select="...">		  <xsl:sort select="substring(dateValue,7,2)" order="descending"/>		  <xsl:sort select="substring(dateValue,3,2)" order="descending"/>		  <xsl:sort select="substring(dateValue,1,2)" order="descending"/>		  array[i] = <xsl:value-of select="dateValue"/>;		  i++;	 </xsl:for-each></script>

So then, you have all your results in the array, and all you have to do is loop through it, stopping once the tenth iteration is reached.

	 for(int j=0;j<10;j++)	 {		  // this is where you output to html		  document.getElementById("someElement").innerHTML=array[j];	 }

In my experience with XSLT and XML technologies, there is a close relationship with JavaScript, in that it needs to be used often to accomplish programmatic events.

Link to comment
Share on other sites

it's actually very easy to accomplish what you want to do using exclusively XSLT directives<xsl:for-each select="...."> <xsl:sort select="substring(dateValue,7,2)" order="descending"/> <xsl:sort select="substring(dateValue,3,2)" order="descending"/> <xsl:sort select="substring(dateValue,1,2)" order="descending"/> <xsl:if test="position() <= 10"> <xsl:value-of select="dateValue"/> </xsl:if> </xsl:for-each>

	<xml id="xmlSource">		<data>			<date>11/01/09</date>			<date>01/01/09</date>			<date>02/01/09</date>			<date>03/01/09</date>			<date>04/01/09</date>			<date>05/01/09</date>			<date>06/01/09</date>			<date>07/01/09</date>			<date>08/01/09</date>			<date>09/01/09</date>			<date>10/01/09</date>			<date>11/01/09</date>			<date>01/01/10</date>			<date>02/02/10</date>			<date>03/01/10</date>			<date>04/01/10</date>		</data>	</xml>	<xml id="xslStyle">		<xsl:stylesheet	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">			<xsl:output method="html" />			<xsl:template match="/">					<xsl:for-each select="//date">						<xsl:sort select="concat(substring(.,7,2),substring(.,3,2),substring(.,1,2))" order="descending"/>						<xsl:if test="position() <= 10">							<xsl:value-of select="."/><br/>						</xsl:if>					</xsl:for-each>			</xsl:template>		</xsl:stylesheet>	</xml>produces this result:04/01/1003/01/1002/02/1001/01/1011/01/0911/01/0910/01/0909/01/0908/01/0907/01/09

p.s. In my experience with XSLT and XML technologies, there is virtually never a need to invoke in-line scripts.

Link to comment
Share on other sites

  • 2 months later...

Archived

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

×
×
  • Create New...