<!DOCTYPE xsl:transform [
<!ENTITY xbrl "http://www.xmlportfolio.com/notReallyXBRL">
<!ENTITY xml  "http://www.w3.org/XML/1998/namespace">
]>

<xsl:transform version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xbrl="&xbrl;">

  <xsl:param name="weight" select="$defaultWeight"/>
  <xsl:param name="type" select="$defaultType"/>
  <xsl:param name="language" select="$defaultLanguage"/>

  <xsl:variable name="defaultWeight" select="string(/xbrl:taxonomy/@defaultWeight)"/>
  <xsl:variable name="defaultType" select="string(/xbrl:taxonomy/@defaultType)"/>
  <xsl:variable name="defaultLanguage" select="string(/xbrl:taxonomy/@defaultLanguage)"/>

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

  <xsl:template match="/xbrl:taxonomy/@defaultWeight">
    <xsl:attribute name="defaultWeight">
      <xsl:value-of select="$weight"/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="/xbrl:taxonomy/@defaultType">
    <xsl:attribute name="defaultType">
      <xsl:value-of select="$type"/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="/xbrl:taxonomy/@defaultLanguage">
    <xsl:attribute name="defaultLanguage">
      <xsl:value-of select="$language"/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="*[not(self::xbrl:*)]">
    <xsl:copy>
      <xsl:call-template name="decideAttributes">
        <xsl:with-param name="newDefault" select="$weight"/>
        <xsl:with-param name="oldDefault" select="$defaultWeight"/>
        <xsl:with-param name="attribute" select="@xbrl:weight"/>
        <xsl:with-param name="localName" select="'weight'"/>
      </xsl:call-template>
      <xsl:call-template name="decideAttributes">
        <xsl:with-param name="newDefault" select="$type"/>
        <xsl:with-param name="oldDefault" select="$defaultType"/>
        <xsl:with-param name="attribute" select="@xbrl:type"/>
        <xsl:with-param name="localName" select="'type'"/>
      </xsl:call-template>
      <xsl:apply-templates select="@*[not((local-name()='weight' and namespace-uri()='&xbrl;') or (local-name()='type' and namespace-uri()='&xbrl;'))]"/>
      <xsl:choose>
        <xsl:when test="$language = $defaultLanguage">
          <xsl:copy-of select="@xbrl:label | xbrl:label"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:choose>
            <xsl:when test="not(xbrl:label)">
              <xsl:if test="@xbrl:label">
                <xbrl:label>
                  <xsl:attribute name="lang" namespace="&xml;">
                    <xsl:value-of select="$defaultLanguage"/>
                  </xsl:attribute>
                  <xsl:value-of select="@xbrl:label"/>
                </xbrl:label>
              </xsl:if>
            </xsl:when>
            <xsl:otherwise>
              <xsl:if test="xbrl:label[@xml:lang=$language]">
                <xsl:attribute name="label" namespace="&xbrl;">
                  <xsl:value-of select="xbrl:label[@xml:lang=$language]"/>
                </xsl:attribute>
              </xsl:if>
              <xsl:if test="@xbrl:label">
                <xbrl:label>
                  <xsl:attribute name="lang" namespace="&xml;">
                    <xsl:value-of select="$defaultLanguage"/>
                  </xsl:attribute>
                  <xsl:value-of select="@xbrl:label"/>
                </xbrl:label>
              </xsl:if>
              <xsl:copy-of select="xbrl:label[@xml:lang!=$language]"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template name="decideAttributes">
    <xsl:param name="newDefault"/>
    <xsl:param name="oldDefault"/>
    <xsl:param name="attribute"/>
    <xsl:param name="localName"/>
    <xsl:choose>
      <xsl:when test="not($newDefault = $oldDefault)">
        <xsl:choose>
          <xsl:when test="$attribute">
            <xsl:if test="not(string($attribute) = $newDefault)">
              <xsl:copy-of select="$attribute"/>
            </xsl:if>
          </xsl:when>
          <xsl:otherwise>
            <xsl:attribute name="{$localName}" namespace="&xbrl;">
              <xsl:value-of select="$oldDefault"/>
            </xsl:attribute>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy-of select="$attribute"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:transform>