<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method = "html" />

<xsl:template match = "/">
   <html>
      <head>
         <title>Word Frequency Results For: <xsl:value-of select = "/documentCount/subject" /></title>
         
         <link rel="stylesheet" href="main.css" />
      </head>
      <body style = "background-image: url(raindrops.gif); 
         background-position: center; background-attachment: fixed;">
         
         <table align = "center" border = "0" cellpadding = "10" width = "100%" 
            style = "border-color: yellow; background-color: white" >
            
            <tr><td>
               <a name = "top" id = "top" ><h1>Results</h1></a>
                     
               <xsl:apply-templates select = "documentCount" mode = "Q1" />
               
               <div class = "blockheader">
      				Links
   				</div>
   				
   				<p style = "text-indent: 0%;">
   					<a href = "#alphabetical">Alphabetical Order</a><br />
   					<a href = "#frequency">Frequency Order</a><br />
   				</p>

   				<p style = "text-indent: 0%;">
   					<a href = "../results.html#subcat">Back to Counted Word Results Page</a>  
   				</p>
               
               <xsl:apply-templates select = "documentCount" mode = "Q2" />
               <xsl:apply-templates select = "documentCount" mode = "Q3" />

            </td></tr>
         </table>
      </body>
   </html>
</xsl:template>

<xsl:template match = "documentCount" mode = "Q1">
   <div class = "blockheader">
      Quick Stats for this XML document
   </div>

   <p align = "center">
   	<b>Language: </b> 
      <i>
         <xsl:value-of select="/documentCount/language" /> 
      </i>
   </p>
   
   <p align = "center">
   	<b>Subject: </b> 
      <i>
         <xsl:value-of select="/documentCount/subject" /> 
      </i>
   </p>
      
   <p align = "center">
      This XML document was created on 
      <i>
         <xsl:value-of select="/documentCount/dateCreated" /> 
      </i>
   </p>
   
   <p align = "center">
      Number of Words in this XML document   
      <i>
         <xsl:variable name = "countWords" select = "count(dictionary/word)" />
         <xsl:value-of select="format-number($countWords, '#,###,##0')" /> 
      </i>
   </p>
   
   <p align = "center">
   	Total frequency in this document: 
   	<i>
   		<xsl:variable name = "totalWords" select = "sum(dictionary/word/freq)" />
   		<xsl:value-of select="format-number($totalWords, '#,###,##0')" />
   	</i>
   </p>
   
   <p align = "center">
   	Total overall frequency (words counted): 
   	<i>
   		<xsl:value-of select="format-number(/documentCount/totalWordsCounted, '#,###,##0')" />
   	</i>
   </p>
   
	<p align = "center">
   	Meaning that this document contains approximately  
   	<i>
   		<xsl:variable name = "percentageTotal" select = "sum(dictionary/word/freq) div /documentCount/totalWordsCounted * 100" />
   		<xsl:value-of select="format-number($percentageTotal, '##0.0')" /> % 
   	</i>
   	of all the words counted in the project.
   </p>
   
   <p class = "new" align = "center">
   	(Words with a frequency of less than 2 are not displayed in this XML file.)
   </p>
</xsl:template>

<xsl:template match = "documentCount" mode = "Q2">

   <div class = "blockheader">
      <a name = "alphabetical" id = "alphabetical">Words in Alphabetical Order</a>
   </div>
   <br />
   
   <table width = "95%" align = "center" border = "1" cellspacing = "1" cellpadding = "4" style = "font-size: 80%;">
      <thead>
         <tr>
            <th width = "20%">Word</th>
            <th width = "40%">Frequency</th>
            <th width = "40%">Probabitiy (%) -To six Decimal places</th>
         </tr>
      </thead>
      <xsl:apply-templates select = "dictionary" mode = "Q2" />
   </table>

   <p align = "center">
   	<a href = "#top">Return to the top of the page</a>
   </p>

   <hr />
</xsl:template>


<xsl:template match = "dictionary" mode = "Q2">
   <tbody>
      <xsl:for-each select = "word">
         <xsl:sort select = "./@word" />
         <tr>
            <td>
               <xsl:value-of select="./@word" />
            </td>
            <td align = "right">
               <xsl:value-of select="format-number(freq, '#,###,##0')" />
            </td>
            <td align = "right">
               <!-- <xsl:value-of select="percentage" /> % -->
               <xsl:variable name = "percent" select = "freq div /documentCount/totalWordsCounted * 100" />
   				<xsl:value-of select="format-number($percent, '##0.000000')" /> %
            </td>
         </tr>  
      </xsl:for-each>
   </tbody>
</xsl:template>

<xsl:template match = "documentCount" mode = "Q3">

   <div class = "blockheader">
      <a name = "frequency" id = "frequency">Words in Frequency Order</a>
   </div>
   <br />
   
   <table width = "95%" align = "center" border = "1" cellspacing = "1" cellpadding = "4" style = "font-size: 80%;">
      <thead>
         <tr>
            <th width = "20%">Word</th>
            <th width = "40%">Frequency</th>
            <th width = "40%">Probabitiy (%) -To six Decimal places</th>
         </tr>
      </thead>
      <xsl:apply-templates select = "dictionary" mode = "Q3" />
   </table>

   <p align = "center">
   	<a href = "#top">Return to the top of the page</a>
   </p>

   <hr />
</xsl:template>


<xsl:template match = "dictionary" mode = "Q3">
   <tbody>
      <xsl:for-each select = "word">
         <xsl:sort select = "freq" data-type = "number" order = "descending" />
      	<xsl:sort select = "./@word" />
         <tr>
            <td>
               <xsl:value-of select="./@word" />
            </td>
            <td align = "right">
               <xsl:value-of select="format-number(freq, '#,###,##0')" />
            </td>
            <td align = "right">
               <!-- <xsl:value-of select="percentage" /> % -->
               <xsl:variable name = "percent" select = "freq div /documentCount/totalWordsCounted * 100" />
   				<xsl:value-of select="format-number($percent, '##0.000000')" /> %
            </td>
         </tr>
      </xsl:for-each>
   </tbody>
</xsl:template>

<xsl:template match = "*|text()" />

</xsl:stylesheet>
