<xsl:transform
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:h="http://www.w3.org/1999/xhtml"
>

<xsl:output method="xml" indent="yes" />

<xsl:param name="MultipleRefLists"
	   select='count(//h:dl[@class="bib"]) &gt; 1' />

<xsl:template match='h:a[@href]'>
  <xsl:choose>
    <xsl:when test='starts-with(@href, "#")'>
      <!-- intra-document links are always OK -->
    </xsl:when>

    <xsl:when test='not(contains(@href, ":"))'>
      <!-- relative links are to other parts of the document -->
    </xsl:when>

    <xsl:when test='ancestor::h:div[@class="head"]
		    or ancestor::h:div/h:h2/h:a/@id="status"'>
      <!-- links in the head and status are controlled by pubrules -->
    </xsl:when>

    <xsl:when test='ancestor::h:div[@class="nav"]
		    or ancestor::h:div[@class="changes"]
		    or ancestor::h:div/h:h2/h:a/@id="status"'>
      <!-- navigational aids and
	   links from the changelog are OK -->
    </xsl:when>

    <xsl:when test='ancestor::*[@class="issue"]'>
      <!-- links from issues are OK -->
    </xsl:when>


    <xsl:when test='@class="inform"'>
      <xsl:call-template name="checkBibEntry">
	<xsl:with-param name="bib">informativeRefs</xsl:with-param>
      </xsl:call-template>
    </xsl:when>

    <xsl:when test='not($MultipleRefLists)'>
      <xsl:call-template name="checkBibEntry" />
    </xsl:when>

    <xsl:when test='@class="norm"'>
      <xsl:call-template name="checkBibEntry">
	<xsl:with-param name="bib">normativeRefs</xsl:with-param>
      </xsl:call-template>
    </xsl:when>

    <xsl:otherwise>
      <xsl:message>
	link is neither local, part, informative, nor normative:
	[<xsl:value-of select="normalize-space(.)" />]
	"<xsl:value-of select="@href" />"
      </xsl:message>
    </xsl:otherwise>
  </xsl:choose>

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


<xsl:template name="checkBibEntry">
  <xsl:param name="bib" />

  <xsl:variable name="addr">
    <xsl:choose>
      <xsl:when test='contains(@href, "#")'>
	<xsl:value-of select='substring-before(@href, "#")' />
      </xsl:when>
      <xsl:otherwise>
	<xsl:value-of select='@href' />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <!-- xsl:message>
    checking: <xsl:value-of select="$addr" />
  </xsl:message -->

  <xsl:variable name="entry">
    <xsl:for-each select='//h:dl[@class="bib"
			  and (not($MultipleRefLists)
			  or preceding-sibling::h:h3[1]/@id=$bib)]/h:dd'>
      <xsl:if test='.//h:a/@href = $addr'>
	<xsl:value-of select='preceding-sibling::h:dt/h:a/@id' />
      </xsl:if>
    </xsl:for-each>
  </xsl:variable>

  <!-- xsl:if test="normalize-space($entry)">
    <xsl:message>
      <xsl:value-of select="$addr" /> is ref #<xsl:value-of select="$entry" /> in <xsl:value-of select="$bib" /> bibliography.
    </xsl:message>
  </xsl:if -->

  <xsl:if test="not(normalize-space($entry))">
    <xsl:message>
      <xsl:value-of select="$addr" /> is linked
      but not cited in <xsl:value-of select="$bib" /> bibliography.
    </xsl:message>
  </xsl:if>

</xsl:template>


<xsl:template match='h:dl[@class="bib"]'>
  <xsl:variable name="bibkeys">
    <xsl:for-each select='h:dd'>
      <xsl:variable name="ref"
		    select="h:a/@href" />
      <xsl:if test='starts-with($ref, "http://www.w3.org/TR/")'>
	<xsl:value-of select='preceding-sibling::h:dt[1]/h:a/@id' />
	<xsl:text>+</xsl:text>
	<xsl:value-of select='$ref' />
	<xsl:text>%0D%0A</xsl:text>
      </xsl:if>
    </xsl:for-each>
  </xsl:variable>
  
  <xsl:message>
    bibkeys: <xsl:value-of select="$bibkeys" />
  </xsl:message>
  
  <xsl:variable name="addr"
		select='concat("http://www.w3.org/2000/06/webdata/xslt?",
    "xslfile=http://www.w3.org/2002/01/tr-automation/tr-biblio.xsl&amp;",
    "xmlfile=http://www.w3.org/2002/01/tr-automation/tr-logs.rdf&amp;",
    "labels=yes&amp;",
    "uris=", $bibkeys)' />

  <xsl:variable name="bibgen" select='document($addr)' />

  <dl>
    <xsl:for-each select="h:dt">
      <xsl:variable name="k" select="h:a/@id" />
      <xsl:variable name="dd" select='following-sibling::h:dd[1]' />
      <xsl:variable name="ref" select="$dd//h:a/@href" />

  <xsl:message>
    k: <xsl:value-of select="$k" />
    ref: <xsl:value-of select="$ref" />
  </xsl:message>

      <xsl:choose>
	<!-- is it a /TR/ reference? -->
	<xsl:when test='starts-with($ref, "http://www.w3.org/TR/")'>
	  <!-- yes; copy dd from generated bib -->
	  <xsl:copy-of select="." />
	  <xsl:copy-of select="$bibgen/h:dl/h:dt[h:a/@id=$k]/following-sibling::h:dd[1]" />
	</xsl:when>

	<xsl:otherwise> <!-- no; keep as is -->
	  <xsl:copy-of select="." />
	  <xsl:copy-of select="$dd" />
	</xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </dl>
</xsl:template>

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

</xsl:transform>
