<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="text" indent="no" omit-xml-declaration="yes" />

	<!-- default column width  -->
	<xsl:variable name="f1FromFCLength">28</xsl:variable>
	<xsl:variable name="f1ToFCLength">28</xsl:variable>
	<xsl:variable name="partsReturnedLength">8</xsl:variable>
	<xsl:variable name="purchasePriceLength">8</xsl:variable>
	<xsl:variable name="f2FromFCLength">4</xsl:variable>
	<xsl:variable name="f2ToFCLength">4</xsl:variable>
	<xsl:variable name="f2ReturnPartsLength">8</xsl:variable>
	<xsl:variable name="f2ContMaintLength">11</xsl:variable>
	<xsl:variable name="f2MachTypeLength">7</xsl:variable>
	<xsl:variable name="f2ModelLength">5</xsl:variable>


	<!-- Make a grouping of FCTRANSACTIONs by FROMMACHTYPE-FROMMODEL-TOMACHTYPE-TOMODEL-HWFCCAT -->
	<xsl:key name="FCTRANSACTION-by-FROMMACHTYPE-FROMMODEL-TOMACHTYPE-TOMODEL-HWFCCAT" match="FCTRANSACTION" 
	use="concat(FROMMACHTYPE, '-', FROMMODEL, '-', TOMACHTYPE, '-', TOMODEL, '-', FROMFEATURE/HWFEATURE/HWFCCAT)" />

	<xsl:template name="FeatureConversion-Format1">
		<xsl:param name="sectionHeader" />
		<xsl:param name="f1FromFCLength" select="$f1FromFCLength" />
		<xsl:param name="f1ToFCLength" select="$f1ToFCLength" />
		<xsl:param name="partsReturnedLength" select="$partsReturnedLength" />
		<xsl:param name="price" />
		<xsl:param name="purchasePriceLength" select="$purchasePriceLength" />

		<xsl:value-of select="$sectionHeader" />

		<!-- group report by FROMMACHTYPE-FROMMODEL-TOMACHTYPE-TOMODEL-HWFCCAT -->
		<xsl:for-each select="FCTRANSACTION[count(. | key('FCTRANSACTION-by-FROMMACHTYPE-FROMMODEL-TOMACHTYPE-TOMODEL-HWFCCAT', 
		concat(FROMMACHTYPE, '-', FROMMODEL, '-', TOMACHTYPE, '-', TOMODEL, '-', FROMFEATURE/HWFEATURE/HWFCCAT))[1]) = 1]">
			<xsl:sort select="FROMMACHTYPE" />
			<xsl:sort select="FROMMODEL" />
			<xsl:sort select="TOMACHTYPE" />
			<xsl:sort select="TOMODEL" />
			<xsl:sort select="FROMFEATURE/HWFEATURE/HWFCCAT" />
			<xsl:variable name="machtype" select="FROMMACHTYPE" />
			<xsl:variable name="model" select="FROMMODEL" />
			<xsl:variable name="tomachtype" select="TOMACHTYPE" />
			<xsl:variable name="tomodel" select="TOMODEL" />
			<xsl:variable name="hwfccat" select="FROMFEATURE/HWFEATURE/HWFCCAT" />
			<xsl:variable name="hwfccat_title" select="FROMFEATURE/HWFEATURE/HWFCCAT_TITLE" />

			<xsl:call-template name="buildFeatureConversionF1Header">
				<xsl:with-param name="machtype" select="$machtype" />
				<xsl:with-param name="model" select="$model" />
				<xsl:with-param name="hwfccat" select="$hwfccat_title" />
				<xsl:with-param name="tomachtype">
					<xsl:if test="(FROMMACHTYPE != TOMACHTYPE) or (FROMMODEL != TOMODEL)">
						<xsl:value-of select="TOMACHTYPE" />
					</xsl:if>
				</xsl:with-param>
				<xsl:with-param name="tomodel">
					<xsl:if test="(FROMMACHTYPE != TOMACHTYPE) or (FROMMODEL != TOMODEL)">
						<xsl:value-of select="TOMODEL" />
					</xsl:if>
				</xsl:with-param>
			</xsl:call-template>

			<!-- Process all the new feature conversions (section 2.2.2)
				Sort specified in spec is:
				To Machine Type
				To Model
				To Feature Code
				From Machine Type
				From Model
				From Feature Code
				
				however, provided examples and direction from Eric is to subgroup by HWFCCAT and sort by:
				From Feature Code
				To Feature Code -->
			<xsl:for-each select="../FCTRANSACTION[FROMMACHTYPE = $machtype and FROMMODEL = $model and 
			TOMACHTYPE = $tomachtype and TOMODEL = $tomodel and FROMFEATURE/HWFEATURE/HWFCCAT = $hwfccat]">
				<xsl:sort select="TOFEATURECODE" />
				<xsl:sort select="FROMFEATURECODE" />

				<xsl:call-template name="printGeosBegin">
					<xsl:with-param name="geos" select="GEOS" />
				</xsl:call-template>

				<!-- per Eric and Rosemary, no editor notes included -->
				<!--
					<xsl:if test="FROMFEATURE/HWFEATURE/EDITORNOTE">
					<xsl:call-template name="printEditorNote">
					<xsl:with-param name="editornote" select="FROMFEATURE/HWFEATURE/EDITORNOTE" />
					</xsl:call-template>
					</xsl:if>
					<xsl:if test="TOFEATURE/HWFEATURE/EDITORNOTE">
					<xsl:call-template name="printEditorNote">
					<xsl:with-param name="editornote" select="TOFEATURE/HWFEATURE/EDITORNOTE" />
					</xsl:call-template>
					</xsl:if>
				-->

				<xsl:variable name="fromFeature" select="concat(FROMFEATURECODE, ' - ', FROMFEATURE/HWFEATURE/MKTGNAME)" />
				<xsl:variable name="toFeature" select="concat(TOFEATURECODE, ' - ', TOFEATURE/HWFEATURE/MKTGNAME)" />
				
				<xsl:call-template name="wrapFeatconvColumns">
					<xsl:with-param name="fromStart" select="'1'" />
					<xsl:with-param name="fromWidth" select="$f1FromFCLength" />
					<xsl:with-param name="fromFeature" select="$fromFeature" />
					<xsl:with-param name="toStart" select="'2' + $f1FromFCLength" />
					<xsl:with-param name="toWidth" select="$f1ToFCLength" />
					<xsl:with-param name="toFeature" select="$toFeature" />
					<xsl:with-param name="returnParts" select="RETURNEDPARTS" />
					<xsl:with-param name="returnPartsLength" select="$partsReturnedLength" />
					<xsl:with-param name="price" select="$price" />
					<xsl:with-param name="purchasePriceLength" select="$purchasePriceLength" />
				</xsl:call-template>

				<xsl:call-template name="printGeosEnd">
					<xsl:with-param name="geos" select="GEOS" />
				</xsl:call-template>

			</xsl:for-each>

			<xsl:value-of select="concat(':exmp.', $newline, $newline)" />
		</xsl:for-each>
	</xsl:template>

	<!-- Feature Conversions (section 2.2.2) -->
	<xsl:template name="FeatureConversion-Format2">
		<xsl:param name="sectionHeader" />
		<xsl:param name="f2FromFCLength" select="$f2FromFCLength" />
		<xsl:param name="f2ToFCLength" select="$f2ToFCLength" />
		<xsl:param name="f2ReturnPartsLength" select="$f2ReturnPartsLength" />
		<xsl:param name="f2ContMaintLength" select="$f2ContMaintLength" />
		<xsl:param name="f2MachTypeLength" select="$f2MachTypeLength" />
		<xsl:param name="f2ModelLength" select="$f2ModelLength" />
		<xsl:param name="price" />
		<xsl:param name="purchasePriceLength" select="$purchasePriceLength" />

		<xsl:value-of select="$sectionHeader" />

		<!-- Process all the new feature conversions (section 2.2.2)
			Sort by:
			To Machine Type
			To Model
			To Feature Code
			From Machine Type
			From Model
			From Feature Code -->
		<xsl:for-each select="FCTRANSACTION">
			<xsl:sort select="TOMACHTYPE" />
			<xsl:sort select="TOMODEL" />
			<xsl:sort select="TOFEATURECODE" />
			<xsl:sort select="FROMMACHTYPE" />
			<xsl:sort select="FROMMODEL" />
			<xsl:sort select="FROMFEATURECODE" />

			<xsl:call-template name="printGeosBegin">
				<xsl:with-param name="geos" select="GEOS" />
			</xsl:call-template>

			<!-- per Eric and Rosemary, no editor notes included -->
			<!--
				<xsl:if test="TOFEATURE/HWFEATURE/EDITORNOTE">
				<xsl:call-template name="printEditorNote">
				<xsl:with-param name="editornote" select="TOFEATURE/HWFEATURE/EDITORNOTE" />
				</xsl:call-template>
				</xsl:if>
				<xsl:if test="FROMFEATURE/HWFEATURE/EDITORNOTE">
				<xsl:call-template name="printEditorNote">
				<xsl:with-param name="editornote" select="FROMFEATURE/HWFEATURE/EDITORNOTE" />
				</xsl:call-template>
				</xsl:if>
			-->

			<xsl:value-of select="concat(substring(concat(FROMFEATURECODE, $padding), 1, $f2FromFCLength), '   ')" />
			<xsl:value-of select="concat(substring(concat(TOFEATURECODE, $padding), 1, $f2ToFCLength), '   ')" />
			<xsl:call-template name="formatReturnParts">
				<xsl:with-param name="returnParts" select="RETURNEDPARTS" />
				<xsl:with-param name="returnPartsLength" select="$f2ReturnPartsLength" />
			</xsl:call-template>
			<xsl:value-of select="'   '" />
			<xsl:choose>
				<xsl:when test="string-length($price) &gt; 0">
					<xsl:value-of select="concat(substring(concat($price, $padding), 1, $purchasePriceLength), ' ')" />
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="concat(substring($padding, 1, $f2ContMaintLength), '   ')" />
					<xsl:value-of select="concat(substring(concat(TOMACHTYPE, $padding), 1, $f2MachTypeLength), '   ')" />
					<xsl:value-of select="concat(substring(concat(TOMODEL, $padding), 1, $f2ModelLength), ' ')" />
				</xsl:otherwise>
			</xsl:choose>
			<xsl:value-of select="$newline" />

			<xsl:call-template name="printGeosEnd">
				<xsl:with-param name="geos" select="GEOS" />
			</xsl:call-template>

		</xsl:for-each>
		<xsl:value-of select="concat(':exmp.', $newline, $newline)" />
	</xsl:template>

	<!-- Wraps feature descriptions in 2 columns of table. -->
	<xsl:template name="wrapFeatconvColumns">
		<xsl:param name="fromStart" />
		<xsl:param name="fromWidth" />
		<xsl:param name="fromFeature" />
		<xsl:param name="toStart" />
		<xsl:param name="toWidth" />
		<xsl:param name="toFeature" />
		<xsl:param name="returnParts" />
		<xsl:param name="returnPartsLength" />
		<xsl:param name="price" />
		<xsl:param name="purchasePriceLength" />
		
		<!-- remove extra whitespace -->
		<xsl:variable name="norm_fromFeature" select="normalize-space($fromFeature)" />
		<xsl:variable name="norm_toFeature" select="normalize-space($toFeature)" />

		<!-- determine where fromFeature needs to be split to fit it's column -->
		<xsl:variable name="splitFromFeature">
			<xsl:choose>
				<xsl:when test="string-length($norm_fromFeature) &gt; $fromWidth">
				    <xsl:call-template name="lineSplitPos">
						<xsl:with-param name="string" select="substring($norm_fromFeature, 1, $fromWidth)" />
   					</xsl:call-template>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="string-length($norm_fromFeature)" />
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<!-- determine where toFeature needs to be split to fit it's column -->
		<xsl:variable name="splitToFeature">
			<xsl:choose>
				<xsl:when test="string-length($norm_toFeature) &gt; $toWidth">
				    <xsl:call-template name="lineSplitPos">
						<xsl:with-param name="string" select="substring($norm_toFeature, 1, $toWidth)" />
    				</xsl:call-template>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="string-length($norm_toFeature)" />
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<!-- Print first line of feature descriptions -->
		<xsl:value-of select="concat(substring(concat(substring($norm_fromFeature, 1, $splitFromFeature), $padding), 1, $fromWidth), ' ')" />
		<xsl:value-of select="concat(substring(concat(substring($norm_toFeature, 1, $splitToFeature), $padding), 1, $toWidth), ' ')" />
		<xsl:call-template name="formatReturnParts">
			<xsl:with-param name="returnParts" select="$returnParts" />
			<xsl:with-param name="returnPartsLength" select="$partsReturnedLength" />
		</xsl:call-template>
		<xsl:value-of select="' '" />
		<xsl:if test="string-length($price) &gt; 0">
			<xsl:value-of select="substring(concat($price, $padding), 1, $purchasePriceLength)" />
		</xsl:if>
		<xsl:value-of select="$newline" />
				
		<!-- fit long descriptions into their table columns -->
		<xsl:if test="string-length($norm_fromFeature) &gt; $f1FromFCLength or string-length($norm_toFeature) &gt; $f1ToFCLength">
			<xsl:call-template name="wrap2Columns">
				<xsl:with-param name="start1" select="$fromStart" />
				<xsl:with-param name="width1" select="$fromWidth" />
				<xsl:with-param name="string1" select="substring($norm_fromFeature, $splitFromFeature + 1)" />
				<xsl:with-param name="start2" select="$toStart" />
				<xsl:with-param name="width2" select="$toWidth" />
				<xsl:with-param name="string2" select="substring($norm_toFeature, $splitToFeature + 1)" />
			</xsl:call-template>
			<xsl:value-of select="$newline" />
		</xsl:if>
	</xsl:template>

</xsl:stylesheet>