<!-- $Id: grokData.xsl,v 1.1 2005/01/19 11:17:04 connolly Exp $ -->
<xsl:transform
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:d="http://www.w3.org/2000/10/swap/util/data#"
  xmlns:r="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  >

<xsl:output method="xml" indent="yes" />

<xsl:template match="d:data">
  <r:RDF>
    <xsl:apply-templates />
  </r:RDF>
</xsl:template>

<xsl:template match="d:thing">
  <r:Description>
    <xsl:if test="@ref">
      <xsl:attribute name="r:about">
	<xsl:value-of select="@ref"/>
      </xsl:attribute>
    </xsl:if>
    <xsl:apply-templates/>
  </r:Description>
</xsl:template>

<xsl:template match="d:item">
  <xsl:variable name="t" select='*[1]'/>
  <xsl:variable name="ns" select='namespace-uri($t)'/>
  <xsl:variable name="n" select='concat($ns, local-name($t))'/>
  
  <r:Description r:about='{$n}'>
    <xsl:for-each select="*[position() &gt; 1]">
      <xsl:apply-templates select="."/>
    </xsl:for-each>
  </r:Description>
</xsl:template>

<xsl:template match="d:rel">
  <xsl:variable name="n" select="@ref" /> <!-- @@expand URI ref. xml:base? -->
  <xsl:variable name="ns">http://example/stuff-before-split@@#</xsl:variable>
  <xsl:variable name="ln"><!-- kinda bogus alg. @@ -->
    <xsl:choose>
      <xsl:when test='contains($n, "#")'>
	<xsl:value-of select='substring-after($n, "#")'/> 
      </xsl:when>
      <xsl:otherwise>
	<xsl:value-of select='"oops"'/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:for-each select="*">
    <xsl:element namespace="{$ns}"
		 name="{$ln}">
      <xsl:apply-templates select="."/>
    </xsl:element>
  </xsl:for-each>
</xsl:template>

<xsl:template match="d:rev">
  <xsl:comment>oops! rev is hard!@@</xsl:comment>
</xsl:template>

<xsl:template match="d:prop">
  <xsl:variable name="t" select="*[1]" />
  <xsl:variable name="ns" select="namespace-uri($t)"/>
  <xsl:variable name="ln" select="local-name($t)"/>

  <xsl:for-each select="*[ position() &gt; 1]">
    <xsl:element namespace="{$ns}"
		 name="{$ln}">
      <xsl:apply-templates select="."/>
    </xsl:element>
  </xsl:for-each>
</xsl:template>

<xsl:template match="d:val">
  <xsl:attribute name="r:datatype">http://www.w3.org/2001/XMLSchema#integer</xsl:attribute>
  <xsl:value-of select="@int"/> <!-- @@ other types -->
</xsl:template>

<xsl:template match="d:txt">
  <xsl:attribute name="r:parseType">Literal</xsl:attribute>
  <xsl:copy-of select="."/> <!-- actually the children @@ -->
</xsl:template>

<xsl:template match="d:str">
  <xsl:value-of select="."/>
</xsl:template>

<xsl:template match="*">
  <xsl:message>
    oops! no template for <xsl:value-of select="name()"/> element in <xsl:value-of select="name(..)"/>
  </xsl:message>
</xsl:template>

</xsl:transform>


