<?xml version='1.0' encoding='utf-8' ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0">
<xsl:output method="html"/>
<xsl:include href="Style1.xsl" />

<!--
Step 1: Define the primary key to be used in the Muenchian grouping. The
beautiful thing about the xsl:key  element in our example is that once we
know the "region", we can easily find all of the products that match that region.
The xsl:key element (different from the key() function) is defined as follows:-->
<xsl:key name="products" match="product" use="region"/>

<!-- Template for our root rule -->
<xsl:template match="/">
	<xsl:apply-templates/>
</xsl:template>


<!-- Template for our "products" rule -->
<xsl:template match="products">
	<xsl:call-template name="Style"/>


	<h2>Grouping of Products by Region, then by Product Name </h2>

	

<!--Step 2: Loop through the unique regions (the primary key) in our document.  --> 	
<xsl:for-each select="//product[generate-id(.)=generate-id(key('products',region))]">

		<!-- Sort Primary key by name in ascending order -->
    	<xsl:sort select="name" order="ascending"/>   

    	<!-- Display the region as our table header -->
   		<h3><xsl:value-of select="region"/> region</h3>
    
 		<!--Display all nodes for a given region in a table-->
  		<table border="1">
      		<tr>
				<th>Product Name</th>
				<th>Price</th>
				<th>Region</th>			
			</tr>

    		 		 		 		 		
			<!-- For each value in our key collection for the given region,
			display values -->

     		<xsl:for-each select="key('products',region)">
<!--
The expression "key('products',region)" will return all of the "product"
elements from the key table whose "use=" expression defined in xsl:key
(see xsl:key at top)  evaluated to the same value as the "region" child
of the current element.  In the the example, we specified use="region".
If region has a value of "SouthWest", then all of the product elements from
the key table that contain a child element with a value of "SouthWest" will
be returned.
-->

				<!--Sort our secondary key, product nodes,  by name-->
      			<xsl:sort select="name"/> 
      			<tr>

					<td><xsl:value-of select="name"/></td>
					<td><xsl:value-of select="price"/></td>
					<td><xsl:value-of select="region"/></td>			
				</tr>
    		</xsl:for-each>
  		</table>
		<br/><br/>
	</xsl:for-each>
  
</xsl:template>

</xsl:stylesheet>
