<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
  <xsl:import href="xmlspec-trdoc-data.xsl"/>
  <xsl:import href="rdfize.xsl"/>
  <xsl:import href="glossary-util.xsl"/>
  <xsl:output method="xml" media-type="text/xml" encoding="utf-8" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:variable name="latest_version">
    <xsl:call-template name="getVersionURI">
      <xsl:with-param name="which" select="'latest'"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="this_version">
    <xsl:call-template name="getVersionURI">
      <xsl:with-param name="which" select="'this'"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="numeric_date">
    <xsl:call-template name="getNumericDate"/>
  </xsl:variable>

  <xsl:template match="/">
    <xsl:call-template name="createSchema">
      <xsl:with-param name="about" select="$latest_version"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="addDocumentData">
    <xsl:call-template name="addDate">
      <xsl:with-param name="date" select="$numeric_date"/>
    </xsl:call-template>

    <xsl:call-template name="addTitle">
      <xsl:with-param name="title">
        <xsl:call-template name="getTitle"/>
      </xsl:with-param>
    </xsl:call-template>

    <xsl:call-template name="addVersion">
      <xsl:with-param name="version" select="$this_version"/>
    </xsl:call-template>

    <xsl:call-template name="getEditorsList"/>
  </xsl:template>

  <xsl:template name="addGlossaryData">
    <xsl:variable name="RDF_Schema_URI">
      <xsl:call-template name="getRDFSchemaURI">
        <xsl:with-param name="date" select="$numeric_date"/>
        <xsl:with-param name="uri" select="$latest_version"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:apply-templates select="/spec/body//glist/gitem|/spec/body//termdef[term]">
      <xsl:with-param name="uri" select="$RDF_Schema_URI"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="gitem">
    <xsl:param name="uri"/>
    <xsl:variable name="tmp" select="normalize-space(label)"/>
    <xsl:variable name="test">
      <xsl:call-template name="isTermValid">
        <xsl:with-param name="str" select="$tmp"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:if test="normalize-space($test)">
      <xsl:call-template name="addTermEntry">
        <xsl:with-param name="term" select="$tmp"/>
        <xsl:with-param name="definition" select="normalize-space(def)"/>
        <xsl:with-param name="about" select="$uri"/>
        <xsl:with-param name="isDefinedBy" select="$this_version"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <xsl:template match="termdef[term]">
    <xsl:param name="uri"/>
    <xsl:variable name="test">
      <xsl:call-template name="isTermValid">
        <xsl:with-param name="str" select="normalize-space(term)"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:if test="normalize-space($test)">
      <xsl:call-template name="addTermEntry">
        <xsl:with-param name="term" select="normalize-space(term)"/>
        <xsl:with-param name="definition" select="normalize-space(.)"/>
        <xsl:with-param name="about" select="$uri"/>
        <xsl:with-param name="isDefinedBy" select="$this_version"/>
      </xsl:call-template>        
    </xsl:if>

  </xsl:template>

  <xsl:template name="formatEditorName">
    <xsl:param name="EditorName"/>
    <xsl:param name="EditorMailbox"/>
    <xsl:call-template name="addEditor">
      <xsl:with-param name="full_name" select="$EditorName"/>
      <xsl:with-param name="mail" select="$EditorMailbox"/>
    </xsl:call-template>
  </xsl:template>
</xsl:stylesheet>