<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                       xmlns:set="http://exslt.org/set" version="1.0" 
                       xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
	 			   extension-element-prefixes="set" set:doc="http://www.exslt.org/set">
	
	<xsl:output encoding="utf-8" method="html"/>
	<xsl:include href="Style1.xsl"/>
	
	<!-- Create variables to hold each of the set of elements that you wish to get the intersection from -->
	<xsl:variable name="ProductA" select="//customer[count(purchase[@type = 'ProductA' and @quantity > 10]) = 1]"/>
	<xsl:variable name="ProductB" select="//customer[count(purchase[@type = 'ProductB' and @quantity > 10]) = 1]"/>
	
	
	<!-- Template for root rule -->
	<xsl:template match="/">
		<!-- Set Formatting Characteristics -->
		<xsl:call-template name="Style1"/>
		<xsl:apply-templates/>
	</xsl:template>
	
	
	<xsl:template match="customers">

		<h1>Customers who purchased 10 or more of  Product A and Product B</h1>

		<xsl:variable name="ProductsAandB">
			<xsl:call-template name="set:intersection">
				<xsl:with-param name="nodes1" select="$ProductA"/>
				<xsl:with-param name="nodes2" select="$ProductB"/>
			</xsl:call-template>
		</xsl:variable>

		<!-- Table Header Creation -->
		<table border="1">
			<tr>
				<th>Name</th>
				<th>Quantity</th>
				<th>Type</th>
			</tr>
			<xsl:for-each select="msxsl:node-set($ProductsAandB)/customer">
				<xsl:variable name="name" select="@name"/>
				<xsl:variable name="howmany" select="sum(purchase[@type=&apos;ProductA&apos;]/@quantity)"/>
				<xsl:for-each select="purchase">
					<tr>
						<td><xsl:value-of select="$name"/></td>
						<td><xsl:value-of select="@quantity"/></td>
						<td><xsl:value-of select="@type"/></td>
					</tr>
				</xsl:for-each>
			</xsl:for-each>
			<!-- End of Table -->
		</table>
	</xsl:template>


	<!-- The following template was created by Jeni Tennison of                        -->
	<!--  http://www.JeniTennison.com and is part of an extremely useful           -->
	<!--  collection of extension functions and templates at http://www.exslt.org -->
	<xsl:template name="set:intersection">
		<xsl:param name="nodes1" select="/.."/>
		<xsl:param name="nodes2" select="/.."/>
		<xsl:copy-of select="$nodes1[count(.|$nodes2) = count($nodes2)]"/>
	</xsl:template>


</xsl:stylesheet>

