<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet href="http://www.w3.org/2002/02/style-xsl.css" type="text/css"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 xmlns:org="http://www.w3.org/2001/04/roadmap/org#"
 xmlns:doc="http://www.w3.org/2000/10/swap/pim/doc#"
 xmlns:dc="http://purl.org/dc/elements/1.1/"
 xmlns:rec="http://www.w3.org/2001/02pd/rec54#"
 xmlns:h="http://www.w3.org/1999/xhtml"
 xmlns:str="http://www.w3.org/2001/10/str-util.xsl"
 xmlns:uri="http://www.w3.org/2000/07/uri43/uri.xsl?template="
 version="1.0">

 <xsl:import href="http://www.w3.org/2001/10/str-util.xsl"/>
 <xsl:import href="http://www.w3.org/2000/07/uri43/uri.xsl"/>
 <xsl:output method="xml" encoding="utf-8" indent="yes"/>

 <xsl:param name="recuri"/>
 <xsl:variable name="controldb" select="document('rec-control.rdf')"/>
 <xsl:variable name="trdb" select="document('../../../2002/01/tr-automation/tr.rdf')"/>

 <xsl:template match="/">
  <rdf:RDF xmlns:doc="http://www.w3.org/2000/10/swap/pim/doc#"
           xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
   <xsl:for-each select="$controldb/rdf:RDF/rec:REC">
    <xsl:choose>
     <xsl:when test="not(doc:hasReferences)">
      <rdf:Description rdf:about="{doc:versionOf/@rdf:resource}">
       <xsl:call-template name="doc">
        <xsl:with-param name="uri" select="doc:versionOf/@rdf:resource"/>
        <xsl:with-param name="version" select="doc:versionOf/@rdf:resource"/>
       </xsl:call-template>
      </rdf:Description>
     </xsl:when>
     <xsl:otherwise>
      <rdf:Description rdf:about="{doc:versionOf/@rdf:resource}">
       <xsl:call-template name="doc">
        <xsl:with-param name="uri" select="doc:hasReferences/@rdf:resource"/>
        <xsl:with-param name="version" select="doc:versionOf/@rdf:resource"/>
       </xsl:call-template>
      </rdf:Description>
     </xsl:otherwise>
    </xsl:choose>
   </xsl:for-each>
  </rdf:RDF>
 </xsl:template>

 <xsl:template name="doc">
  <xsl:param name="uri"/>
  <xsl:param name="version"/>
  <xsl:variable name="trdoc" select="document(concat('http://cgi.w3.org/cgi-bin/tidy-if?docAddr=',$uri))"/>
  <xsl:choose>
   <xsl:when test="$trdoc/h:html/h:body//h:*[starts-with(local-name(),'h')][contains(translate(.,$str:uc,$str:lc),'normative') and not(contains(translate(.,$str:uc,$str:lc),'references')) and not(contains(translate(.,$str:uc,$str:lc),'non-normative'))] and not($trdoc/h:html/h:body//h:*[starts-with(local-name(),'h')][contains(translate(.,$str:uc,$str:lc),'normative') and contains(translate(.,$str:uc,$str:lc),'references') and not(contains(translate(.,$str:uc,$str:lc),'non-normative'))])">
    <xsl:for-each select="$trdoc/h:html/h:body//h:*[starts-with(local-name(),'h')][contains(translate(.,$str:uc,$str:lc),'normative') and not(contains(translate(.,$str:uc,$str:lc),'references')) and not(contains(translate(.,$str:uc,$str:lc),'non-normative'))]/following-sibling::h:*[1]//h:a[@href and (starts-with(@href,'http://www.w3.org/TR/') or starts-with(@href,'/TR/'))]">
     <rec:cites>
      <xsl:attribute name="rdf:resource">
       <xsl:call-template name="uri:expand">
        <xsl:with-param name="there" select="@href"/>
        <xsl:with-param name="base" select="$uri"/>
       </xsl:call-template>
      </xsl:attribute>
     </rec:cites>
    </xsl:for-each>
   </xsl:when>
   <xsl:when test="$trdoc/h:html/h:body//h:*[starts-with(local-name(),'h')][not(contains(translate(.,$str:uc,$str:lc),'non-normative')) and contains(translate(.,$str:uc,$str:lc),'references')] and not($trdoc/h:html/h:body//h:*[starts-with(local-name(),'h')][contains(translate(.,$str:uc,$str:lc),'normative') and contains(translate(.,$str:uc,$str:lc),'references') and not(contains(translate(.,$str:uc,$str:lc),'non-normative'))])">
    <xsl:for-each select="$trdoc/h:html/h:body//h:*[starts-with(local-name(),'h')][not(contains(translate(.,$str:uc,$str:lc),'non-normative')) and contains(translate(.,$str:uc,$str:lc),'references')]/following-sibling::h:dl[1]//h:a[@href and (starts-with(@href,'http://www.w3.org/TR/') or starts-with(@href,'/TR/'))]">
     <rec:cites>
      <xsl:attribute name="rdf:resource">
       <xsl:call-template name="uri:expand">
        <xsl:with-param name="there" select="@href"/>
        <xsl:with-param name="base" select="$uri"/>
       </xsl:call-template>
      </xsl:attribute>
     </rec:cites>
    </xsl:for-each>
   </xsl:when>
   <xsl:when test="$trdoc/h:html/h:body//h:*[starts-with(local-name(),'h')][contains(translate(normalize-space(),$str:uc,$str:lc),'normative') and contains(translate(normalize-space(),$str:uc,$str:lc),'references')]/following-sibling::h:*[1][not(local-name()='dl')]">
    <xsl:for-each select="$trdoc/h:html/h:body//h:*[starts-with(local-name(),'h')][contains(translate(normalize-space(),$str:uc,$str:lc),'normative') and contains(translate(normalize-space(),$str:uc,$str:lc),'references')]/following-sibling::h:dl[1]//h:a[@href and (starts-with(@href,'http://www.w3.org/TR/') or starts-with(@href,'/TR/'))]">
     <rec:cites>
      <xsl:attribute name="rdf:resource">
       <xsl:call-template name="uri:expand">
        <xsl:with-param name="there" select="@href"/>
        <xsl:with-param name="base" select="$uri"/>
       </xsl:call-template>
      </xsl:attribute>
     </rec:cites>
    </xsl:for-each>
   </xsl:when>
   <xsl:when test="$trdoc/h:html/h:body//h:*[starts-with(local-name(),'h')][contains(translate(.,$str:uc,$str:lc),'normative') and contains(translate(.,$str:uc,$str:lc),'references')]">
    <xsl:for-each select="$trdoc/h:html/h:body//h:*[starts-with(local-name(),'h')][contains(translate(.,$str:uc,$str:lc),'normative') and contains(translate(.,$str:uc,$str:lc),'references')]/following-sibling::h:*[1]//h:a[@href and (starts-with(@href,'http://www.w3.org/TR/') or starts-with(@href,'/TR/'))]">
     <rec:cites>
      <xsl:attribute name="rdf:resource">
       <xsl:call-template name="uri:expand">
        <xsl:with-param name="there" select="@href"/>
        <xsl:with-param name="base" select="$uri"/>
       </xsl:call-template>
      </xsl:attribute>
     </rec:cites>
    </xsl:for-each>
   </xsl:when>
  </xsl:choose>
  <dc:title>
   <xsl:value-of select="normalize-space($trdb/rdf:RDF/rec:REC[doc:versionOf/@rdf:resource=$version]/dc:title)"/>
  </dc:title>
 </xsl:template>
</xsl:stylesheet>
