<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
	<!ENTITY nbsp "&#160;">
]>
<!--<?altova_samplexml file:///C:/projects/topaz/prdDealValue_lay.xml?>
--><?altova_samplexml file:///C:/projects/topaz/report_lay.xml?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:sv="http://www.tibco.com/gi/print" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:tibf="http://www.tibco.com/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xsl:param name="datasource_param" as="xs:string" required="yes"/>
	<xsl:variable name="datasource">
		<xsl:choose>
			<xsl:when test="string-length($datasource_param) > 128">
				<xsl:copy-of select="$datasource_param"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:copy-of select="document($datasource_param)"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<xsl:variable name="debug" select="0"/>
	<xsl:variable name="sysFields" select="'jsxtext@jsxid@included@jsximg@maskName@required@uneditable@totstyle@type@jsxvalue@jsxopen@jsxinvalid@jsxstyle@jsxtypeid@jsxdisabled@summarytype@optional@jsxinvalid@optionalBillTo@referenceId@rate@jsxtip'"/>
	<xsl:function name="tibf:nvl" as="xs:string">
		<xsl:param name="value" as="xs:string?"/>
		<xsl:param name="defValue" as="xs:string"/>
		<xsl:choose>
			<xsl:when test="not($value) or normalize-space($value)='' ">
				<xsl:value-of select="$defValue"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$value"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:function>
	<xsl:template match="layout">
		<html>
			<head>
				<title class="title">Report</title>
				<style type="text/css">
              .tableArea{
                      border: solid #C0C0FF;
                      border-width: 0px 0px 0px 0px;
                      background-color: #E0E0FF;
              }
              .tableHeader_td{
                      padding: 0px 5px 0px 5px;
<!--                      text-align: right;-->
                      border: solid #C0C0FF;
                      border-width: 0px 1px 1px 0px;
                      background-color: #E0E0FF;
                      height: 24px;
											font-name: Verdana; font-size: 12px; font-weight:bold;
              }
              .value_td{
                      padding: 0px 5px 0px 5px;
                      border: solid #C0C0D0;
                      border-width: 0px 1px 1px 0px;
                      background-color: #FCFCFF;
                      height: 24px;
                      width: 0px;
											font-name: Verdana; font-size: 12px; font-weight:;
              }
              .left_td{
                      padding: 0px 5px 0px 5px;
                      text-align: left;
                      border: solid #C0C0D0;
                      border-width: 0px 1px 1px 0px;
                      background-color: #E4E4FF;
                      height: 24px;
											font-name: Verdana; font-size: 12px; font-weight:;
              }
              .section_hdr{
                      padding: 0px 5px 0px 5px;
                      text-align: left;
                      border: solid #C0C0D0;
                      border-width: 0px 1px 1px 0px;
                      background-color: #D0D0F0;
                      height: 24px;
					  font-family: Verdana; font-size: 14px; font-weight: bold;
               }
						  .section_tbl{
											background-color: #C0C0D0;
											border: solid #C0C0D0;
                      border-width: 0px 0px 0px 1px;
                      border-style: none;
							}
						  .area_tbl{
											background-color: #E0E0FF;
                      border-width: 0px 1px 1px 0px;
                      border-style: none;
							}
              .table_title{
                      padding: 0px 5px 0px 5px;
                      text-align: left;
                      border: solid #C0C0D0;
                      border-width: 0px 1px 1px 0px;
                      background-color: #A0A0E0;
                      height: 24px;
											font-family: Verdana; font-size: 12px; font-weight: bold;
               }
              .copyright_td{
                      text-align: center;
											font-family: Verdana; font-size: 10px; font-weight: bold;
               }
              .maintitle_td{
                      text-align: center;
											font-family: Verdana; font-size: 20px; font-weight: bold;
               }
				</style>
			</head>
			<body onload="self.window.focus()" oncontextmenu="return false">
				<table border="{$debug}" cellpadding="0" cellspacing="0" align="left" id="bd_dv" width="800px" style="border-style:none">
					<tr>
						<td class="maintitle_td" height="64" align="center">Report</td>
					</tr>
					<xsl:for-each select="*">
						<tr>
							<td>
								<xsl:apply-templates select="."/>
							</td>
						</tr>
					</xsl:for-each>
					<tr>
						<td height="64" class="copyright_td">Copyright Tibco. All rights reserved</td>
					</tr>
				</table>
				<!-- #EndTemplate -->
			</body>
		</html>
	</xsl:template>
	<!-- 
************************************************************************************************************************
-->
	<!-- 
************************************************************************************************************************
-->
	<xsl:template match="section">
		<table id="{@id}Section" border="{@border}" align="left" cellpadding="0" cellspacing="0" width="100%" class="section_tbl" style="border-style: none; {@style}">
			<tr>
				<td class="section_hdr" width="100%" align="left">
					<xsl:value-of select="./@text"/>
				</td>
			</tr>
			<tr>
				<td>
					<table id="{@id}SectionItems" border="0" cellpadding="0" cellspacing="0" width="100%" style="border-style:none">
						<xsl:for-each select="*">
							<tr>
								<td>
									<xsl:apply-templates select="."/>
								</td>
							</tr>
						</xsl:for-each>
					</table>
				</td>
			</tr>
		</table>
	</xsl:template>
	<xsl:template match="area">
<!--		<xsl:message>[area] <xsl:value-of select="@id"/>
		</xsl:message>
-->		<xsl:choose>
			<xsl:when test="@type='table'">
				<xsl:call-template name="tableAreaTemplate">
					<xsl:with-param name="node" select="."/>
				</xsl:call-template>
			</xsl:when>
			<xsl:when test="@type='hsplit'">
				<table id="{@id}HArea" border="0" align="left" cellpadding="0" cellspacing="0" width="100%" class="area_tbl" style="border-style: none">
					<tr>
						<xsl:for-each select="*">
							<td valign="top" width="50%">
								<xsl:apply-templates select="."/>
							</td>
						</xsl:for-each>
					</tr>
				</table>
			</xsl:when>
			<xsl:otherwise>
				<table id="{@id}Area" border="0" align="left" cellpadding="0" cellspacing="0" width="100%" class="area_tbl" style="border-style: none">
					<xsl:for-each select="*">
						<tr>
							<td valign="top">
								<xsl:apply-templates select="."/>
							</td>
						</tr>
					</xsl:for-each>
				</table>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<xsl:template match="record">
		<xsl:variable name="field" select="."/>
		<xsl:variable name="datasourceName" select="ancestor-or-self::node()[@datasource]/@datasource"/>
		<xsl:variable name="cdf" select="$datasource/datasource/data[@name=$datasourceName]"/>
		<xsl:if test="count($cdf) = 0">
			<xsl:message select="concat(count($datasource/datasource/data), ' datasources')"/>
			<xsl:message select="concat(count($cdf), ' CDF nodes')"/>
			<xsl:message terminate="yes" select="concat('Datasource ''', $datasourceName, ''' not found')"/>
		</xsl:if>				
				
		<xsl:variable name="dataNode" select="$cdf//record[@jsxid = $field/@id]"/>
		<table border="0" cellpadding="0" cellspacing="0" width="100%">
			<tr>
				<xsl:if test="not(@notitle)">
					<td class="left_td" align="left" width="100px">
						<xsl:value-of select="tibf:nvl(tibf:nvl($dataNode/@jsxtext, string($field/@text)),$field/@id)"/>
					</td>
				</xsl:if>
				<td class="value_td" width="100%" align="left">
					<xsl:value-of select="tibf:nvl($dataNode/@jsxvalue, '&nbsp;')"/>
				</td>
			</tr>
		</table>
	</xsl:template>
	<xsl:template name="tableAreaTemplate">
		<xsl:param name="node"/>
		<table id="{$node/@id}TableArea" border="0" cellpadding="0" cellspacing="0" width="100%" class="tableArea">
			<xsl:if test="@text">
				<tr>
					<td class="table_title" width="100%" align="left" colspan="{count($node/columns/column) + number(@rowtitles='1')}">
						<xsl:value-of select="./@text"/>
					</td>
				</tr>
			</xsl:if>
			<tr>
				<xsl:if test="@rowtitles='1'">
					<td class="tableHeader_td" width="100%">&nbsp;</td>
				</xsl:if>
				<xsl:for-each select="$node/columns/column">
					<td class="tableHeader_td" width="100%">
						<xsl:if test="@align">
							<xsl:attribute name="align" select="@align"/>
						</xsl:if>
						<xsl:value-of select="tibf:nvl(@text, @id)"/>
					</td>
				</xsl:for-each>
			</tr>
			<xsl:for-each select="record">
				<xsl:variable name="layoutNode" select="."/>
				<xsl:variable name="datasourceName" select="ancestor-or-self::node()[@datasource]/@datasource"/>
				<xsl:variable name="cdf" select="$datasource/datasource/data[@name=$datasourceName]"/>
				<xsl:variable name="dataNode" select="$cdf//record[@jsxid=$layoutNode/@id]"/>
				<tr>
					<xsl:if test="$node/@rowtitles='1'">
						<td class="left_td" align="left">
							<xsl:value-of select="$layoutNode/@id"/>
						</td>
					</xsl:if>
					<xsl:for-each select="$node/columns/column">
						<xsl:variable name="columnNode" select="."/>
						<xsl:variable name="data" select="$dataNode/@*[local-name() = $columnNode/@id]"/>
						<td class="value_td">
							<xsl:if test="$columnNode/@align">
								<xsl:attribute name="align" select="$columnNode/@align"/>
							</xsl:if>
							<xsl:if test="$dataNode/@totstyle">
								<xsl:attribute name="style" select="$dataNode/@totstyle"/>
							</xsl:if>
							<xsl:value-of select="tibf:nvl($data,'&nbsp;')"/>
						</td>
					</xsl:for-each>
				</tr>
			</xsl:for-each>
		</table>
	</xsl:template>
</xsl:stylesheet>
