﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:local="urn:local" extension-element-prefixes="msxsl">
	<xsl:output method="xml" indent="yes" />
	<msxsl:script language="CSharp" implements-prefix="local">
		public string dateTimeNow()
		{
		//String date = DateTime.Now.ToString("yyyyMMddTHHmmsszzz");
		String date = DateTime.Now.ToString("yyyyMMddTHH0000zzz");
		date = date.Replace(":", String.Empty);
		return date;
		}
	</msxsl:script>
	<!-- params -->
	<xsl:param name="LibelleGroupePage" />
	<xsl:param name="LibellePage" />
	<!-- main template -->
	<xsl:template match="text()|@*"></xsl:template>
	<xsl:template match="/">
		<nitf version="-//IPTC//DTD NITF 3.6//EN" change.date="September 11, 2009" change.time="12:26">
			<head>
				<title>
					<xsl:value-of select="$LibellePage" />
				</title>
				<!-- LIBELLE GROUPE PAGE / PAGE -->
				<tobject tobject.type="news">
					<tobject.property tobject.property.type="current" />
					<tobject.subject tobject.subject.ipr="IPTC" />
				</tobject>
				<docdata management-status="usable">
					<!--<doc-id id-string="090feaaa6d3144a6b576cb297878882f" />-->
					<doc-id />
					<doc.copyright holder="SoHO Sports. All rights reserved." year="2013" />
					<doc-scope scope="Agate" />
					<date.expire norm="YYYYMMDD000000-HHMM" />
					<xsl:variable name="dateTimeNow" select="local:dateTimeNow()" />
					<date.issue norm="{$dateTimeNow}" />
					<!--<date.issue norm="YYYYMMDD000000-HHMM" />-->
					<!-- DATE EXPORT -->
					<date.release norm="YYYYMMDD000000-HHMM" />
					<ed-msg info="" />
					<key-list>
						<keyword>Agate</keyword>
					</key-list>
					<series series.part="0" series.totalpart="0" />
				</docdata>
				<urgency ed-urg="5" />
			</head>
			<body>
				<body.head>
					<hedline>
						<hl1>
							<xsl:value-of select="$LibellePage" />
						</hl1>
						<hl2>
							<xsl:value-of select="$LibellePage" />
						</hl2>
					</hedline>
					<byline>
						<person></person>
						<byttl>SoHO Sports</byttl>
					</byline>
					<dateline>
						<location>
							<city></city>
							<state></state>
							<country></country>
						</location>
						<story.date norm="20120925T161006-0400" />
					</dateline>
					<distributor>SOHO SPORTS</distributor>
					<abstract>
						<media>
							<media-metadata name="TransRef" value="" />
						</media>
					</abstract>
				</body.head>
				<body.content>
					<block id="CPARTICLE">
						<!-- LEAGUE NAME-->
						<xsl:apply-templates mode="dataContainer" select="//dataContainer[@type='2']" />
						<!-- STANDINGS -->
						<xsl:apply-templates mode="dataContainer" select="//dataContainer[@type='3'or @type='4' or @type='6']" />
						<!-- FOOTER (could be replaced by end block) -->
						<xsl:if test="(//dataBloc[@name='Footer']/outputs/output[@type='Raw']/value)[1]">
							<p>
								<xsl:value-of disable-output-escaping="yes" select="(//dataBloc[@name='Footer']/outputs/output[@type='Raw']/value)[1]"></xsl:value-of>
							</p>
						</xsl:if>
						<!-- RESULTS -->
						<xsl:apply-templates mode="dataContainer" select="//dataContainer[@type='5'or @type='8' or @type='9']" />
					</block>
				</body.content>
			</body>
		</nitf>
	</xsl:template>
	<xsl:template name="dataContainers" mode="dataContainers" match="dataContainers">
		<xsl:apply-templates mode="dataContainer" />
	</xsl:template>
	<xsl:template name="dataContainer" mode="dataContainer" match="dataContainer">
		<xsl:choose>
			<!-- header line -->
			<xsl:when test="@type = '2' or @type = '3' or @type = '4'">
				<xsl:for-each select="./dataBlocs/dataBloc/outputs/output[@type='Web']/value/dataCell/element">
					<p>
						<b><xsl:value-of select="./text()" disable-output-escaping="yes" /></b>
					</p>				
				</xsl:for-each>
			</xsl:when>
			<!-- standings -->
			<xsl:when test="@type = '6'">
				<xsl:apply-templates mode="standing" select="dataBlocs" />
			</xsl:when>
			<!-- schedule -->
			<xsl:when test="@type = '5' or @type = '8'">
				<xsl:apply-templates mode="schedule" select="dataBlocs" />
			</xsl:when>
			<xsl:when test="@type = '9'">
				<tr>
					<td>
						<xsl:value-of select="./dataBlocs/dataBloc/outputs/output[@type='Raw']/value/text()" />
					</td>
				</tr>
			</xsl:when>
		</xsl:choose>
	</xsl:template>
	<xsl:template name="standing" mode="standing" match="dataBlocs">
		<table>
			<tbody>
				<xsl:apply-templates mode="standingLine" select="dataBloc[not(@name='Footer')]" />
			</tbody>
		</table>
	</xsl:template>
	<xsl:template name="standingLine" mode="standingLine" match="dataBloc">
		<tr>
			<xsl:apply-templates mode="standingLineCell" select="outputs/output[@type='Web']/value/dataCell" />
		</tr>
	</xsl:template>
	<xsl:template name="standingLineCell" mode="standingLineCell" match="dataCell">
		<td>
			<xsl:apply-templates mode="element" />
		</td>
	</xsl:template>
	<xsl:template name="schedule" mode="schedule" match="dataBlocs">
		<xsl:apply-templates mode="scheduleLine" />
	</xsl:template>
	<xsl:template mode="scheduleLine" match="dataBloc">
		<xsl:choose>
			<xsl:when test="@name='Break'">
				<!-- <p><bold><xsl:value-of select="outputs/output[@type='Raw']/value/text()" /></bold></p> -->
				<p><xsl:value-of select="outputs/output[@type='Raw']/value" disable-output-escaping="yes" /></p>
			</xsl:when>
			<xsl:otherwise>
				<p><xsl:apply-templates mode="scheduleLineCell" select="outputs/output[@type='Web']/value/dataCell" /></p>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<xsl:template mode="scheduleLineCell" match="dataCell">
		<xsl:apply-templates mode="element" />
	</xsl:template>
	<xsl:template name="element" mode="element" match="element">
		<xsl:variable name="myVar">
			<xsl:call-template name="string-replace-all">
				<xsl:with-param name="text" select="." />
				<xsl:with-param name="replace" select="'\n'" />
				<xsl:with-param name="by" select="''" />
			</xsl:call-template>
		</xsl:variable>
		<xsl:variable name="myVar2">
			<xsl:call-template name="string-replace-all">
				<xsl:with-param name="text" select="$myVar" />
				<xsl:with-param name="replace" select="'..'" />
				<xsl:with-param name="by" select="'.'" />
			</xsl:call-template>
		</xsl:variable>
		<xsl:value-of select="$myVar2" disable-output-escaping="yes" />
	</xsl:template>
	<xsl:template name="string-replace-all">
		<xsl:param name="text" />
		<xsl:param name="replace" />
		<xsl:param name="by" />
		<xsl:choose>
			<xsl:when test="contains($text, $replace)">
				<xsl:value-of select="substring-before($text,$replace)" />
				<xsl:value-of select="$by" />
				<xsl:call-template name="string-replace-all">
					<xsl:with-param name="text" select="substring-after($text,$replace)" />
					<xsl:with-param name="replace" select="$replace" />
					<xsl:with-param name="by" select="$by" />
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$text" disable-output-escaping="yes" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>