<!-- instance2rdf.xsl: convert an XBRL instance to RDF/XML. 
     2008-09-22 Bob DuCharme no warrantee expressed or implied. -->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xi="http://www.xbrl.org/2003/instance"
                xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                xmlns:link="http://www.xbrl.org/2003/linkbase"
                version="1.0">

  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>
  <!--

sample XBRL fact used as input: 

      <us-gaap:SharesOutstanding
         contextRef="x1"
         unitRef="Shares"
         decimals="-6">485000000</us-gaap:SharesOutstanding>

In addition to expressing the element name, decimals and PCDATA as
RDF, the stylesheet looks up the relevant context and unit elements
and pulls the relevant information to express as triples about the
same fact.

  -->

  <xsl:template match="xi:xbrl">
    <rdf:RDF xmlns="http://www.xbrl.org/2003/instance"
             xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
      <xsl:apply-templates/>
    </rdf:RDF>
  </xsl:template>


  <!-- Process all elements that have some content but no children. -->
  <xsl:template match="*[normalize-space(.) and not(*)]">

    <xsl:variable name="contextID" select="@contextRef"/>

    <!-- Some URLs are created by adding a value to another URL, so
         the next few variables calculate wither a # is necessary at
         the end of that URL. -->

    <xsl:variable name="elNamespaceLastChar"
                  select="substring(namespace-uri(),
                          string-length(namespace-uri()))"/>

    <xsl:variable name="URLDelimiter">
      <xsl:if test="($elNamespaceLastChar != '#') and 
                    ($elNamespaceLastChar != '/')">#</xsl:if>
    </xsl:variable>

<!-- So // isn't the most efficient thing to use in XBRL, but the XBRL
     instance documents are small enough that it's not a huge waste of
     cycles. -->
    <xsl:variable name="identifierScheme"
                  select="//xi:context[@id=$contextID]/xi:entity/xi:identifier/@scheme"/>

    <xsl:variable name="identifierNamespaceLastChar"
                  select="substring($identifierScheme,
                          string-length($identifierScheme))"/>

    <xsl:variable name="identifierDelimiter">
      <xsl:if test="($identifierNamespaceLastChar != '#') and 
                    ($identifierNamespaceLastChar != '/')">#</xsl:if>
    </xsl:variable>

    <xsl:variable name="identifier"
                  select="concat($identifierScheme,$identifierDelimiter,
                          //xi:context[@id=$contextID]/xi:entity/xi:identifier)"/>

    <rdf:Description rdf:ID="_{generate-id()}" rdf:value="{.}">
      <xsl:apply-templates select="@*"/>
      <rdf:type rdf:resource="{concat(namespace-uri(),$URLDelimiter,
                              local-name())}"/>
      <xsl:if test="@contextRef">
        <xi:identifier rdf:resource="{$identifier}">
        </xi:identifier>
      </xsl:if>
    </rdf:Description>

  </xsl:template>


  <xsl:template match="@*">

    <xsl:choose>

      <xsl:when test="name() = local-name()">
        <!-- If it's not in some namespace, put it in xbrl instance one -->
        <xsl:attribute name="xi:{name()}">
          <xsl:value-of select="."/>
        </xsl:attribute>
      </xsl:when>

      <xsl:otherwise>
        <xsl:attribute name="{name()}">
          <xsl:value-of select="."/>
        </xsl:attribute>
      </xsl:otherwise>

    </xsl:choose>

  </xsl:template>


  <!--
      Convert

     unitRef="USD"

and

      <unit id="USD">
        <measure>iso4217:USD</measure>
      </unit>

to 

      xi:measure="iso4217:USD"

  -->
  <xsl:template match="@unitRef">
    <xsl:variable name="attVal" select="."/>
    <xsl:attribute name="xi:{local-name(//xi:unit[@id=$attVal]/*)}">
      <xsl:value-of select="//xi:unit[@id=$attVal]"/>
    </xsl:attribute>
  </xsl:template>

  <!-- 
Convert 

      contextRef="eol_0001193125-08-104278_STD_p3m_20070331_4"

and

      <context id="eol_0001193125-08-104278_STD_p3m_20080331_4">
        <entity>
          <identifier scheme="http://www.sec.gov/CIK">0000029669</identifier>
        </entity>
        <period>
          <startDate>2008-01-01</startDate>
          <endDate>2008-03-31</endDate>
        </period>
      </context>

to 

      xi:identifier="http://www.sec.gov/CIK#0000029669"
      xi:startDate="2008-01-01"
      xi:endDate="2008-01-01"

  -->

  <xsl:template match="@contextRef">
    <xsl:variable name="attVal" select="."/>

    <!-- Following used for debugging
         <xsl:attribute name="xi:contextRef">
         <xsl:value-of select="."/>
         </xsl:attribute>
    -->

    <xsl:apply-templates mode="element2attribute"
                         select="//xi:context[@id=$attVal]/xi:period/xi:startDate |
                                 //xi:context[@id=$attVal]/xi:period/xi:endDate | 
                                 //xi:context[@id=$attVal]/xi:period/xi:instant"/>

  </xsl:template>


  <xsl:template match="*" mode="element2attribute">
    <xsl:attribute name="xi:{local-name()}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>


  <xsl:template match="xi:context|xi:unit|link:footnoteLink"/>


</xsl:stylesheet>
