<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<!-- Here we load the input document as an uparsed string -->
	<xsl:variable name="unparsed" select="unparsed-text(document-uri(.))"/>
	<!-- The next four varaibles are preparations to make it easier to modify the name of the output file -->
	<xsl:variable name="input-file" select="tokenize(document-uri(.), '(\\|/)')[last()]"/>
	<xsl:variable name="input-filename" select="substring-before($input-file, '.')"/>
	<xsl:variable name="input-path" select="substring-before(document-uri(.), $input-filename)"/>
	<xsl:variable name="input-extension" select="substring-after($input-file, '.')"/>
	<!-- The next two variables are for single and double quote to make the next variable easier to read -->
	<xsl:variable name="apos" select="&quot;&apos;&quot;"/>
	<xsl:variable name="quot" select="'&quot;'"/>
	<!-- Here we normalize-space and single qoutes are replaced by double qoutes -->
	<xsl:variable name="unparsed-normalized" select="normalize-space(translate($unparsed, $apos,
		$quot))"/>
	<!-- Here we remove all comments so they don't fool our testing  -->
	<xsl:variable name="unparsed-no-comment" select="replace($unparsed-normalized,
		'&lt;!--.+--&gt;', '')"/>
	<!-- Here we remove all PI's so they don't fool our testing  -->
	<xsl:variable name="unparsed-no-pi">
		<xsl:choose>
			<xsl:when test="contains(substring($unparsed-normalized, 1, 5), '&lt;?xml')">
				<xsl:value-of select="replace(substring-after($unparsed-no-comment, '?>'), '&lt;\?.+\?&gt;', '')"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="replace($unparsed-no-comment, '&lt;\?.+\?&gt;', '')"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<!-- Here we remove all CDATA sections so they don't fool our testing  -->
	<xsl:variable name="unparsed-no-cdatasection" select="replace($unparsed-no-pi,
		'&lt;!\[CDATA\[.+\]\]&gt;', '')"/>
	<!-- Here we remove the rest, that is everything but what is left of the prolog  -->
	<xsl:variable name="unparsed-no-topelement" select="replace($unparsed-no-cdatasection,
		'&lt;\i.*', '')"/>
	<!-- dtd-declaration-string -->
	<!-- The string contains nothing but the DTD declaration and only if it exists. If the DTD declaration don't exist the string is empty -->
	<xsl:param name="dtd-declaration-string">
		<xsl:choose>
			<xsl:when test="normalize-space($unparsed-no-topelement) ne ''">
				<xsl:value-of select="normalize-space($unparsed-no-topelement)"/>
			</xsl:when>
			<xsl:otherwise>false</xsl:otherwise>
		</xsl:choose>
	</xsl:param>
	<!-- **************************************** -->
	<!-- Template match / -->
	<!-- **************************************** -->
	<xsl:template match="/">
		<!-- If an internal DTD subset is detected the transformation stops here, if "xsl:message" is supported. -->
		<xsl:variable name="afterDoctype" select="substring-after($dtd-declaration-string, 'DOCTYPE')"/>
		<xsl:if test="contains($afterDoctype, '&lt;!')">
			<xsl:message terminate="yes">Internal DTD subset detected. Subsets are not recreated since it is not possible in XSLT 2.0 without extensions.</xsl:message>
		</xsl:if>
		<!-- xml declaration -->
		<xsl:variable name="input-xml-declaration">
			<xsl:choose>
				<xsl:when test="contains(substring($unparsed-normalized, 1, 5), '&lt;?xml')">true</xsl:when>
				<xsl:otherwise>false</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<!-- xml declaration string -->
		<xsl:variable name="xml-declaration-string">
			<xsl:choose>
				<xsl:when test="$input-xml-declaration eq 'true'">
					<xsl:value-of select="concat(substring-before($unparsed-normalized,'?&gt;'), '?&gt;')"/>
				</xsl:when>
				<xsl:otherwise>false</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<!-- xml version pseudo-attribute -->
		<xsl:variable name="input-xml-version">
			<xsl:choose>
				<xsl:when test="$input-xml-declaration eq 'true'">
					<xsl:variable name="version-plus" select="substring-after($xml-declaration-string, 'version=&#034;')"/>
					<xsl:value-of select="substring-before($version-plus, '&#034;')"/>
				</xsl:when>
				<xsl:otherwise>false</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<!-- xml endoding pseudo-atttribute -->
		<xsl:variable name="input-xml-encoding">
			<xsl:choose>
				<xsl:when test="$input-xml-declaration eq 'true'">
					<xsl:choose>
						<xsl:when test="contains($xml-declaration-string, 'encoding=')">
							<xsl:variable name="encoding-plus" select="substring-after($xml-declaration-string,
								'encoding=&#034;')"/>
							<xsl:value-of select="substring-before($encoding-plus, '&#034;')"/>
						</xsl:when>
						<xsl:otherwise>UTF-8</xsl:otherwise>
					</xsl:choose>
				</xsl:when>
				<xsl:otherwise>UTF-8</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<!-- xml standalone pseudo-atttribute -->
		<xsl:variable name="input-xml-standalone">
			<xsl:choose>
				<xsl:when test="$input-xml-declaration eq 'true'">
					<xsl:choose>
						<xsl:when test="contains($xml-declaration-string, 'standalone=')">
							<xsl:variable name="standalone-plus" select="substring-after($xml-declaration-string,
								'standalone=&#034;')"/>
							<xsl:value-of select="substring-before($standalone-plus, '&#034;')"/>
						</xsl:when>
						<xsl:otherwise>omit</xsl:otherwise>
					</xsl:choose>
				</xsl:when>
				<xsl:otherwise>omit</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<!-- DOCTYPE public -->
		<xsl:variable name="input-dtd-public">
			<xsl:choose>
				<xsl:when test="matches($dtd-declaration-string, '&lt;!DOCTYPE.+PUBLIC.+&gt;')">
					<xsl:variable name="dtd-system-plus" select="substring-after($dtd-declaration-string, 'PUBLIC &#034;')"/>
					<xsl:variable name="dtd-system-plus2" select="substring-after($dtd-system-plus, '&#034;')"/>
					<xsl:variable name="dtd-system-plus3" select="substring-after($dtd-system-plus2,
						'&#034;')"/>
					<xsl:value-of select="substring-before($dtd-system-plus, '&#034;')"/>
				</xsl:when>
				<xsl:otherwise>false</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<!-- DOCTYPE system -->
		<xsl:variable name="input-dtd-system">
			<xsl:choose>
				<xsl:when test="contains($dtd-declaration-string, 'SYSTEM')">
					<xsl:variable name="dtd-system-plus" select="substring-after($dtd-declaration-string, 'SYSTEM &#034;')"/>
					<xsl:value-of select="substring-before($dtd-system-plus, '&#034;')"/>
				</xsl:when>
				<xsl:when test="contains($dtd-declaration-string, 'PUBLIC')">
					<xsl:variable name="dtd-system-plus" select="substring-after($dtd-declaration-string, 'PUBLIC &#034;')"/>
					<xsl:variable name="dtd-system-plus2" select="substring-after($dtd-system-plus,
						'&#034;')"/>
					<xsl:variable name="dtd-system-plus3" select="substring-after($dtd-system-plus2,
						'&#034;')"/>
					<xsl:value-of select="substring-before($dtd-system-plus3, '&#034;')"/>
				</xsl:when>
				<xsl:otherwise>false</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<!-- Omit XML declaration -->
		<xsl:variable name="input-omit-xml-declaration">
			<xsl:choose>
				<xsl:when test="$input-xml-declaration eq 'true'">no</xsl:when>
				<xsl:otherwise>yes</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<!-- ******************************* -->
		<!-- Configuration -->
		<!-- ******************************* -->
		<!-- Output path and filename -->
		<xsl:variable name="output-filename" select="concat($input-path, '/output/',
			$input-filename, '.', $input-extension)"/>
		<!-- omit-xml-declaration -->
		<xsl:variable name="output-omit-xml-declaration">
			<xsl:choose>
				<xsl:when test="$input-omit-xml-declaration eq 'no'">no</xsl:when>
				<xsl:when test="$input-omit-xml-declaration eq 'yes'">yes</xsl:when>
			</xsl:choose>
		</xsl:variable>
		<!-- xml-version -->
		<xsl:variable name="output-xml-version">
			<xsl:choose>
				<xsl:when test="$input-xml-version eq '1.0'">1.0</xsl:when>
				<xsl:when test="$input-xml-version eq '1.1'">1.1</xsl:when>
				<xsl:otherwise>1.0</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<!-- xml-encoding -->
		<xsl:variable name="output-xml-encoding">
			<xsl:choose>
				<xsl:when test="upper-case($input-xml-encoding) eq 'US-ASCII'">US-ASCII</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'UTF-8'">UTF-8</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'UTF-16'">UTF-16</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'ISO-10646-UCS-2'">ISO-10646-UCS-2</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'ISO-10646-UCS-4'">SO-10646-UCS-4</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'ISO-8859-1'">ISO-8859-1</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'ISO-8859-2'">ISO-8859-2</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'ISO-8859-3'">ISO-8859-3</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'ISO-8859-4'">ISO-8859-4</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'ISO-8859-5'">ISO-8859-5</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'ISO-8859-6'">ISO-8859-6</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'ISO-8859-7'">ISO-8859-7</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'ISO-8859-8'">ISO-8859-8</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'ISO-8859-9'">ISO-8859-9</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'ISO-8859-10'">ISO-8859-10</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'ISO-8859-11'">ISO-8859-11</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'ISO-8859-13'">ISO-8859-13</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'ISO-8859-14'">ISO-8859-14</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'ISO-8859-15'">ISO-8859-15</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'ISO-2022-JP'">ISO-2022-JP</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'SHIFT_JIS'">SHIFT_JIS</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'EUC-JP'">EUC-JP</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'BIG5'">BIG5</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'GB2312'">GB2312</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'KOI6-R'">KOI6-R</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'ISO-2022-KR'">ISO-2022-KR</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'EUC-KR'">EUC-KR</xsl:when>
				<xsl:when test="upper-case($input-xml-encoding) eq 'ISO-2022-CN'">ISO-2022-CN</xsl:when>
				<!-- here you can add more encodings -->
				<xsl:otherwise>UTF-8</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<!-- xml-standalone -->
		<xsl:variable name="output-xml-standalone">
			<xsl:choose>
				<xsl:when test="$input-xml-standalone eq 'yes'">yes</xsl:when>
				<xsl:when test="$input-xml-standalone eq 'no'">no</xsl:when>
				<xsl:when test="$input-xml-standalone eq 'omit'">omit</xsl:when>
				<xsl:otherwise>omit</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<!-- dtd-public -->
		<xsl:variable name="output-dtd-public">
			<xsl:choose>
				<xsl:when test="$input-dtd-public eq 'false'">false</xsl:when>
				<xsl:when test="$input-dtd-public ne 'false'">
					<xsl:value-of select="$input-dtd-public"/>
				</xsl:when>
			</xsl:choose>
		</xsl:variable>
		<!-- dtd-system -->
		<xsl:variable name="output-dtd-system">
			<xsl:choose>
				<xsl:when test="$input-dtd-system eq 'false'">false</xsl:when>
				<xsl:when test="$input-dtd-system ne 'false'">
					<xsl:value-of select="$input-dtd-system"/>
				</xsl:when>
			</xsl:choose>
		</xsl:variable>
		<!-- ******************************* -->
		<!-- Result document -->
		<!-- ******************************* -->
		<xsl:choose>
			<xsl:when test="$output-dtd-public ne 'false' and $output-dtd-system ne 'false'">
				<xsl:result-document href="{$output-filename}" method="xml" omit-xml-declaration="{$output-omit-xml-declaration}" output-version="{$output-xml-version}" encoding="{$output-xml-encoding}" doctype-public="{$output-dtd-public}" doctype-system="{$output-dtd-system}" standalone="{$output-xml-standalone}" indent="yes">
					<xsl:apply-templates/>
				</xsl:result-document>
			</xsl:when>
			<xsl:when test="$output-dtd-system ne 'false'">
				<xsl:result-document href="{$output-filename}" method="xml" omit-xml-declaration="{$output-omit-xml-declaration}" output-version="{$output-xml-version}" encoding="{$output-xml-encoding}" doctype-system="{$output-dtd-system}" standalone="{$output-xml-standalone}" indent="yes">
					<xsl:apply-templates/>
				</xsl:result-document>
			</xsl:when>
			<xsl:otherwise>
				<xsl:result-document href="{$output-filename}" method="xml" omit-xml-declaration="{$output-omit-xml-declaration}" output-version="{$output-xml-version}" encoding="{$output-xml-encoding}" standalone="{$output-xml-standalone}" indent="yes">
					<xsl:apply-templates/>
				</xsl:result-document>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>
