<?xml version="1.0" encoding="UTF-8"?>
<!-- (c) dret 2004; xslt canonicalizer, implements commented canonicalization, ignores namespaces completely... -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="text" encoding="UTF-8"/>
	<xsl:template match="/">
		<xsl:if test="(count(//*) div count(//*/namespace::*)) != 1">
			<xsl:message terminate="yes">Sorry, this program does not support XML Namespaces...</xsl:message>
		</xsl:if>
		<xsl:apply-templates select="node()"/>
	</xsl:template>
	<xsl:template match="*">
		<xsl:value-of select="concat('&lt;',local-name())"/>
		<xsl:apply-templates select="@*">
			<xsl:sort select="local-name()"/>
		</xsl:apply-templates>
		<xsl:value-of select="'&gt;'"/>
		<xsl:apply-templates select="node()"/>
		<xsl:value-of select="concat('&lt;/',local-name(),'&gt;')"/>
	</xsl:template>
	<xsl:template match="@*">
		<xsl:value-of select="concat(' ',local-name(),'=&quot;')"/>
		<xsl:call-template name="attribute-value">
			<xsl:with-param name="string" select="string(.)"/>
		</xsl:call-template>
		<xsl:value-of select="'&quot;'"/>
	</xsl:template>
	<xsl:template match="text()">
		<xsl:call-template name="element-content">
			<xsl:with-param name="string" select="string(.)"/>
		</xsl:call-template>
	</xsl:template>
	<xsl:template match="comment() | processing-instruction()">
		<xsl:if test="(generate-id(ancestor::node()) = generate-id(/)) and count(preceding-sibling::*)">
			<xsl:value-of select="'&#xA;'"/>
		</xsl:if>
		<xsl:choose>
			<xsl:when test="self::processing-instruction()">
				<xsl:value-of select="concat('&lt;?',local-name())"/>
				<xsl:if test="string-length(.) &gt; 0">
					<xsl:value-of select="concat(' ',.)"/>
				</xsl:if>
				<xsl:value-of select="'?>'"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="'&lt;!--'"/>
				<xsl:value-of select="."/>
				<xsl:value-of select="'-->'"/>
			</xsl:otherwise>
		</xsl:choose>
		<xsl:if test="(generate-id(ancestor::node()) = generate-id(/)) and count(following-sibling::*)">
			<xsl:value-of select="'&#xA;'"/>
		</xsl:if>
	</xsl:template>
	<xsl:template name="element-content">
		<xsl:param name="string"/>
		<xsl:if test="string-length($string) &gt; 0">
			<xsl:choose>
				<xsl:when test="substring($string,1,1) = '&lt;'">
					<xsl:value-of select="'&amp;lt;'"/>
				</xsl:when>
				<xsl:when test="substring($string,1,1) = '&amp;'">
					<xsl:value-of select="'&amp;amp;'"/>
				</xsl:when>
				<xsl:when test="substring($string,1,1) = '&gt;'">
					<xsl:value-of select="'&amp;lt;'"/>
				</xsl:when>
				<xsl:when test="substring($string,1,1) = '&#xD;'">
					<xsl:value-of select="'&amp;#xD;'"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="substring($string,1,1)"/>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:call-template name="element-content">
				<xsl:with-param name="string" select="substring($string,2,string-length($string)-1)"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>
	<xsl:template name="attribute-value">
		<xsl:param name="string"/>
		<xsl:if test="string-length($string) &gt; 0">
			<xsl:choose>
				<xsl:when test="substring($string,1,1) = '&lt;'">
					<xsl:value-of select="'&amp;lt;'"/>
				</xsl:when>
				<xsl:when test="substring($string,1,1) = '&amp;'">
					<xsl:value-of select="'&amp;amp;'"/>
				</xsl:when>
				<xsl:when test="substring($string,1,1) = '&quot;'">
					<xsl:value-of select="'&amp;quot;'"/>
				</xsl:when>
				<xsl:when test="substring($string,1,1) = '&#x9;'">
					<xsl:value-of select="'&amp;#x9;'"/>
				</xsl:when>
				<xsl:when test="substring($string,1,1) = '&#xA;'">
					<xsl:value-of select="'&amp;#xA;'"/>
				</xsl:when>
				<xsl:when test="substring($string,1,1) = '&#xD;'">
					<xsl:value-of select="'&amp;#xD;'"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="substring($string,1,1)"/>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:call-template name="attribute-value">
				<xsl:with-param name="string" select="substring($string,2,string-length($string)-1)"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>
</xsl:stylesheet>

