<?xml version = "1.0"?>
<xsl:transform version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">

<xsl:variable name = "existingDoc" select = "'balances.xml'"/>

<xsl:output method = "xml" indent = "yes" omit-xml-declaration = "no" media-type = "text/xml"/>

<xsl:template match="*|/"><xsl:apply-templates/></xsl:template>

<xsl:template match="text()|@*"><xsl:value-of select="."/></xsl:template>

<xsl:template match="*|/">
		<xsl:apply-templates select = "node()"/>
	</xsl:template>

<xsl:template match="text()|@*">
		<xsl:value-of select = "." disable-output-escaping = "no"/>
	</xsl:template>

<xsl:template match="/">    
		<xsl:processing-instruction name = "xml:stylesheet">
			<xsl:text disable-output-escaping = "no">href = 'balances.xsl' type = 'text/xsl'</xsl:text>
		</xsl:processing-instruction>
		<xsl:apply-templates select = "//TimeSheetDoc[1]"/>  
	</xsl:template>

<xsl:template match="TimeSheetDoc">    
		<xsl:copy>      
			<xsl:apply-templates select = "PayPeriod[1]"/>      
			<xsl:apply-templates select = "Employee[1]"/>      
			<xsl:apply-templates select = "TimeSheet[1]"/>    
		</xsl:copy>  
	</xsl:template>

<xsl:template match="*|@*|comment()|processing-instruction()|text()">    
		<xsl:copy>      
			<xsl:apply-templates select = "*|@*|comment()|processing-instruction()|text()"/>    
		</xsl:copy>  
	</xsl:template>

<xsl:template match="TimeSheet">
		<xsl:copy>
			<xsl:apply-templates select = "CodeSet" mode = "profile"/>
			<xsl:copy-of select = "document($existingDoc)//TimeSheet/CodeSet[@PayPeriodOnly='true']"/>
			<xsl:copy-of select = "document($existingDoc)//TimeSheet/CodeSet[@PayPeriodOnly = 'false' and ChargedTime and not(Code[@CodeClass = current()/CodeSet/Code/@CodeClass and @Code = current()/CodeSet/Code/@Code]/..)]"/>
			<xsl:copy-of select = "document($existingDoc)//TimeSheet/StartStopSet[StartStop]"/>
			<xsl:call-template name = "addClock">
				<xsl:with-param name = "profileItem" select = "//TimeSheet/StartStopSet[@Name='Leave'][1]"/>
				<xsl:with-param name = "howManyMore" select = "count(//TimeSheet/StartStopSet[@Name='Leave']) - count(document($existingDoc)//TimeSheet/StartStopSet[@Name='Leave' and StartStop])"/>
			</xsl:call-template>
			<xsl:call-template name = "addClock">
				<xsl:with-param name = "profileItem" select = "//TimeSheet/StartStopSet[@Name='ActualTour'][1]"/>
				<xsl:with-param name = "howManyMore" select = "count(//TimeSheet/StartStopSet[@Name='ActualTour']) - count(document($existingDoc)//TimeSheet/StartStopSet[@Name='ActualTour' and StartStop])"/>
			</xsl:call-template>
			<xsl:copy-of select = "Note"/>
		</xsl:copy>
	</xsl:template>

<xsl:template name="addClock">
		<xsl:param name = "profileItem"/>
		<xsl:param name = "howManyMore"/>
		<xsl:if test = "$howManyMore > 0">
			<xsl:copy-of select = "$profileItem"/>
			<xsl:call-template name = "addClock">
				<xsl:with-param name = "profileItem" select = "$profileItem"/>
				<xsl:with-param name = "howManyMore" select = "$howManyMore - 1"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>

<xsl:template match="CodeSet" mode="profile">
		<xsl:choose>
			<xsl:when test = "document($existingDoc)//TimeSheet/CodeSet/Code[@CodeClass = current()/Code/@CodeClass and @Code = current()/Code/@Code]/..">
				<xsl:copy-of select = "document($existingDoc)//TimeSheet/CodeSet/Code[@CodeClass = current()/Code/@CodeClass and @Code = current()/Code/@Code]/.."/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:copy-of select = "."/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

</xsl:transform>
