<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:xxx="urn:my-namespace">
	<xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>

	<msxsl:script language="VBScript" implements-prefix="xxx">

		' ***********************************************************************************************************************
		' Global variables
		' ***********************************************************************************************************************
		Dim CellPrecision(10)
		Dim CellDataType(10)
		
		Function StoreCellPrecision( ByVal Column, ByVal Precision, ByVal DataType  )
			StoreCellPrecision = 1
			CellPrecision(Column) = Precision
			CellDataType(Column) = DataType
		End Function
		
		Function GetCellType( ByVal Column )
			GetCellType = CellDataType( Column )
		End Function

		Function GetCellPrecision( ByVal Column )
			GetCellPrecision = CellPrecision( Column )
		End Function
		
		
	</msxsl:script>

	<xsl:param name="Title"/>
	
	<xsl:template match="/*">
		<xsl:if test="string-length($Title) > 0">
			<xsl:text>&#xa;</xsl:text>
			<xsl:value-of select="$Title"/>
			<xsl:text>&#xa;</xsl:text>
		</xsl:if>
		<xsl:apply-templates/>
	</xsl:template>
	
	<xsl:template match="FieldList">
		<xsl:text>&#xa;</xsl:text>
		<xsl:variable name="TheTitle">
			<xsl:value-of select="Title"/>
		</xsl:variable>			
		<xsl:if test="string-length($TheTitle)>0">
			<xsl:value-of select="$TheTitle"/>
			<xsl:text>...</xsl:text>
			<xsl:text>&#xa;</xsl:text>
		</xsl:if>
		<xsl:apply-templates select="./Field"/>
		<xsl:apply-templates select="./Table"/>
	</xsl:template>

	<xsl:template match="Table">
		<xsl:if test="count(.//Row)>0">
			<xsl:text>&#xa;</xsl:text>
			<xsl:text>&#32;</xsl:text>
			<xsl:text>[</xsl:text>
			<xsl:value-of select="Title"/>
			<xsl:text>]</xsl:text>
			<xsl:text>&#xa;</xsl:text>
			<xsl:apply-templates select="Columns"/>
			<xsl:apply-templates select="Rows"/>
		</xsl:if>
	</xsl:template>

	<xsl:template match="Columns">
		<xsl:text>&#32;</xsl:text>
		<xsl:text>&#32;</xsl:text>
			
		<xsl:for-each select="Column">
		
			<xsl:variable name="Type" select="@type"/>
			<xsl:choose>
				<xsl:when test="$Type = 'Float'">
					<xsl:variable name="Precision" select="@precision"/>
					<xsl:variable name="Temp" select="xxx:StoreCellPrecision( position(), number($Precision), string($Type) )"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:variable name="Precision" select="0"/>
					<xsl:variable name="Temp" select="xxx:StoreCellPrecision( position(), 0, string($Type) )"/>
				</xsl:otherwise>
			</xsl:choose>
			
			<xsl:value-of select="@name"/>
			
			<xsl:variable name="TheUnits" select="@units"/>
			<xsl:if test="string-length($TheUnits)>0">
				<xsl:text>(</xsl:text>
				<xsl:value-of select="@units"/>
				<xsl:text>)</xsl:text>
			</xsl:if>
			
			<xsl:text>&#32;</xsl:text>
			
		</xsl:for-each>

		<xsl:text>&#xa;</xsl:text>
		
	</xsl:template>

	<xsl:template match="Rows">
		<xsl:for-each select="Row">

			<xsl:text>&#32;</xsl:text>
			<xsl:number format="1."/>
			<xsl:text>&#32;</xsl:text>

			<xsl:for-each select="Cell">
				<xsl:variable name="Column" select="position()"/>
				<xsl:call-template name="Format-Cell">
					<xsl:with-param name="Type" select="xxx:GetCellType( $Column )"/>
					<xsl:with-param name="Precision" select="xxx:GetCellPrecision( $Column )"/>
				</xsl:call-template>
				<xsl:text>&#32;</xsl:text>
			</xsl:for-each>
			
			<xsl:text>&#xa;</xsl:text>
			
		</xsl:for-each>
	</xsl:template>

	<xsl:template match="Field">
		<xsl:text>&#32;</xsl:text>
		<xsl:value-of select="@name"/>
		<xsl:text>:</xsl:text>
		<xsl:text>&#32;</xsl:text>
		<xsl:call-template name="Format-Value"/>
		<xsl:text> </xsl:text>
		<xsl:value-of select="@units"/>
		<xsl:text>&#xa;</xsl:text>
	</xsl:template>
	
	<xsl:template name="Format-Value">
	
		<xsl:variable name="Type" select="@type"/>
		<xsl:variable name="Precision" select="@precision"/>
		<xsl:variable name="Value" select="."/>

		<xsl:choose>
			<xsl:when test="$Type='Int'">
				<xsl:value-of select="format-number($Value,'0')"/>
			</xsl:when>
			<xsl:when test="$Type='Float'">
				<xsl:variable name="NumberFormat">
					<xsl:choose>
						<xsl:when test="$Precision=0">
							<xsl:text>0</xsl:text>
						</xsl:when>
						<xsl:when test="$Precision=1">
							<xsl:text>0.0</xsl:text>
						</xsl:when>
						<xsl:when test="$Precision=2">
							<xsl:text>0.00</xsl:text>
						</xsl:when>
						<xsl:otherwise>
							<xsl:text>0.000</xsl:text>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:variable>					
				<xsl:choose>
					<xsl:when test="contains($Value,',')">
						<xsl:value-of select="$Value"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="format-number($Value,$NumberFormat)"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:when test="$Type='String'">
				<xsl:value-of select="$Value"/>
			</xsl:when>
		</xsl:choose>
		
	</xsl:template>

	<xsl:template name="Format-Cell">
	
		<xsl:param name="Type"/>
		<xsl:param name="Precision"/>
		<xsl:variable name="Value" select="."/>

		<xsl:choose>
			<xsl:when test="$Type='String'">
				<xsl:value-of select="$Value"/>
			</xsl:when>
			<xsl:when test="$Type='Int'">
				<xsl:choose>
					<xsl:when test="$Value = 'Initial'">
						<xsl:value-of select="$Value"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="number($Value)"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:when test="$Type='Float'">
				<xsl:variable name="NumberFormat">
					<xsl:choose>
						<xsl:when test="$Precision=0">
							<xsl:text>0</xsl:text>
						</xsl:when>
						<xsl:when test="$Precision=1">
							<xsl:text>0.0</xsl:text>
						</xsl:when>
						<xsl:when test="$Precision=2">
							<xsl:text>0.00</xsl:text>
						</xsl:when>
						<xsl:otherwise>
							<xsl:text>0.000</xsl:text>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:variable>					
				<xsl:choose>
					<xsl:when test="$Value = 'Initial'">
						<xsl:value-of select="$Value"/>
					</xsl:when>
					<xsl:when test="contains($Value,',')">
						<xsl:value-of select="$Value"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="format-number($Value,$NumberFormat)"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
		</xsl:choose>
		
	</xsl:template>

</xsl:stylesheet>
