I want to generate a nice report using XML & XSL.

Peter Ciuffetti

The following stylesheet output rows of customer records in a table where the customer name is displayed in column 1 in only the first record for a given customer.

It uses the preceding::RECORD axis in the comparison, which gathers all of the RECORDs before the current RECORD being processed in the for-each loop. The [last()-1] predicate on the test gets the last one of these.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="RECORDSET">
<table border="1">
  <xsl:for-each select="RECORD">
		  <!-- If it's the first record, fill in column 1 -->
		  <xsl:when test="position()=1">
		    <td><xsl:value-of select="CUST"/></td>
		  <!-- If the last preceding record has a different customer, fill in column 1 -->
		  <xsl:when test="preceding::RECORD[last()-1] and string(preceding::RECORD[last()-1]/CUST)!=string(CUST)">
		    <td><xsl:value-of select="CUST"/></td>
		  <!-- Otherwise its the same customer, output an empty cell in column 1 -->
	    <td><xsl:value-of select="VALUE"/></td>


Given the example test data, the table produced looks like:

Cust 1 1
Cust 2 3
Cust 3 5