<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:trd="http://www.w3.org/2001/10/trdoc-data.xsl"
    xmlns:str="http://www.w3.org/2001/10/str-util.xsl"
  xmlns:uri="http://www.w3.org/2000/07/uri43/uri.xsl?template="
    xmlns:html="http://www.w3.org/1999/xhtml"
    xmlns="http://www.w3.org/1999/xhtml"
    exclude-result-prefixes="trd html str uri"
    version="1.0">
  <xsl:import href="http://www.w3.org/2000/07/uri43/uri.xsl" />
  <xsl:import href="http://www.w3.org/2001/10/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:param name="doc_uri"/>

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

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

  <xsl:variable name="numeric_date">
    <xsl:call-template name="trd: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="trd: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="trd: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="//html:dl[not(ancestor::*[@class='head'])][@class='glossary']/html:dt" mode="glossary">
      <xsl:with-param name="uri" select="$RDF_Schema_URI"/>
    </xsl:apply-templates>
    <xsl:apply-templates select="//html:dl[not(ancestor::*[@class='head'])][not(@class='glossary')]/html:dt[not(child::html:code[@class='tag'])]" mode="not_glossary">
      <xsl:with-param name="uri" select="$RDF_Schema_URI"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="*|@*|text()" mode="copy">
    <xsl:copy>
      <xsl:apply-templates select="*|@*|text()" mode="copy"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="html:a[@href]" mode="copy">
    <xsl:copy>
      <xsl:attribute name="href">
        <xsl:call-template name="uri:expand">
          <xsl:with-param name="base"
            select="$doc_uri"/>
          <xsl:with-param name="there"
            select="@href"/>
        </xsl:call-template>
      </xsl:attribute>
      <xsl:apply-templates select="*|@*[not(local-name()='href')]|text()" mode="copy"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="html:dt" mode="glossary">
    <xsl:param name="uri"/>
    <xsl:variable name="tmp" select="generate-id(.)"/>
    <xsl:variable name="term">
      <xsl:choose>
        <xsl:when test="html:a/html:dfn">
          <xsl:value-of select="normalize-space(html:a/html:dfn)"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="normalize-space(.)"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="def">
      <xsl:for-each select="following-sibling::html:dd[generate-id(preceding-sibling::html:dt[1])=$tmp]">
        <xsl:if test="not(starts-with(normalize-space(translate(.,$str:uc,$str:lc)),'see also'))">
          <div>
<!-- this makes rdflib 1.2.[3|4] chokes as of 2004-02-17 -->
<!--            <xsl:attribute name="xml:lang">
              <xsl:choose>
                <xsl:when test="ancestor-or-self::*[@xml:lang]">
                  <xsl:value-of select="ancestor-or-self::*[@xml:lang][1]/@xml:lang"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:text>en</xsl:text>
                </xsl:otherwise>
              </xsl:choose>  
            </xsl:attribute>
    -->        
            <xsl:apply-templates select="*|@*|text()" mode="copy"/>
          </div>
        </xsl:if>
      </xsl:for-each>
    </xsl:variable>
    <xsl:call-template name="addTermEntry">
      <xsl:with-param name="term" select="$term"/>
      <xsl:with-param name="definition" select="$def"/>
      <xsl:with-param name="about" select="$uri"/>
      <xsl:with-param name="isDefinedBy" select="$this_version"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="html:dt" mode="not_glossary">
    <xsl:param name="uri"/>
    <xsl:variable name="previous_section">
      <xsl:call-template name="trim-and-lower">
        <xsl:with-param name="str" select="parent::html:dl/preceding-sibling::html:h2[1]"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="validPreviousSection">
      <xsl:call-template name="isPreviousSectionValid">
        <xsl:with-param name="section" select="$previous_section"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:if test="normalize-space($validPreviousSection)">
      <xsl:variable name="tmp" select="generate-id(.)"/>
      <xsl:variable name="validTerm">
        <xsl:call-template name="isTermValid">
          <xsl:with-param name="str" select="normalize-space(.)"/>
        </xsl:call-template>
      </xsl:variable>
      <xsl:if test="normalize-space($validTerm)">
        <xsl:variable name="def">
          <xsl:for-each select="following-sibling::html:dd[generate-id(preceding-sibling::html:dt[1])=$tmp]">
          <div xmlns="http://www.w3.org/1999/xhtml">
<!-- this makes rdflib 1.2.[3|4] chokes as of 2004-02-17 -->
<!--
            <xsl:attribute name="xml:lang">
              <xsl:choose>
                <xsl:when test="ancestor-or-self::*[@xml:lang]">
                  <xsl:value-of select="ancestor-or-self::*[@xml:lang][1]/@xml:lang"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:text>en</xsl:text>
                </xsl:otherwise>
              </xsl:choose>  
            </xsl:attribute>
-->
            <xsl:apply-templates select="*|@*|text()" mode="copy"/>
          </div>
          </xsl:for-each>
        </xsl:variable>
        <xsl:call-template name="addTermEntry">
          <xsl:with-param  name="term" select="normalize-space(.)"/>
          <xsl:with-param name="definition" select="$def"/>
          <xsl:with-param name="about" select="$uri"/>
          <xsl:with-param name="isDefinedBy" select="$this_version"/>
        </xsl:call-template>
      </xsl:if>
    </xsl:if>
  </xsl:template>

  <xsl:template name="trd:getStatusSubtitle">
    <xsl:variable name="tmp">
      <xsl:choose>
        <xsl:when test="normalize-space($trd:divHead//html:h2[starts-with(normalize-space(.),'W3C')])">
          <xsl:value-of select="normalize-space($trd:divHead//html:h2[starts-with(normalize-space(.),'W3C')])"/>
        </xsl:when>
        <xsl:when test="normalize-space($trd:divHead//html:h2/html:a[@name and starts-with(normalize-space(.),'W3C')])">
          <xsl:value-of select="normalize-space($trd:divHead//html:h2/html:a[@name and starts-with(normalize-space(.),'W3C')])"/>
        </xsl:when>
      </xsl:choose>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="contains($tmp,',')">
        <xsl:value-of select="substring-before($tmp,',')"/> 
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$tmp"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

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