<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:raa="http://www.ruby-lang.org/xmlns/soap/interface/RAA/0.0.1#"
  xmlns="http://www.ruby-lang.org/xmlns/soap/interface/RAA/0.0.1#"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  version="1.0">

  <xsl:param name="body-namespace-prefix" select="'n2'"/>
  <xsl:param name="body-namespace-uri" select="'http://www.ruby-lang.org/xmlns/soap/interface/RAA/0.0.1'"/>
  <!--  <xsl:param name="schema-types-namespace-prefix" select="'xsd'"/> -->
  <xsl:param name="schema-types-namespace-uri" select="'http://www.w3.org/2001/XMLSchema'"/>


  <xsl:output indent="yes"/>

  <xsl:template match="/">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="env:Envelope">
    <rdf:RDF env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
      <xsl:apply-templates select="env:Body/*"/>
    </rdf:RDF>
  </xsl:template>

  <xsl:template match="env:Body/*">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>


  <!-- now on to the meaty stuff -->

  <xsl:template match="*">
    <xsl:copy>
      <xsl:if test="@xsi:type">
        <!-- test whether this is a literal -->
        <xsl:choose>
          <xsl:when test="count(*)=0">
            <xsl:attribute name="rdf:datatype">
              <xsl:value-of select="concat($schema-types-namespace-uri,'#',substring-after(@xsi:type,':'))"/>
            </xsl:attribute>
          </xsl:when>
          <xsl:otherwise>
            <xsl:if test="starts-with(@xsi:type,concat($body-namespace-prefix,':'))">
              <xsl:attribute name="rdf:parseType">Resource</xsl:attribute>
              
              <!-- dump the type as first child -->
              <rdf:type rdf:resource="{$body-namespace-uri}#{substring-after(@xsi:type,':')}"/>
            </xsl:if>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:if>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
  
</xsl:stylesheet>
