<?xml version="1.0"?>
<!--
split_tcx.xsl v1.00
by Dave Horlick
2008-08-01

Catalogues a Garmin Training Center exported TCX file, or splits out a single Id from it based on a provided TargetId parameter.

released under a GNU GPL 2.0 license
http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
-->

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
		xmlns:tcx="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2">

<xsl:param name="TargetId" />

<xsl:output method="xml" indent="yes" encoding="UTF-8"/>

<xsl:template match="/">
	<xsl:if test="not($TargetId) or $TargetId=''">
		<xsl:message>
To filter to a particular Id, provide a TargetId parameter. Here is a catalogue of Id's in the file:
		</xsl:message>
	</xsl:if>

	<xsl:apply-templates select="tcx:TrainingCenterDatabase"/>
</xsl:template>

<xsl:template match="tcx:TrainingCenterDatabase">
	<xsl:element name="tcx:TrainingCenterDatabase">
		<xsl:apply-templates select="tcx:Activities"/>
	</xsl:element>
</xsl:template>

<xsl:template match="tcx:Activities">
	<xsl:element name="tcx:Activities">
		<xsl:apply-templates select="tcx:Activity"/>
	</xsl:element>
</xsl:template>

<xsl:template match="tcx:Activity">
	<xsl:choose>
		<xsl:when test="not($TargetId) or $TargetId=''">
		
			<xsl:element name="tcx:Activity">
				<xsl:if test="@Sport">
					<xsl:attribute name="Sport">
						<xsl:value-of select="@Sport" />
					</xsl:attribute>
				</xsl:if>
				<xsl:apply-templates select="tcx:Id,tcx:Lap"/>
			</xsl:element>

		</xsl:when>
		<xsl:otherwise>
			<xsl:if test="tcx:Id=$TargetId">
				<xsl:copy-of select="." />
			</xsl:if>
		</xsl:otherwise>
	</xsl:choose>

</xsl:template>

<xsl:template match="tcx:Lap">
	<xsl:element name="tcx:Lap">
		<xsl:if test="@StartTime">
			<xsl:attribute name="StartTime">
				<xsl:value-of select="@StartTime" />
			</xsl:attribute>
		</xsl:if>
	</xsl:element>
</xsl:template>

<xsl:template match="tcx:Id">
	<xsl:element name="tcx:Id">
		<xsl:apply-templates />
	</xsl:element>
</xsl:template>
   
</xsl:stylesheet>