<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <!--
    genid.xsl
    version 1.0
    by Mike J. Brown <mailto:mike%40skew.org>
    license: none; use freely
  -->

  <!-- URI of source doc (absolute, with scheme) -->
  <xsl:param name="src-uri" select="'file:///genid.xsl'"/>

  <!-- URI of stylesheet doc (absolute, with scheme) -->
  <xsl:param name="sty-uri" select="'file:///genid.xsl'"/>

  <xsl:variable name="src-scheme" select="substring-before($src-uri,':')"/>
  <xsl:variable name="sty-scheme" select="substring-before($sty-uri,':')"/>
  <xsl:variable name="src-file">
    <xsl:call-template name="get-tail">
      <xsl:with-param name="path" select="$src-uri"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="sty-file">
    <xsl:call-template name="get-tail">
      <xsl:with-param name="path" select="$sty-uri"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="src-authority" select="substring-before(substring-after($src-uri,'://'),'/')"/>
  <xsl:variable name="sty-authority" select="substring-before(substring-after($sty-uri,'://'),'/')"/>
  <xsl:variable name="src-path" select="substring-after($src-uri, concat($src-scheme,'://',$src-authority))"/>
  <xsl:variable name="sty-path" select="substring-after($sty-uri, concat($sty-scheme,'://',$sty-authority))"/>
  <xsl:variable name="src-base-path" select="substring($src-path, string-length($src-path) - string-length($src-file))"/>
  <xsl:variable name="sty-base-path" select="substring($sty-path, string-length($sty-path) - string-length($sty-file))"/>
  <xsl:variable name="src-relative-uri-ref" select="$src-file"/>
  <xsl:variable name="src-absolute-uri-ref" select="$src-uri"/>
  <xsl:variable name="src-localhost-uri-ref" select="concat('file://localhost',$src-path)"/>
  <xsl:variable name="sty-relative-uri-ref" select="$sty-file"/>
  <xsl:variable name="sty-absolute-uri-ref" select="$sty-uri"/>
  <xsl:variable name="sty-localhost-uri-ref" select="concat('file://localhost',$sty-path)"/>
 
  <xsl:variable name="src-is-sty" select="$src-uri = $sty-uri"/>
  <xsl:variable name="ok-to-test-src-localhost" select="$src-scheme='file' and $src-authority=''"/>
  <xsl:variable name="ok-to-test-sty-localhost" select="$sty-scheme='file' and $sty-authority=''"/>

  <xsl:output method="html" encoding="us-ascii" indent="yes"/>

  <xsl:template match="/">

    <xsl:variable name="src-locpath-root" select="/"/>
    <xsl:variable name="src-thisdoc-root" select="document('',$src-locpath-root)"/>
    <xsl:variable name="src-relative-root" select="document($src-relative-uri-ref,$src-locpath-root)"/>
    <xsl:variable name="src-absolute-root" select="document($src-absolute-uri-ref)"/>
    <xsl:variable name="src-localhost-root" select="document($src-localhost-uri-ref)"/>

    <xsl:variable name="sty-thisdoc-root" select="document('')"/>
    <xsl:variable name="sty-relative-root" select="document($sty-relative-uri-ref)"/>
    <xsl:variable name="sty-absolute-root" select="document($sty-absolute-uri-ref)"/>
    <xsl:variable name="sty-localhost-root" select="document($sty-localhost-uri-ref)"/>

    <xsl:variable name="src-locpath-root-id" select="generate-id($src-locpath-root)"/>
    <xsl:variable name="src-thisdoc-root-id" select="generate-id($src-thisdoc-root)"/>
    <xsl:variable name="src-relative-root-id" select="generate-id($src-relative-root)"/>
    <xsl:variable name="src-absolute-root-id" select="generate-id($src-absolute-root)"/>
    <xsl:variable name="src-localhost-root-id" select="generate-id($src-localhost-root)"/>

    <xsl:variable name="sty-thisdoc-root-id" select="generate-id($sty-thisdoc-root)"/>
    <xsl:variable name="sty-relative-root-id" select="generate-id($sty-relative-root)"/>
    <xsl:variable name="sty-absolute-root-id" select="generate-id($sty-absolute-root)"/>
    <xsl:variable name="sty-localhost-root-id" select="generate-id($sty-localhost-root)"/>

    <xsl:variable name="vendor" select="system-property('xsl:vendor')"/>

    <html>
      <head>
        <title>results</title>
      </head>
      <body>
        <h1>results for vendor <xsl:value-of select="$vendor"/></h1>
        <table border="1">
          <tr><th colspan="2">source doc IDs:<br/>these should all be the same</th></tr>
          <tr>
            <td><b><xsl:value-of select="$src-locpath-root-id"/></b></td>
            <td>root node of source via XPath location path /</td>
          </tr>
          <tr>
            <td><b><xsl:value-of select="$src-thisdoc-root-id"/></b></td>
            <td>root node of source via same-document URI reference ('') relative to XPath location path /</td>
          </tr>
          <tr>
            <td><b><xsl:value-of select="$src-relative-root-id"/></b></td>
            <td>root node of source via relative URI ref '<xsl:value-of select="$src-relative-uri-ref"/>'</td>
          </tr>
          <tr>
            <td><b><xsl:value-of select="$src-absolute-root-id"/></b></td>
            <td>root node of source via absolute URI ref '<xsl:value-of select="$src-absolute-uri-ref"/>'</td>
          </tr>
          <xsl:if test="$ok-to-test-src-localhost">
            <tr>
              <td><b><xsl:value-of select="$src-localhost-root-id"/></b></td>
              <td>root node of source via absolute URI ref '<xsl:value-of select="$src-localhost-uri-ref"/>'</td>
            </tr>
          </xsl:if>
          <tr><th colspan="2">stylesheet doc IDs:<br/>these should all be the same,<br/>
              <xsl:choose>
                <xsl:when test="$src-is-sty">and identical to the ID of the source doc</xsl:when>
                <xsl:otherwise>but different to the ID of the source doc</xsl:otherwise>
              </xsl:choose>
            </th>
          </tr>
          <tr>
            <td><b><xsl:value-of select="$sty-thisdoc-root-id"/></b></td>
            <td>root node of stylesheet via same-document URI reference ('')</td>
          </tr>
          <tr>
            <td><b><xsl:value-of select="$sty-relative-root-id"/></b></td>
            <td>root node of stylesheet via relative URI ref '<xsl:value-of select="$sty-relative-uri-ref"/>'</td>
          </tr>
          <tr>
            <td><b><xsl:value-of select="$sty-absolute-root-id"/></b></td>
            <td>root node of stylesheet via absolute URI ref '<xsl:value-of select="$sty-absolute-uri-ref"/>'</td>
          </tr>
          <xsl:if test="$ok-to-test-sty-localhost">
            <tr>
              <td><b><xsl:value-of select="$sty-localhost-root-id"/></b></td>
              <td>root node of stylesheet via absolute URI ref '<xsl:value-of select="$sty-localhost-uri-ref"/>'</td>
            </tr>
          </xsl:if>
          <tr>
            <th colspan="2">factors affecting results above:<br/>these should all be true</th>
          </tr>
          <tr>
            <td><b><xsl:value-of select="not(generate-id(/..))"/></b></td>
            <td>generate-id() on empty node-set returns empty string</td>
          </tr>
          <tr>
            <td><b><xsl:value-of select="$src-locpath-root-id=generate-id($src-locpath-root) and $sty-thisdoc-root-id=generate-id($sty-thisdoc-root)"/></b></td>
            <td>generate-id() on same node returns same results</td>
          </tr>
          <tr>
            <td><b><xsl:value-of select="$src-locpath-root-id!=generate-id($src-locpath-root/*)"/></b></td>
            <td>generate-id() on different nodes returns different results</td>
          </tr>
          <tr>
            <td><b><xsl:value-of select="$src-absolute-root and $sty-absolute-root"/></b></td>
            <td>URIs given as src-uri and sty-uri are resolvable</td>
          </tr>
          <tr>
            <td><b><xsl:value-of select="$src-relative-root=document($src-relative-uri-ref,$src-locpath-root) and $src-absolute-root=document($src-absolute-uri-ref)"/></b></td>
            <td>given same URI reference, document() returns same node</td>
          </tr>
          <tr>
            <td><b><xsl:value-of select="$src-relative-root-id=$src-absolute-root-id"/></b></td>
            <td>given equivalent relative and absolute URI references, document() returns same node</td>
          </tr>
          <tr>
            <td><b><xsl:value-of select="$src-locpath-root-id=$src-absolute-root-id"/></b></td>
            <td>URI given as src-uri parameter is recognized as URI of source doc fed to processor</td>
          </tr>
          <tr>
            <td><b><xsl:value-of select="$sty-thisdoc-root-id=$sty-absolute-root-id"/></b></td>
            <td>URI given as sty-uri parameter is recognized as URI of stylesheet fed to processor</td>
          </tr>
          <xsl:choose>
            <xsl:when test="$ok-to-test-src-localhost">
              <tr>
                <td><b><xsl:value-of select="$src-absolute-root-id=$src-localhost-root-id"/></b></td>
                <td>URI resolver treats file:/// and file://localhost/ same as per RFC 1738</td>
              </tr>
            </xsl:when>
            <xsl:when test="$ok-to-test-sty-localhost">
              <tr>
                <td><b><xsl:value-of select="$sty-absolute-root-id=$sty-localhost-root-id"/></b></td>
                <td>URI resolver treats file:/// and file://localhost/ same as per RFC 1738</td>
              </tr>
            </xsl:when>
          </xsl:choose>
        </table>
      </body>
    </html>
      
  </xsl:template>

  <xsl:template name="get-tail">
    <xsl:param name="path"/>
    <xsl:choose>
      <xsl:when test="contains($path,'/')">
        <xsl:call-template name="get-tail">
          <xsl:with-param name="path" select="substring-after($path,'/')"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$path"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>
