<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes"/> 
	
	<!-- A few Constants -->
	<xsl:variable name="BYTES_PER_PAGE" select="/TraceData/Memory/PageSize/@Bytes" />
	<xsl:variable name="BYTES_PER_MB" select="1024 * 1024" />
	<xsl:variable name="BYTES_PER_KB" select="1024" />
	
  <xsl:template match="/*">
	<xsl:call-template name="ProcessNode">
		<xsl:with-param name="Node" select="."/>
	</xsl:call-template>
  </xsl:template>
  
  <xsl:template name="ProcessNode">
		<xsl:param name="Node" />
		<xsl:copy>
		
			  <!-- Grab the attributes -->
		      <xsl:for-each select="@*">
				<xsl:choose>
					<xsl:when test="name(.)='Pages'">
						<xsl:choose>
							<!-- If it is a descendent of /TraceData/Memory/Summary convert it to MB for the overview page -->
							<xsl:when test="/TraceData/Memory/Summary//@Pages[generate-id(.) = generate-id(current())]">
								<xsl:attribute name="MB">
									<!-- We mult by 10^3  then div again by 10^3  so we can round to 3 decimal places -->
									<xsl:value-of select="format-number((round ((. * $BYTES_PER_PAGE * 1000) div $BYTES_PER_MB) ) div 1000, '0.###')"/>
								</xsl:attribute>
							</xsl:when>
							<!-- If it is a descendent of anything else convert it to KB for the details pages -->
							<xsl:otherwise>
								<xsl:attribute name="KB">
									<!-- We mult by 10^3  then div again by 10^3  so we can round to 3 decimal places -->
									<xsl:value-of select="format-number((round ((. * $BYTES_PER_PAGE * 1000) div $BYTES_PER_KB) ) div 1000, '0.###')"/>
								</xsl:attribute>
							</xsl:otherwise>
						</xsl:choose> 
					</xsl:when>
					<xsl:otherwise>
						<xsl:copy/>
					</xsl:otherwise>
				</xsl:choose> 
			  </xsl:for-each>

			  <!-- Grab the nodes -->
		      <xsl:for-each select="$Node/*">
			    <xsl:call-template name="ProcessNode">
					<xsl:with-param name="Node" select="."/>
				</xsl:call-template>
			  </xsl:for-each>
			  
		</xsl:copy>
  </xsl:template>
</xsl:stylesheet>


