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">
	  <tr>
	    <xsl:choose>
		  <!-- If it's the first record, fill in column 1 -->
		  <xsl:when test="position()=1">
		    <td><xsl:value-of select="CUST"/></td>
		  </xsl:when>
		  <!-- 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>
		  </xsl:when>
		  <!-- Otherwise its the same customer, output an empty cell in column 1 -->
		  <xsl:otherwise>
		    <td></td>
		  </xsl:otherwise>
	    </xsl:choose>
	    <td><xsl:value-of select="VALUE"/></td>
	  </tr>
  </xsl:for-each>
</table>
</xsl:template>

</xsl:stylesheet>

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

Cust 1 1
2
Cust 2 3
4
Cust 3 5
6
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact