<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


<xsl:template match="/">
  <html>
<meta HTTP-EQUIV="Content-Type" content="text/html; charset=UTF-8" />
  <body>
      <table border="1">
      <tr bgcolor="#9acd32"> 
        <th>Category</th>
        <th>Item Name</th>
        <th>Item id</th>
        <th>Description</th>
        <th>Serving Unit</th>
	<th>Serving Size</th>
        <th>Serving Unit</th>
        <th>Serving Size</th> 
        <th>Calories Total</th>
        <th>%DailyValue</th>
        <th>Protein</th>
        <th>%DailyValue</th>
        <th>Total Fat (grams)</th>
        <th>%DailyValue</th>
        <th>Carbohydrates (grams)</th>
        <th>%DailyValue</th>
        <th>Sodium (milligram)</th>
        <th>%DailyValue</th>
        <th>Calories From Fat</th>
        <th>%DailyValue</th>
        <th>Saturated Fat (grams)</th>
        <th>%DailyValue</th>
        <th>Transfatty Acid (grams)</th>
        <th>%DailyValue</th>
        <th>Cholesterol (milligram)</th>
        <th>%DailyValue</th>
        <th>Dietary Fiber (grams)</th>
        <th>%DailyValue</th>
        <th>Sugars (grams)</th>
        <th>%DailyValue</th>
        <th>Vitamin A (IU)</th>
        <th>%DailyValue</th>
        <th>Vitamin C (milligram)</th>
        <th>%DailyValue</th>
        <th>Calcium (milligram)</th>
        <th>%DailyValue</th>
        <th>Iron (milligram)</th>
        <th>%DailyValue</th>     
	<th>Small Image</th>  
	<th>Large Image</th>  
      </tr>
<!-- Loop through the menucategory node  -->
      <xsl:for-each select="itemdetails/menucategory">
<!-- declare and set the menu_category variable to the category value  -->
     <xsl:variable name="menu_category" select="@name"></xsl:variable>
<!-- Loop through the item node  -->
      <xsl:for-each select="item">
      <tr>
<!-- While looping through the item node, retrieve the local variable value-->
        <td><xsl:value-of select="$menu_category" /></td>            
        <td><xsl:value-of select="@name"/></td>
<!-- Retrieve the item id  -->
      <xsl:for-each select="id/@value">
        <td><xsl:value-of select="."/></td>
      </xsl:for-each>  
<!-- Retrieve the item description  -->
      <xsl:for-each select="description/@value">
        <td><xsl:value-of select="."/></td>
      </xsl:for-each>  




<!-- Retrieve the item serving size ounce & grams -->
      
      <xsl:for-each select="servingsize/unit/@name">  	   
            
	   <xsl:variable name="unitnamecount" select="position()" />
	   <xsl:if test="$unitnamecount = 1">
		<td><xsl:value-of select="."/></td>  
	   </xsl:if>
      </xsl:for-each> 

      <xsl:for-each select="servingsize/unit/@value"> 
	   <xsl:variable name="unitnamecount" select="position()" />
	   <xsl:if test="$unitnamecount = 1">
	      <td><xsl:value-of select="."/></td>
	   </xsl:if>
      </xsl:for-each>  

<!-- Block to check the serving size unit occurance, if it is 1 then creating a blank <td> -->      
      <xsl:variable name="unitcount">
	   <xsl:for-each select="servingsize/unit/@name">         
	      <xsl:value-of select="position()"/>	      
	 </xsl:for-each>
      </xsl:variable> 

<!-- Creating a blank TD for serving Unit 
      <xsl:if test="$unitcount = 1">-->
      <xsl:choose>
      <xsl:when test="$unitcount = 1">
         <td> N/A </td>	
	 <td> N/A </td>
      </xsl:when>
      <xsl:otherwise>
          <xsl:for-each select="servingsize/unit/@name">  	   
            
	     <xsl:variable name="unitnamecount" select="position()" />
	     <xsl:if test="$unitnamecount = 2">
		<td><xsl:value-of select="."/></td>  
	     </xsl:if>
          </xsl:for-each> 

      <xsl:for-each select="servingsize/unit/@value"> 
	   <xsl:variable name="unitnamecount" select="position()" />
	      <xsl:if test="$unitnamecount = 2">
	         <td><xsl:value-of select="."/></td>
	      </xsl:if>
           </xsl:for-each>  
      </xsl:otherwise>
      </xsl:choose>
      <!-- </xsl:if>
      End of Block -->

     

      



<!-- Retrieve the nutritional values  -->
      <xsl:for-each select="nutrients/nutrient/fact/@value">
        <td><xsl:value-of select="."/></td>
      </xsl:for-each>

<!-- Retrieving small Image Value -->

      <xsl:for-each select="images/small_image/@value">
        <td><xsl:value-of select="."/></td>
      </xsl:for-each> 

<!-- Retrieving Large Image Value -->

      <xsl:for-each select="images/large_image/@value">
        <td><xsl:value-of select="."/></td>
      </xsl:for-each> 


<!-- End retrieve the nutritional values  -->
      </tr>
<!-- End loop item node  -->        
      </xsl:for-each>
<!-- End loop menucategory node  -->  
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>


