<!-- 
RdfXStoNTriple.xsl - XSLT to transform RDF to database API calls.
description and revision history at http://www.w3.org/2002/03/11-RDF-XSL/
$Id: RdfXStoNTriple.xsl,v 1.7 2004/02/09 23:51:59 eric Exp $
-->
<xsl:transform version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:r="http://www.w3.org/1999/02/22-rdf-syntax-ns#">

  <!-- re-use Evan Lenz's xml-to-string from http://www.xmlportfolio.com/xml-to-string -->
  <xsl:import href="xml-to-string.xsl"/>
  <!-- overloaded global parameters -->
  <xsl:param name="attribute-delimiter" select="'\&quot;'"/>
  <!-- overloaded templates from xml-to-string -->
  <xsl:template match="/" name="xml-to-string-root-rule">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template name="escape-markup-characters">
    <xsl:param name="text"/>
    <xsl:variable name="ampEscaped">
      <xsl:call-template name="replace-string">
        <xsl:with-param name="text" select="$text"/>
        <xsl:with-param name="replace" select="'&amp;'"/>
        <xsl:with-param name="with" select="'&amp;amp;'"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="ltEscaped">
      <xsl:call-template name="replace-string">
        <xsl:with-param name="text" select="$ampEscaped"/>
        <xsl:with-param name="replace" select="'&lt;'"/>
        <xsl:with-param name="with" select="'&amp;lt;'"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="cDataClose">
      <xsl:call-template name="replace-string">
	<xsl:with-param name="text" select="$ltEscaped"/>
	<xsl:with-param name="replace" select="']]>'"/>
	<xsl:with-param name="with" select="']]&amp;gt;'"/>
      </xsl:call-template>
    </xsl:variable>

    <!-- overloaded to add N-Triples string encoding:
         http://www.w3.org/2001/sw/RDFCore/ntriples/#sec-string1 -->
    <xsl:variable name="backslash">
      <xsl:call-template name="replace-string">
	<xsl:with-param name="text" select="$cDataClose"/>
	<xsl:with-param name="replace" select="'\'"/>
	<xsl:with-param name="with" select="'\\'"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="quote">
      <xsl:call-template name="replace-string">
	<xsl:with-param name="text" select="$backslash"/>
	<xsl:with-param name="replace" select="'&#x22;'"/>
	<xsl:with-param name="with" select="'\&#x22;'"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="linefeed">
      <xsl:call-template name="replace-string">
	<xsl:with-param name="text" select="$quote"/>
	<xsl:with-param name="replace" select="'&#xA;'"/>
	<xsl:with-param name="with" select="'\n'"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="return">
      <xsl:call-template name="replace-string">
	<xsl:with-param name="text" select="$linefeed"/>
	<xsl:with-param name="replace" select="'&#xD;'"/>
	<xsl:with-param name="with" select="'\r'"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:call-template name="replace-string">
      <xsl:with-param name="text" select="$return"/>
      <xsl:with-param name="replace" select="'&#x9;'"/>
      <xsl:with-param name="with" select="'\t'"/>
    </xsl:call-template>
  </xsl:template>

  <!-- end xml-to-string -->

  <!-- start document -->
  <xsl:template name="startDocument"/>

  <!-- end document -->
  <xsl:template name="endDocument"/>

  <!-- generate a triple and add it to knowledge base -->
  <xsl:template name="addTriple">
    <xsl:param name="predicate"/>
    <xsl:param name="subject"/>
    <xsl:param name="object"/>

    <xsl:value-of select="$subject"/>
    <xsl:text> </xsl:text>
    <xsl:value-of select="$predicate"/>
    <xsl:text> </xsl:text>
    <xsl:value-of select="$object"/>
    <xsl:text> .&#xA;</xsl:text>
  </xsl:template>


  <!-- addLiteralTriple - encode nested elements and scoop out namespace -->
  <xsl:template name="addLiteralTriple">
    <xsl:param name="predicate"/>
    <xsl:param name="subject"/>
    <xsl:value-of select="$subject"/>
    <xsl:text> </xsl:text>
    <xsl:value-of select="$predicate"/>
    <xsl:text> literal("</xsl:text>
    <xsl:for-each select="./*">
      <xsl:call-template name="xml-to-string"/>
    </xsl:for-each>
    <xsl:text>", [</xsl:text>
    <xsl:for-each select="./namespace::*">
      <xsl:text>{"</xsl:text>
      <xsl:value-of select="name()"/>
      <xsl:text>", "</xsl:text>
      <xsl:value-of select="."/>
      <xsl:text>"}, </xsl:text>
    </xsl:for-each>
    <xsl:text>] .&#xA;</xsl:text>
  </xsl:template>

  <!-- addTriple_lit - shortcut for adding triples with a literal object -->
  <xsl:template name="addTriple_lit">
    <xsl:param name="predicate"/>
    <xsl:param name="subject"/>
    <xsl:param name="object"/>
    <xsl:call-template name="addTriple">
      <xsl:with-param name="predicate" select="$predicate"/>
      <xsl:with-param name="subject" select="$subject"/>
      <xsl:with-param name="object" select="concat('&quot;', $object, '&quot;')"/>
    </xsl:call-template>
  </xsl:template>

  <!-- addTriple_id - shortcut for adding triples with an XML:ID object -->
  <xsl:template name="addTriple_id">
    <xsl:param name="predicate"/>
    <xsl:param name="subject"/>
    <xsl:param name="object"/>
    <xsl:call-template name="addTriple">
      <xsl:with-param name="predicate" select="$predicate"/>
      <xsl:with-param name="subject" select="$subject"/>
      <xsl:with-param name="object" select="concat('&lt;', $baseURI, '#', $object, '&gt;')"/>
    </xsl:call-template>
  </xsl:template>

  <!-- addTriple_ref - shortcut for adding triples with an href object -->
  <xsl:template name="addTriple_ref">
    <xsl:param name="predicate"/>
    <xsl:param name="subject"/>
    <xsl:param name="object"/>
    <xsl:call-template name="addTriple">
      <xsl:with-param name="predicate" select="$predicate"/>
      <xsl:with-param name="subject" select="$subject"/>
      <xsl:with-param name="object" select="concat('&lt;', $baseURI, $object, '&gt;')"/>
    </xsl:call-template>
  </xsl:template>

  <!-- error - encode nested elements and scoop out namespace -->
  <xsl:template name="error">
    <xsl:param name="hint"/>
    <xsl:param name="expected"/>
    <xsl:message terminate="yes">
      <xsl:value-of select="concat('**Fatal error: ', $hint, '**&#xA;', $expected, '&#xA;')"/>
      <xsl:for-each select=".">
	<xsl:call-template name="xml-to-string"/>
      </xsl:for-each>
      <xsl:value-of select="'&#xA;'"/>
    </xsl:message>
  </xsl:template>

</xsl:transform>

