<?xml version="1.0" encoding="UTF-8"?>
<!-- xml2rdf1.xsl       XSLT stylesheet to transform XML into RDF/XML

     Version             1.0  (2007-03-26)
     Features            elements and attributes become literals connected by
                         blank nodes similar to the Java tool OwlMap .
     Web page            http://www.gac-grid.org/project-products/Software/XML2RDF.html
     Usage               xsltproc xml2rdf1.xsl file.xml
     Authors             Frank Breitling (fbreitling at aip.de)
                         Michael Braun (mbraun at aip.de)
     Copyright 2007      AstroGrid-D (http://www.gac-grid.org/) 
 
     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
 
         http://www.apache.org/licenses/LICENSE-2.0
 
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
 -->

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
       xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">

       <xsl:strip-space elements="*"/>
       <xsl:output method="xml" indent="yes"/>

       <!-- Begin RDF document -->
       <xsl:template match="/">
              <xsl:element name="rdf:RDF">
                     <rdf:Description rdf:about="{*/@uid}">
                            <xsl:apply-templates/>
                     </rdf:Description>
              </xsl:element>
       </xsl:template>

       <!-- Turn nodes into blank nodes and keep their namespaces -->
       <xsl:template match="*">
              <xsl:element name="{name()}" namespace="{namespace-uri()}#">
                     <xsl:attribute name="rdf:parseType">Resource</xsl:attribute>
                     <xsl:apply-templates select="node()|@*"/>
              </xsl:element>
       </xsl:template>

       <!-- Convert any attribute into an element and apply namespace-uri -->
       <xsl:template match="@*">
              <xsl:element name="{name()}" namespace="{namespace-uri(..)}#">
                     <xsl:value-of select="."/>
              </xsl:element>
       </xsl:template>

       <!-- Enclose text nodes in value tags -->
       <xsl:template match="text()">
              <!-- Retrieve the namespace prefix of the parent node and compose
                   the name of the new element as 'prefix:value'.
                   If there is no prefix, the name is simply 'value'. -->
              <xsl:variable name="valname">
                     <xsl:if test="contains(name(..),':')">
                            <xsl:value-of select="substring-before(name(..),':')"/>
                            <xsl:text>:</xsl:text>
                     </xsl:if>
                     <xsl:text>value</xsl:text>
              </xsl:variable>

              <!-- Create a new element from the text node using
               $valname and the namespace of the parent node -->
              <xsl:element name="{$valname}" namespace="{namespace-uri(..)}#">
                     <xsl:value-of select="."/>
              </xsl:element>
       </xsl:template>

</xsl:stylesheet>
