<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:param name="f" select="'english'"/>
	<xsl:variable name="config" select="document('configuration.xml')/configuration"/>
	<xsl:strip-space elements="*"/>
	<xsl:output method="text"/>
	<xsl:decimal-format name="nordic" grouping-separator="." decimal-separator=","/>
	<xsl:decimal-format name="english" grouping-separator="," decimal-separator="."/>
	<xsl:template match="products/product">
		<!--variables normalized input-->
		<xsl:variable name="idN">
			<xsl:choose>
				<xsl:when test="$config/fields/id/normalize = 'yes'">
					<xsl:value-of select="normalize-space(@id)"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="@id"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="nameN">
			<xsl:choose>
				<xsl:when test="$config/fields/name/normalize = 'yes'">
					<xsl:value-of select="normalize-space(name)"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="name"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="priceN">
			<xsl:choose>
				<xsl:when test="$config/fields/price/normalize = 'yes'">
					<xsl:value-of select="normalize-space(price)"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="price"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="stockN">
			<xsl:choose>
				<xsl:when test="$config/fields/stock/normalize = 'yes'">
					<xsl:value-of select="normalize-space(stock)"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="stock"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="countryN">
			<xsl:choose>
				<xsl:when test="$config/fields/country/normalize = 'yes'">
					<xsl:value-of select="normalize-space(country)"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="country"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<!--variables formated input-->
		<xsl:variable name="priceNF" select="format-number($priceN, $config/decimalFormat/*[name() = $f], $f)"/>
		<!--variables extra spaces-->
		<xsl:variable name="idSpace">
			<xsl:call-template name="times">
				<xsl:with-param name="counter" select="$config/fields/id/length - string-length($idN)"/>
			</xsl:call-template>
		</xsl:variable>
		<xsl:variable name="nameSpace">
			<xsl:call-template name="times">
				<xsl:with-param name="counter" select="$config/fields/name/length - string-length($nameN)"/>
			</xsl:call-template>
		</xsl:variable>
		<xsl:variable name="priceSpace">
			<xsl:call-template name="times">
				<xsl:with-param name="counter" select="$config/fields/price/length - string-length($priceNF)"/>
			</xsl:call-template>
		</xsl:variable>
		<xsl:variable name="stockSpace">
			<xsl:call-template name="times">
				<xsl:with-param name="counter" select="$config/fields/stock/length - string-length($stockN)"/>
			</xsl:call-template>
		</xsl:variable>
		<xsl:variable name="countrySpace">
			<xsl:call-template name="times">
				<xsl:with-param name="counter" select="$config/fields/country/length - string-length($countryN)"/>
			</xsl:call-template>
		</xsl:variable>
		<!--fixed output begins here-->
		<xsl:value-of select="concat($idN, $idSpace, $nameSpace, $nameN, $priceSpace, $priceNF, $stockSpace, $stockN, $countrySpace, $countryN)"/>
		<xsl:text>&#xA;</xsl:text>
	</xsl:template>
	<!--template to return spaces x number of times-->
	<xsl:template name="times">
		<xsl:param name="counter"/>
		<xsl:if test="$counter > 0">
			<xsl:value-of select="' '"/>
			<xsl:call-template name="times">
				<xsl:with-param name="counter" select="$counter - 1"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>
	<!-- Version 1d -->
	<!-- 1) We have made a configuration file, configuration.xml. -->
	<!-- 2) We can configure if input should be normalized. -->
	<!-- 3) We can configure length of each output field. -->
	<!-- 4) We can configure mask for decimal-format.  -->
	<!-- 5) We have declared a "nordic" decimal-format. -->
	<!-- 6) We have made the English decimal-format explicit. -->
	<!-- 7) XMLSpy's built-in processor is stripping whitespace-only text-nodes. Xsl:strip-space is not needed. -->
	<!-- 8) In Saxon we must include xsl:strip-space or delete the linefeed at the end of last template. -->
	<!-- 9) The solution above works in both Saxon and in XMLSpy's build-in processor. -->
</xsl:stylesheet>

