<?xml version="1.0"?> 

<!DOCTYPE uridef[
  <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns">
  <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema">
  <!ENTITY owl "http://www.w3.org/2002/07/owl">
  <!ENTITY xsd "http://www.w3.org/2001/XMLSchema">
  <!ENTITY swrl "http://www.w3.org/2003/11/swrl">  
  <!ENTITY ruleml "http://www.ruleml.org/ruleml">
  <!ENTITY nbsp "&#160;">  
]>

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:rdf="&rdf;#"
 xmlns:rdfs="&rdfs;#"
 xmlns:owl="&owl;#"
 xmlns:xsd="&xsd;#"
 xmlns:swrl="&swrl;#" 
 xmlns:ruleml="&ruleml;#" 
 exclude-result-prefixes="rdf rdfs owl xsd xsl ruleml swrl"
 >
 
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
  <xsl:template match="/"><xsl:apply-templates /></xsl:template>
  <xsl:template match="rdf:RDF"><xsl:apply-templates /></xsl:template>
 

<xsl:template match="swrl:Imp">
<Implies><xsl:text xml:space="preserve"> 
  </xsl:text><body>
    <xsl:apply-templates select="swrl:body" /><xsl:text xml:space="preserve"> 
  </xsl:text></body><xsl:text xml:space="preserve"> 
  </xsl:text><head>
    <xsl:apply-templates select="swrl:head" /><xsl:text xml:space="preserve"> 
  </xsl:text></head>
</Implies>
</xsl:template>


  <!-- declare body -->
<xsl:template match="swrl:body">
    <xsl:choose>
      <xsl:when test="@rdf:parseType"><xsl:text xml:space="preserve"> 
    </xsl:text><And><xsl:for-each select="."><xsl:apply-templates /></xsl:for-each>
    <xsl:text xml:space="preserve">  </xsl:text></And>
      </xsl:when>
      <xsl:otherwise>
		<xsl:apply-templates />
	  </xsl:otherwise>                    
    </xsl:choose>
</xsl:template>  

  <!-- declare head -->
<xsl:template match="swrl:head">
    <xsl:choose>
      <xsl:when test="@rdf:parseType"><xsl:text xml:space="preserve"> 
    </xsl:text><And><xsl:for-each select="."><xsl:apply-templates /></xsl:for-each>
    <xsl:text xml:space="preserve">  </xsl:text></And>
      </xsl:when>
      <xsl:otherwise>
		<xsl:apply-templates />
	  </xsl:otherwise>                    
    </xsl:choose>
</xsl:template>  

  <!-- declare AtomList -->
<xsl:template match="swrl:AtomList">
    <And><xsl:for-each select="."><xsl:apply-templates /></xsl:for-each>
    </And>
</xsl:template>  

  <!-- declare first -->
<xsl:template match="rdf:first">
  <xsl:apply-templates />
</xsl:template>  

  <!-- declare rest -->
<xsl:template match="rdf:rest">
  <xsl:apply-templates />
</xsl:template>  

  <!-- declare class atom -->
<xsl:template match="swrl:ClassAtom">
    <Atom><xsl:text xml:space="preserve"> 
      </xsl:text><Rel wref="swrl:ClassAtom"/><xsl:text xml:space="preserve"> 
      </xsl:text><slot><xsl:text xml:space="preserve"> 
        </xsl:text><Ind wref="swrl:classPredicate"/><xsl:text xml:space="preserve"> 
        </xsl:text><Ind><xsl:apply-templates select="swrl:classPredicate" /></Ind><xsl:text xml:space="preserve"> 
      </xsl:text></slot><xsl:text xml:space="preserve"> 
      </xsl:text><slot><xsl:text xml:space="preserve"> 
        </xsl:text><Ind wref="swrl:argument1"/><xsl:text xml:space="preserve"> 
        </xsl:text><Var><xsl:apply-templates select="swrl:argument1" /></Var><xsl:text xml:space="preserve"> 
      </xsl:text></slot><xsl:text xml:space="preserve"> 
    </xsl:text></Atom>
</xsl:template>  

  <!-- declare individual property atom -->
<xsl:template match="swrl:IndividualPropertyAtom">
    <Atom><xsl:text xml:space="preserve"> 
      </xsl:text><Rel wref="swrl:IndividualPropertyAtom"/><xsl:text xml:space="preserve"> 
      </xsl:text><slot><xsl:text xml:space="preserve"> 
        </xsl:text><Ind wref="swrl:propertyPredicate"/><xsl:text xml:space="preserve"> 
        </xsl:text><Ind><xsl:apply-templates select="swrl:propertyPredicate" /></Ind><xsl:text xml:space="preserve"> 
      </xsl:text></slot><xsl:text xml:space="preserve"> 
      </xsl:text><slot><xsl:text xml:space="preserve"> 
        </xsl:text><Ind wref="swrl:argument1"/><xsl:text xml:space="preserve"> 
        </xsl:text><Var><xsl:apply-templates select="swrl:argument1" /></Var><xsl:text xml:space="preserve"> 
      </xsl:text></slot><xsl:text xml:space="preserve"> 
      </xsl:text><slot><xsl:text xml:space="preserve"> 
        </xsl:text><Ind wref="swrl:argument2"/><xsl:text xml:space="preserve"> 
        </xsl:text><Var><xsl:apply-templates select="swrl:argument2" /></Var><xsl:text xml:space="preserve"> 
      </xsl:text></slot><xsl:text xml:space="preserve"> 
    </xsl:text></Atom>
</xsl:template>  

  <!-- declare datavalued property atom -->
<xsl:template match="swrl:DatavaluedPropertyAtom">
    <Atom><xsl:text xml:space="preserve"> 
      </xsl:text><Rel wref="swrl:DatavaluedPropertyAtom"/><xsl:text xml:space="preserve"> 
      </xsl:text><slot><xsl:text xml:space="preserve"> 
        </xsl:text><Ind wref="swrl:propertyPredicate"/><xsl:text xml:space="preserve"> 
        </xsl:text><Ind><xsl:apply-templates select="swrl:propertyPredicate" /></Ind><xsl:text xml:space="preserve"> 
      </xsl:text></slot><xsl:text xml:space="preserve"> 
      </xsl:text><slot><xsl:text xml:space="preserve"> 
        </xsl:text><Ind wref="swrl:argument1"/><xsl:text xml:space="preserve"> 
        </xsl:text><Var><xsl:apply-templates select="swrl:argument1" /></Var><xsl:text xml:space="preserve"> 
      </xsl:text></slot><xsl:text xml:space="preserve"> 
      </xsl:text><slot><xsl:text xml:space="preserve"> 
        </xsl:text><Ind wref="swrl:argument2"/><xsl:text xml:space="preserve"> 
        </xsl:text><Var><xsl:apply-templates select="swrl:argument2" /></Var><xsl:text xml:space="preserve"> 
      </xsl:text></slot><xsl:text xml:space="preserve"> 
    </xsl:text></Atom>
</xsl:template> 

  <!-- declare same individual atom -->
<xsl:template match="swrl:SameIndividualAtom">
    <Atom><xsl:text xml:space="preserve"> 
      </xsl:text><Rel wref="swrl:SameIndividualAtom"/><xsl:text xml:space="preserve"> 
      </xsl:text><slot><xsl:text xml:space="preserve"> 
        </xsl:text><Ind wref="swrl:argument1"/><xsl:text xml:space="preserve"> 
        </xsl:text><Var><xsl:apply-templates select="swrl:argument1" /></Var><xsl:text xml:space="preserve"> 
      </xsl:text></slot><xsl:text xml:space="preserve"> 
      </xsl:text><slot><xsl:text xml:space="preserve"> 
        </xsl:text><Ind wref="swrl:argument2"/><xsl:text xml:space="preserve"> 
        </xsl:text><Var><xsl:apply-templates select="swrl:argument2" /></Var><xsl:text xml:space="preserve"> 
      </xsl:text></slot><xsl:text xml:space="preserve"> 
    </xsl:text></Atom>
</xsl:template> 

  <!-- declare different individual atom -->
<xsl:template match="swrl:DifferentIndividualsAtom">
    <Atom><xsl:text xml:space="preserve"> 
      </xsl:text><Rel wref="swrl:DifferentIndividualsAtom"/><xsl:text xml:space="preserve"> 
      </xsl:text><slot><xsl:text xml:space="preserve"> 
        </xsl:text><Ind wref="swrl:argument1"/><xsl:text xml:space="preserve"> 
        </xsl:text><Var><xsl:apply-templates select="swrl:argument1" /></Var><xsl:text xml:space="preserve"> 
      </xsl:text></slot><xsl:text xml:space="preserve"> 
      </xsl:text><slot><xsl:text xml:space="preserve"> 
        </xsl:text><Ind wref="swrl:argument2"/><xsl:text xml:space="preserve"> 
        </xsl:text><Var><xsl:apply-templates select="swrl:argument2" /></Var><xsl:text xml:space="preserve"> 
      </xsl:text></slot><xsl:text xml:space="preserve"> 
    </xsl:text></Atom>
</xsl:template> 

  <!-- declare class predicate -->
<xsl:template match="swrl:classPredicate">
    <xsl:call-template name="get-child-ID"/>
</xsl:template>  

  <!-- declare property predicate -->
<xsl:template match="swrl:propertyPredicate">
    <xsl:call-template name="get-child-ID"/>
</xsl:template>   

  <!-- declare argument 1 -->
<xsl:template match="swrl:argument1">
    <xsl:call-template name="get-argument"/>
</xsl:template>  
  
  <!-- declare argument 2 -->
<xsl:template match="swrl:argument2">
    <xsl:call-template name="get-argument"/>
</xsl:template> 


  <!-- named templates - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->



  <!-- get ID or About of the current node -->
  <xsl:template name="get-ID" >
    
    <xsl:choose>
      <xsl:when test="@rdf:ID">
        <xsl:call-template name="get-namespace-ID"><xsl:with-param name="id"><xsl:value-of select="@rdf:ID"/></xsl:with-param></xsl:call-template> 
      </xsl:when>
      <xsl:when test="@rdf:about">
        <xsl:call-template name="get-namespace-ID"><xsl:with-param name="id"><xsl:value-of select="@rdf:about"/></xsl:with-param></xsl:call-template> 
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="generate-id(.)"/>
      </xsl:otherwise>
    </xsl:choose> 
  </xsl:template>


  <!-- get datavalue of the current node -->
  <xsl:template name="get-datavalue" >
    
    <xsl:choose>
      <xsl:when test="@rdf:datatype">
        <xsl:value-of select="concat(normalize-space(.),'^^xsd:',substring-after(@rdf:datatype,'#'))"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="normalize-space(.)"/>
      </xsl:otherwise>
    </xsl:choose> 
  </xsl:template>
    
  <!-- get the ID or About of the parent node -->
  <xsl:template name="get-father-ID" >
    
    <xsl:choose>
      <xsl:when test="../@rdf:ID">
        <xsl:call-template name="get-namespace-ID"><xsl:with-param name="id"><xsl:value-of select="../@rdf:ID"/></xsl:with-param></xsl:call-template> 
      </xsl:when>
      <xsl:when test="../@rdf:about">
        <xsl:call-template name="get-namespace-ID"><xsl:with-param name="id"><xsl:value-of select="../@rdf:about"/></xsl:with-param></xsl:call-template> 
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="generate-id(..)"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <!-- get the ID or About of the grandparent node -->
  <xsl:template name="get-grandfather-ID" >
    
    <xsl:choose>
      <xsl:when test="../../@rdf:ID">
        <xsl:call-template name="get-namespace-ID"><xsl:with-param name="id"><xsl:value-of select="../../@rdf:ID"/></xsl:with-param></xsl:call-template> 
      </xsl:when>
      <xsl:when test="../../@rdf:about">
        <xsl:call-template name="get-namespace-ID"><xsl:with-param name="id"><xsl:value-of select="../../@rdf:about"/></xsl:with-param></xsl:call-template> 
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="generate-id(../..)"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  
  <!-- get the ID or About of the first child  -->

  <xsl:template name="get-child-ID" >
    <xsl:choose>
      <xsl:when test="@rdf:resource">
        <xsl:call-template name="get-namespace-ID"><xsl:with-param name="id"><xsl:value-of select="@rdf:resource"/></xsl:with-param></xsl:call-template> 
      </xsl:when>
      <xsl:when test="*[position()=1]/@rdf:ID">
        <xsl:call-template name="get-namespace-ID"><xsl:with-param name="id"><xsl:value-of select="*[position()=1]/@rdf:ID"/></xsl:with-param></xsl:call-template> 
      </xsl:when>
      <xsl:when test="*[position()=1]/@rdf:about">
        <xsl:call-template name="get-namespace-ID"><xsl:with-param name="id"><xsl:value-of select="*[position()=1]/@rdf:about"/></xsl:with-param></xsl:call-template> 
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="generate-id(*[position()=1])"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template name="get-namespace-ID" >
   <xsl:param name="id" />
    <xsl:choose>
      <xsl:when test="starts-with($id,'&rdf;#')">
        <xsl:value-of select="concat('rdf:',substring-after($id,'#'))"/> 
      </xsl:when>
      <xsl:when test="starts-with($id,'&rdfs;#')">
        <xsl:value-of select="concat('rdfs:',substring-after($id,'#'))"/> 
      </xsl:when>
      <xsl:when test="starts-with($id,'&owl;#')">
        <xsl:value-of select="concat('owl:',substring-after($id,'#'))"/> 
      </xsl:when>  
      <xsl:when test="starts-with($id,'&xsd;#')">
        <xsl:value-of select="concat('xsd:',substring-after($id,'#'))"/> 
      </xsl:when> 
      <xsl:otherwise>
		<xsl:value-of select="$id"/>
	  </xsl:otherwise>                    
    </xsl:choose>
  </xsl:template>
  
  <xsl:template name="get-argument" >
    <xsl:choose>
      <xsl:when test="@rdf:resource">
        <xsl:value-of select="concat('?',substring-after(@rdf:resource,'#'))"/>
      </xsl:when>
      <xsl:when test="*[position()=1]/@rdf:ID">
        <xsl:value-of select="concat('?',*[position()=1]/@rdf:ID)"/>
      </xsl:when>
      <xsl:when test="*[position()=1]/@rdf:about">
        <xsl:value-of select="concat('?',*[position()=1]/@rdf:about)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="concat('?',generate-id(*[position()=1]))"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>   
    
  <xsl:template match="*" />
  
   
</xsl:stylesheet>