[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
How_To_XML_Display_XML_Min_Max_Values_XSLT

How to display minimum and maximum values in HTML table using XSLT?

This example shows how to sort a list of numbers and then display only the highest and lowest values in a table.

These values can be determined as the first and last elements of an already sorted list. If no order is specified, the default sorting is ascending ie. numbers will start from the smallest.

The first and last nodes's values can be displayed by using <xsl:if> and the position()  ?XPath function.

Create the XML document and link it to the XSL stylesheet.

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="sort_min_max.xsl"?>
<root> 
	<number> 
		<digit>5</digit> 
	</number> 
	<number> 
		<digit>3</digit> 
	</number> 
	<number> 
		<digit>6</digit> 
	</number> 
	<number> 
		<digit>1</digit> 
	</number> 
	<number> 
		<digit>2</digit> 
	</number> 
	<number> 
		<digit>7</digit> 
	</number> 
	<number> 
		<digit>4</digit> 
	</number> 
</root>


Create the XSL stylesheet.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:template match="/">
<html>
<head><title>Min - max sort</title>
</head>
<body>
<table width="20%" border="1">
 <THEAD>
  <TR>
   <TD width="50%"><B>Max</B></TD>
    <TD width="50%"><B>Min</B></TD>
  </TR>
 </THEAD> 
 <TBODY><TR>
  <xsl:for-each select="root/number">	
  <xsl:sort select="digit" order="descending" data-type="number" />	     
  <xsl:if test="position()=1"><TD><xsl:value-of select="."/></TD></xsl:if>
  <xsl:if test="position()=last()"><TD><xsl:value-of select="."/></TD></xsl:if>  
  </xsl:for-each></TR>
 </TBODY>
</table>
</body>
</html>
</xsl:template> 
</xsl:stylesheet>


<xsl:for-each select="root/number">


This finds all <number> elements contained within the <root> element.

<xsl:sort select="digit" order="descending" data-type="number" />


This sorts all <digit> elements starting from the highest to the lowest.

<xsl:if test="position()=1"><TD><xsl:value-of select="."/></TD></xsl:if>


If the element found is the first (highest) its value will be displayed in a table cell.

<xsl:if test="position()=last()"><TD><xsl:value-of select="."/></TD></xsl:if>


If the element found is the last (lowest) its value will be displayed in a table cell.

Related threads:
Trying to display selected data from xsl:sort

XML FAQ

last edited (April 19, 2004) by lillu, Number of views: 1930, Current Rev: 4 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.