<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl" xmlns:html="http://www.w3.org/tr/rec-html40" result-ns="">
<xsl:template match="/">
<TABLE border='1'>
<THEAD>
<xsl:apply-templates select="/xml/s:Schema/s:ElementType[@name='row']/s:AttributeType"/>
</THEAD>
<TBODY CLASS="TableBody" id='tableBody'>
	<xsl:apply-templates select="/xml/rs:data/z:row" order-by="@*[2]"/>						
</TBODY>
</TABLE>
</xsl:template>
	
	
<xsl:template match="s:AttributeType">
	<xsl:choose>
		<xsl:when test="@rs:hidden" />
		<xsl:when test="/xml/layout/field[@name $ieq$ context(-1)/@name]/@visible[text() $ieq$ 'false']"/>
		<xsl:otherwise>
			<th>
				<xsl:if test="/xml/layout/field[@name $ieq$ context(-1)/@name]/@info" >
				<xsl:attribute name="title">
					<xsl:value-of select="/xml/layout/field[@name $ieq$ context(-1)/@name]/@info" />
				</xsl:attribute>
				</xsl:if>
				<xsl:choose>
				<xsl:when test="/xml/layout/field[@name $ieq$ context(-1)/@name]/@caption" >
					<xsl:value-of select="/xml/layout/field[@name $ieq$ context(-1)/@name]/@caption" />
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="@name" />
				</xsl:otherwise>
				</xsl:choose>
			</th>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>	

<xsl:template match="z:row">
<TR>
<xsl:for-each select="/xml/s:Schema/s:ElementType[@name='row']/s:AttributeType">
   	<xsl:choose>
		<xsl:when test="@rs:hidden" />
		<xsl:when test="/xml/layout/field[@name $ieq$ context(-1)/@name]/@visible[text() $ieq$ 'false']"/>
		<xsl:otherwise>
			<td nowrap='true'>
				<xsl:choose>
				<xsl:when test="s:datatype/@dt:type[text() = 'string']"/>
				<xsl:otherwise>
					<xsl:attribute name="align">right</xsl:attribute>
				</xsl:otherwise>				
				</xsl:choose>
				<xsl:apply-templates select="context(-2)/@*[nodeName()=context(-1)/@name]" />
				
			</td>
		</xsl:otherwise>
	</xsl:choose>
</xsl:for-each>
</TR>
</xsl:template>	
<xsl:template match="@*">
	<xsl:choose>
		<xsl:when test="/xml/layout/field[@name $ieq$ context(-2)/@name]/@format[text() $ieq$ 'currency']">
			<xsl:eval >getCurrency(this.text)</xsl:eval>
		</xsl:when>
		<xsl:when test="/xml/layout/field[@name $ieq$ context(-2)/@name]/@format[text() $ieq$ 'dateTime']">
			<xsl:eval >getDateTime(this.text)</xsl:eval>
		</xsl:when>
		<xsl:when test="/xml/layout/field[@name $ieq$ context(-2)/@name]/@format[text() $ieq$ 'date']">
			<xsl:eval >getDate(this.text)</xsl:eval>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="text()" />
		</xsl:otherwise>				
	</xsl:choose>
</xsl:template>

<xsl:script>
function getDateTime(value){
		temp = new Date();
		temp.setYear(value.substr(0,4));
		temp.setMonth(value.substr(5,2));
		temp.setDate(value.substr(8,2));
		temp.setHours(value.substr(11,2));
		temp.setMinutes(value.substr(14,2));
		temp.setSeconds(value.substr(17,2));
		return temp.toLocaleString(); 
}
function getCurrency(value){return formatNumber(value, "#,###.00 $");}
function getDate(value){
		temp = new Date();
		temp.setYear(value.substr(0,4));
		temp.setMonth(value.substr(5,2));
		temp.setDate(value.substr(8,2));
		return temp.toLocaleString().substr(0,10); 
}
</xsl:script>
</xsl:stylesheet>
