﻿<?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>

	<!-- main template -->
	<xsl:template match="text()|@*"/>
	
	<xsl:template match="/">
		<nitf version="-//IPTC//DTD NITF 3.6//EN" change.date="September 11, 2009" change.time="12:26">
			<head>
				<!--<title>HKN-Sums-(Away Team name)-(Home Team Name)</title>-->
				<title>HKO-Sums-<xsl:value-of select="/root/groups/group/dataContainers/dataContainer[@type='8']/dataBlocs/dataBloc[@name='Header' and @order='0']/dataList/data[@name='Equipe2_ter']/value/text()" />-<xsl:value-of select="/root/groups/group/dataContainers/dataContainer[@type='8']/dataBlocs/dataBloc[@name='Header' and @order='0']/dataList/data[@name='Equipe1_ter']/value/text()" />
				</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>HKO-Sums</hl1>
						<hl2>HKO-Sums</hl2>
					</hedline>
					<byline>
						<person/>
						<byttl>SoHO Sports</byttl>
					</byline>
					<dateline>
						<location>
							<city/>
							<state/>
							<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">
						<xsl:apply-templates />
					</block>
				</body.content>
			</body>
		</nitf>
	</xsl:template>	

	<xsl:template match="dataContainers">

		<xsl:variable name="goaliesHeader">
			<xsl:apply-templates name="selectRaw" mode="selectRaw" select="//dataBloc/styleContainer/styles/style[@name='StyleSheet']/value[text() = 'goaliesHeader']/../../../../outputs/output[@type='Raw']" />
		</xsl:variable>

		<xsl:variable name="goalies">
			<xsl:value-of select="//dataBloc/styleContainer/styles/style[@name='StyleSheet']/value[text() = 'goalies']/../../../../outputs/output[@type='Raw']" />
			<xsl:value-of select="//dataBloc/styleContainer/styles/style[@name='StyleSheet']/value[text() = 'goalies2']/../../../../outputs/output[@type='Raw']" />
		</xsl:variable>

		<xsl:variable name="shotsongoalHeader">
			<xsl:apply-templates name="selectRaw" mode="selectRaw" select="//dataBloc/styleContainer/styles/style[@name='StyleSheet']/value[text() = 'shotsongoalHeader']/../../../../outputs/output[@type='Raw']" />
		</xsl:variable>

		<xsl:variable name="shotsongoal">
			<xsl:element name="table">
				<xsl:attribute name="border">1</xsl:attribute>
				<xsl:attribute name="cellSpacing">1</xsl:attribute>
				<xsl:attribute name="cellPadding">1</xsl:attribute>
				
				<xsl:element name="tbody">	
					<xsl:apply-templates name="statistics" mode="statistics" select="//dataBloc/styleContainer/styles/style[@name='StyleSheet']/value[text() = 'shotsongoal']/../../../../outputs/output[@type='Web']/value" />
				</xsl:element>				
			</xsl:element>
			<xsl:element name="br"></xsl:element>
		</xsl:variable>

		<xsl:variable name="powerplays">
			<xsl:apply-templates name="selectRaw" mode="selectRaw" select="//dataBloc/styleContainer/styles/style[@name='StyleSheet']/value[text() = 'powerplaysHeader']/../../../../outputs/output[@type='Raw']" />
			<xsl:value-of select="//dataBloc/styleContainer/styles/style[@name='StyleSheet']/value[text() = 'powerplays']/../../../../outputs/output[@type='Raw']" />
			<xsl:value-of select="//dataBloc/styleContainer/styles/style[@name='StyleSheet']/value[text() = 'powerplays2']/../../../../outputs/output[@type='Raw']" />
		</xsl:variable>

		<xsl:variable name="referee">
			<xsl:apply-templates name="selectRaw" mode="selectRaw" select="//dataBloc/styleContainer/styles/style[@name='StyleSheet']/value[text() = 'referee']/../../../../outputs/output[@type='Raw']" />
		</xsl:variable>

		<xsl:variable name="linesman">
			<xsl:apply-templates name="selectRaw" mode="selectRaw" select="//dataBloc/styleContainer/styles/style[@name='StyleSheet']/value[text() = 'linesman']/../../../../outputs/output[@type='Raw']" />
		</xsl:variable>

		<xsl:variable name="attendance">
			<xsl:apply-templates name="selectRaw" mode="selectRaw" select="//dataBloc/styleContainer/styles/style[@name='StyleSheet']/value[text() = 'attendance']/../../../../outputs/output[@type='Raw']" />
			<xsl:apply-templates name="selectRaw" mode="selectRaw" select="//dataBloc/styleContainer/styles/style[@name='StyleSheet']/value[text() = 'locale_itl']/../../../../outputs/output[@type='Raw']" />
		</xsl:variable>

		<!-- EVENTS -->
		<xsl:apply-templates />
		
		<p>
			<xsl:value-of select="$shotsongoalHeader" disable-output-escaping="yes" />
		</p>
		<xsl:copy-of select="$shotsongoal" disable-output-escaping="yes"/>
		
		<p>
			<xsl:value-of select="$goaliesHeader" disable-output-escaping="yes" />
			<xsl:copy-of select="$goalies" disable-output-escaping="yes" />
		</p>
		<p>
			<xsl:copy-of select="$powerplays" disable-output-escaping="yes" />
		</p>
		<p>
			<xsl:value-of select="$referee" disable-output-escaping="yes" />
		</p>
		<p>
			<xsl:value-of select="$linesman" disable-output-escaping="yes" />
		</p>
		<p>
			<xsl:value-of select="$attendance" disable-output-escaping="yes" />
		</p>

	</xsl:template>
	
	<!-- NAMED TEMPLATES (stats and headers) -->
	<xsl:template name="statistics" mode="statistics" match="value">
		<xsl:element name="tr">
			<xsl:apply-templates mode="statisticsLine" select="dataCell" />
		</xsl:element>
	</xsl:template>

	<xsl:template name="statisticsLine" mode="statisticsLine" match="element">
		<xsl:element name="td">
			<xsl:value-of select="text()" />
		</xsl:element>
	</xsl:template>
	
	<xsl:template name="goalies" mode="goalies" match="value">		
		<xsl:value-of select="." />
	</xsl:template>

	<xsl:template name="selectRaw" mode="selectRaw" match="value">
		<!--<xsl:value-of select="." />-->
		<xsl:call-template name="text-replacer">
			<xsl:with-param name="text-to-replace"><xsl:value-of select="." disable-output-escaping="yes" /></xsl:with-param>
		</xsl:call-template>
	</xsl:template>
	<!-- END NAMED TEMPLATES -->
	
	<!-- RECURSIVE TEMPLATES (events) -->
	<xsl:template match="dataContainer">
		<xsl:choose>
			<!-- header line -->
			<xsl:when test="@type = '2' or @type = '3' or @type = '4'">
				<p>
					<xsl:value-of select="./dataBlocs/dataBloc/dataList/data/value" />
				</p>
			</xsl:when>
			<xsl:when test="@type = '5' or @type = '6' or @type = '8'">
				<xsl:apply-templates />
			</xsl:when>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="dataBlocs">
		<xsl:apply-templates />
	</xsl:template>

	<xsl:template match="dataBloc">	
		<xsl:choose>
			<xsl:when test="styleContainer/styles/style[@name='StyleSheet']/value[text() != 'shotsongoal'] 
						and styleContainer/styles/style[@name='StyleSheet']/value[text() != 'shotsongoalHeader'] 
						and styleContainer/styles/style[@name='StyleSheet']/value[text() != 'goalies'] 
						and styleContainer/styles/style[@name='StyleSheet']/value[text() != 'goaliesHeader'] 
						and styleContainer/styles/style[@name='StyleSheet']/value[text() != 'powerplaysHeader']
						and styleContainer/styles/style[@name='StyleSheet']/value[text() != 'powerplays']
						and styleContainer/styles/style[@name='StyleSheet']/value[text() != 'referee'] 
						and styleContainer/styles/style[@name='StyleSheet']/value[text() != 'linesman'] 
						and styleContainer/styles/style[@name='StyleSheet']/value[text() != 'attendance']
						and styleContainer/styles/style[@name='StyleSheet']/value[text() != 'locale_itl']">
				<p><xsl:apply-templates select="./outputs/output[@type='Raw']" /></p>
			</xsl:when>
			<xsl:when test="styleContainer/styles/style[@name='StyleSheet']/value = ''">
				<xsl:variable name="content">
					<xsl:apply-templates select="./outputs/output[@type='Raw']" />
				</xsl:variable>
				<xsl:if test="$content != ''">
					<p><xsl:copy-of select="$content"></xsl:copy-of></p>
				</xsl:if>
			</xsl:when>
		</xsl:choose>		
	</xsl:template>

	<!--
	<xsl:template match="dataCell">
		<xsl:apply-templates />
	</xsl:template>
	-->
	<!-- END RECURSIVE TEMPLATES -->

	<xsl:template match="value">
		<xsl:call-template name="text-replacer">
			<xsl:with-param name="text-to-replace"><xsl:value-of select="." /></xsl:with-param>
		</xsl:call-template>
	</xsl:template>
	
	<xsl:template name="text-replacer">
		<xsl:param name="text-to-replace"/>
		<xsl:variable name="myVar">
			<xsl:call-template name="string-replace-all">
				<xsl:with-param name="text" select="$text-to-replace" />
				<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" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

</xsl:stylesheet>