<!DOCTYPE xsl:transform [
<!ENTITY xbrl "http://www.xmlportfolio.com/notReallyXBRL">
]>

<xsl:transform version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xsd="http://www.w3.org/1999/XMLSchema"
  xmlns:xbrl="&xbrl;"
  xmlns:xbrl2="http://www.xbrl.org/core/2000-07-31/metamodel"
  exclude-result-prefixes="xsd xbrl2">

  <xsl:param name="removeDots" select="'yes'"/>

  <xsl:param name="defaultWeight" select="'0'"/>
  <xsl:param name="defaultType" select="'string'"/>
  <xsl:param name="defaultLanguage" select="'en'"/>

  <xsl:template match="/">
    <xsl:comment>
WARNING: This is not a valid XBRL taxonomy; it is only an experimental view of another
possible way of specifying taxonomies that are tree structures. Emphasis is made on
leveraging features of XML syntax for clarity and human readability. The XSLT used to
generate this document is meant for a taxonomy that consists of a tree structure, which
requires that 1) there is only one rollup per type, and 2) there is one root that doesn't
have a rollup, and 3) no rollups ultimately point to themselves. Note that the default*
attributes on the root element provide another level of "cleaning up". Where a value
is not the default, it is explicitly provided inline (including label elements). Also
note that all reference elements have been relegated to an "appendix", referred to
individually by referenceID attributes where applicable.
    </xsl:comment>
    <xsl:if test="$removeDots='yes'">
      <xsl:comment>
In this particular taxonomy, it's possible that element names are not unique, because
the "removeDots" flag was set to 'yes' (default), when the stylesheet was applied to the
original taxonomy document. Exactly how XBRL instance documents would then refer to this
"taxonomy" is not yet clear - perhaps with something like XPointer and XPath expressions.
Note that by using a tree structure, elements have an identity outside their name alone -
their context within the tree. Thus, they need not have unique element names, if there's
another way to reference them from instance documents. This would remove the need for the
visually redundant parent.child naming convention, which itself does not guarantee
uniqueness without potentially needing to arbitrarily change names in a taxonomy.
      </xsl:comment>
    </xsl:if>
    <xbrl:taxonomy defaultWeight="{$defaultWeight}" defaultType="{$defaultType}" defaultLanguage="{$defaultLanguage}">
      <xsl:apply-templates select="/xsd:schema/xsd:element[not(xsd:annotation/xsd:appinfo/xbrl2:rollup)]"/>
      <xbrl:references>
        <xsl:apply-templates select="//xbrl2:reference" mode="references"/>
      </xbrl:references>
    </xbrl:taxonomy>
  </xsl:template>

  <xsl:template match="/xsd:schema/xsd:element">
    <xsl:variable name="elementName">
      <xsl:choose>
        <xsl:when test="$removeDots='yes' and contains(@name,'.')">
          <xsl:value-of select="substring-after(@name,'.')"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="string(@name)"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:element name="{$elementName}" namespace="{string(/xsd:schema/@targetNamespace)}">
      <xsl:apply-templates select="xsd:annotation/xsd:appinfo/xbrl2:rollup/@*[not(local-name()='to')]">
        <xsl:with-param name="position" select="position()"/>
      </xsl:apply-templates>
      <xsl:apply-templates select="@*[not(local-name()='name')]"/>
      <xsl:if test="xsd:annotation/xsd:appinfo/xbrl2:label[@xml:lang=$defaultLanguage]">
        <xsl:attribute name="label" namespace="&xbrl;">
          <xsl:value-of select="xsd:annotation/xsd:appinfo/xbrl2:label[@xml:lang=$defaultLanguage]"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:apply-templates select="xsd:annotation/xsd:appinfo/xbrl2:label[@xml:lang!=$defaultLanguage]"/>
      <xsl:apply-templates select="xsd:annotation/xsd:appinfo/xbrl2:reference"/>
      <xsl:apply-templates select="xsd:annotation/xsd:appinfo/*[not(self::xbrl2:label)]"/>
      <xsl:apply-templates select="/xsd:schema/xsd:element[xsd:annotation/xsd:appinfo/xbrl2:rollup/@to = current()/@name]"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="xbrl2:rollup"/>

  <xsl:template match="xbrl2:label">
    <xbrl:label>
      <xsl:apply-templates select="@*|node()"/>
    </xbrl:label>
  </xsl:template>

  <xsl:template match="xbrl2:reference">
    <xsl:attribute name="referenceID" namespace="&xbrl;">
      <xsl:value-of select="generate-id()"/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="xsd:*">
    <xsl:element name="{local-name()}" namespace="&xbrl;">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="*">
    <xsl:element name="{local-name()}" namespace="{namespace-uri()}">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="@*">
    <xsl:copy-of select="."/>
  </xsl:template>

  <xsl:template match="@type">
    <xsl:if test="not(.=$defaultType)">
      <xsl:attribute name="type" namespace="&xbrl;">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>

  <xsl:template match="@order">
    <xsl:param name="position"/>
    <xsl:if test="not(.=$position)">
      <xsl:attribute name="order" namespace="&xbrl;">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>

  <xsl:template match="@weight">
    <xsl:if test="not(.=$defaultWeight)">
      <xsl:attribute name="weight" namespace="&xbrl;">
        <xsl:value-of select="."/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>

  <xsl:template match="xbrl2:reference" mode="references">
    <xbrl:reference>
      <xsl:copy-of select="@*[string(.)]"/>
      <xsl:attribute name="ID">
        <xsl:value-of select="generate-id()"/>
      </xsl:attribute>
    </xbrl:reference>
  </xsl:template>

</xsl:transform>
