<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tag="http://www.corefiling.com/ns/2007/xhtml2xbrl" xmlns:xbrli="http://www.xbrl.org/2003/instance" exclude-result-prefixes="tag">
	<xsl:output indent="yes"/>
	<xsl:variable name="single-facts">
	    <xsl:for-each select="//*[@tag:name and not(ancestor::node()[@tag:name] or descendant::node()[@tag:name])]">
	        <xsl:sort select="concat(@tag:name, @tag:contextRef, @tag:unitRef, @tag:decimals)"/>
		<xsl:copy-of select="."/>
	    </xsl:for-each>
	</xsl:variable>
	<xsl:variable name="tuple-facts">
	    <xsl:perform-sort select="//*[@tag:name and descendant::node()[@tag:name] and not(ancestor::node()[@tag:name])]">
	        <xsl:sort select="@tag:name"/>
	    </xsl:perform-sort>
	</xsl:variable>
	<xsl:template match="/">
	    <xsl:processing-instruction name="xml-stylesheet">
	        <xsl:text>type="text/xsl" href="./CH-AE-stylesheet.xsl" </xsl:text>
	    </xsl:processing-instruction>
		<xsl:variable name="xbrls" select="//xbrli:xbrl"/>
		<xsl:if test="count($xbrls) &gt; 1">
		  <xsl:message terminate="yes">More than one xbrl element found, cannot process.</xsl:message>
		</xsl:if>
		<xsl:for-each select="$xbrls">
			<xsl:copy>
		        <xsl:apply-templates select="@*|node()" mode="identity-only"/>
				<xsl:for-each select="$tuple-facts/*">
				    <xsl:variable name="pos" select="position()"/>
				    <xsl:if test="$pos = 1 or not(deep-equal($tuple-facts/*[$pos - 1], $tuple-facts/*[$pos]))">
					<xsl:call-template name="process-tuples">
						<xsl:with-param name="concept" select="."/>
					</xsl:call-template>
				    </xsl:if>
				</xsl:for-each>
				<xsl:for-each select="$single-facts/*">
				    <xsl:variable name="pos" select="position()"/>
				    <xsl:if test="$pos = 1 or concat(@tag:name, @tag:contextRef, @tag:unitRef, @tag:decimals) != concat($single-facts/*[$pos - 1]/@tag:name, $single-facts/*[$pos - 1]/@tag:contextRef, $single-facts/*[$pos - 1]/@tag:unitRef, $single-facts/*[$pos - 1]/@tag:decimals)">
					<xsl:call-template name="process-single-facts">
						<xsl:with-param name="concept"/>
					</xsl:call-template>
				    </xsl:if>
				</xsl:for-each>
			</xsl:copy>
		</xsl:for-each>
	</xsl:template>
	<xsl:template match="@*|node()" mode="identity-only">
		<xsl:copy>
			<xsl:apply-templates select="@*|node()" mode="identity-only"/>
		</xsl:copy>
	</xsl:template>
	<xsl:template name="qname-namespace">
		<xsl:param name="qname"/>
		<xsl:param name="context"/>
		<xsl:variable name="prefix"><xsl:value-of select="substring-before($qname, ':')"/></xsl:variable>
		<xsl:value-of select="$context/namespace::*[local-name() = $prefix]"/>
	</xsl:template>
	<xsl:template name="process-tuples">
		<xsl:param name="concept"/>
		<xsl:variable name="namespace">
			<xsl:call-template name="qname-namespace">
				<xsl:with-param name="qname" select="@tag:name"/>
				<xsl:with-param name="context" select="."/>
			</xsl:call-template>
		 </xsl:variable>
		<xsl:element name="{@tag:name}" namespace="{$namespace}">
			<xsl:for-each select="@*[local-name() != 'name' and local-name() != 'scale' and local-name() != 'seq' and namespace-uri() = 'http://www.corefiling.com/ns/2007/xhtml2xbrl']">
				<xsl:attribute name="{local-name()}"><xsl:value-of select="."/></xsl:attribute>
            </xsl:for-each>
			<xsl:for-each select="child::node()[@tag:name]">
			    <xsl:sort select="@tag:seq"/>
				<xsl:call-template name="process-tuples">
					<xsl:with-param name="concept" select="."/>
				</xsl:call-template>
			</xsl:for-each>
			<xsl:if test="not(child::node()[@tag:name])">
				<xsl:choose>
					<xsl:when test="@tag:unitRef">
						<xsl:call-template name="unformat">
							<xsl:with-param name="value"><xsl:value-of select="."/></xsl:with-param>
							<xsl:with-param name="scale"><xsl:value-of select="@tag:scale"/></xsl:with-param>
						</xsl:call-template>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="."/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:if>
		</xsl:element>
	</xsl:template>
	<xsl:template name="process-single-facts">
		<xsl:param name="concept"/>
		<xsl:variable name="namespace">
			<xsl:call-template name="qname-namespace">
				<xsl:with-param name="qname" select="@tag:name"/>
				<xsl:with-param name="context" select="."/>
			</xsl:call-template>
		 </xsl:variable>
		<xsl:element name="{@tag:name}" namespace="{$namespace}">
			<xsl:for-each select="@*[local-name() != 'name' and local-name() != 'scale' and namespace-uri() = 'http://www.corefiling.com/ns/2007/xhtml2xbrl']">
				<xsl:attribute name="{local-name()}"><xsl:value-of select="."/></xsl:attribute>
            </xsl:for-each>
            <xsl:choose>
              <xsl:when test="@tag:unitRef">
                <xsl:call-template name="unformat">
                  <xsl:with-param name="value"><xsl:value-of select="."/></xsl:with-param>
                  <xsl:with-param name="scale"><xsl:value-of select="@tag:scale"/></xsl:with-param>
                </xsl:call-template>
              </xsl:when>
              <xsl:when test="contains(@tag:name,'Date')">
				  <xsl:call-template name="parse-date">
					  <xsl:with-param name="day" select="substring-before(., ' ')"/>
					  <xsl:with-param name="month-name" select="substring-before(substring-after(., ' '), ' ')"/>
					  <xsl:with-param name="year" select="substring-after(substring-after(., ' '), ' ')"/>
				  </xsl:call-template>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="."/>
              </xsl:otherwise>
            </xsl:choose>
		</xsl:element>
	</xsl:template>
	
	<xsl:template name="unformat">
    <!-- TODO: Implement properly, this is naive support for jlt's example and reuters stuff. -->
		<xsl:param name="value"/>
		<xsl:param name="scale"/>
		<xsl:variable name="scaleMe">
			<xsl:call-template name="_unformat">
				<xsl:with-param name="value" select="$value"/>
			</xsl:call-template>
		</xsl:variable>
		<xsl:variable name="scaled">
			<xsl:choose>
				<xsl:when test="string(number($scale)) != 'NaN'">
					<xsl:value-of select="format-number($scaleMe * $scale, '###0.##')"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$scaleMe"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:value-of select="string($scaled)"/>
	</xsl:template>
  
	<xsl:template name="_unformat">
		<xsl:param name="value"/>
		<xsl:param name="scale"/>
		<xsl:choose>
			<xsl:when test="contains($value, ',')">
				<xsl:call-template name="_unformat">
					<xsl:with-param name="value" select="translate($value, ',', '')"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:when test="starts-with($value, '(')">
				<xsl:value-of select="translate($value, '()', '-')"/>
			</xsl:when>
			<xsl:when test="contains($value, 'p')">
				<xsl:value-of select="number(substring-before($value, 'p')) div 100"/>
			</xsl:when>
			<xsl:when test="contains($value, 'B')">
				<xsl:value-of select="number(substring-before($value, 'B')) * 1000000000"/>
			</xsl:when>
			<xsl:when test="contains($value, '%')">
				<xsl:value-of select="number(substring-before($value, '%')) div 100"/>
			</xsl:when>
			 <xsl:otherwise>
				<xsl:value-of select="$value"/>
			</xsl:otherwise>
		</xsl:choose>
	  </xsl:template>
	  
	  <xsl:template name="parse-date">
		  <xsl:param name="day"/>
		  <xsl:param name="month-name"/>
		  <xsl:param name="year"/>
		  <xsl:variable name="month" select="substring(substring-after('January01February02March03April04May05June06July07August08September09October10November11December12',$month-name),1 ,2)"/>
		  <xsl:value-of select="concat($year, '-', $month, '-', $day)"/>
	  </xsl:template>
</xsl:stylesheet>

