<?xml-stylesheet href="http://www.w3.org/StyleSheets/base.css" type="text/css"?>
<?xml-stylesheet href="http://www.w3.org/2002/02/style-xsl.css" type="text/css"?>
<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:anno="http://www.w3.org/2000/10/annotation-ns#" xmlns:dc="http://purl.org/dc/elements/1.0/" xmlns="http://www.w3.org/1999/xhtml" xmlns:uri="http://www.w3.org/2000/07/uri43/uri.xsl?template=" exclude-result-prefixes="rdf anno dc uri">
  <xsl:import href="http://www.w3.org/2000/07/uri43/uri.xsl" />

  <xsl:output method="html" indent="yes" doctype-public="-//W3C//DTD HTML 4.01//EN"/>
  
  <xsl:param name="xmlfile"/>
  <xsl:variable name="doc_uri">
    <xsl:choose>
      <xsl:when test="starts-with($xmlfile,'http://cgi.w3.org/cgi-bin/tidy-if?docAddr=')">
        <xsl:value-of select="substring-after($xmlfile,'http://cgi.w3.org/cgi-bin/tidy-if?docAddr=')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$xmlfile"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:param name="annoteaServer" select="'http://annotest.w3.org/annotations'"/>
  <xsl:param name="tooltip" select="0"/>
  <xsl:param name="inclusion" select="0"/>
  <xsl:variable name="annotations" select="document(concat($annoteaServer,'?w3c_forceText=*&amp;w3c_annotates=',$doc_uri))/rdf:RDF"/>
  <xsl:variable name="quote" select='"&apos;"'/>
  <xsl:variable name="apos" select="'&quot;'"/>
  <xsl:variable name="quotes" select="concat($quote,$apos)"/>


<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      <title>Annotea client in XSLT</title>
      <link rel="stylesheet" href="http://www.w3.org/StyleSheets/base"/>
    </head>
    <body>
      <div class='head'><a href="/"><img src='/Icons/w3c_home' alt='W3C'/></a></div>
      <h1>Annotea client in XSLT</h1>

      <p>See <a href="annotea-show">the HTML interface</a> and the <a href="http://lists.w3.org/Archives/Public/www-annotation/2003JulDec/0068.html">announce to www-annotation</a>.</p>

<address>
<a href="/People/Dom/">Dominique Haza&#xeb;l-Massieux</a><br />
<small>$Id: annotea-proxy.xsl,v 1.44 2007/10/22 09:12:49 dom Exp $</small>
</address>
</body>
</html>

  <!-- default: Identity Transformation -->
  <xsl:template match="*|@*|comment()|text()">
    <xsl:copy>
      <xsl:apply-templates select="*|@*|comment()|text()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="html:head">
    <xsl:copy>
      <xsl:apply-templates select="*|@*|comment()|text()"/>
      <xsl:if test="$inclusion">
        <style type='text/css'>
          blockquote.annotation:before { content:url("http://www.w3.org/Amaya/images/annot.png");}
          blockquote.annotation { background-color:#ffe; color:black;}
        </style>
      </xsl:if>
    </xsl:copy>
  </xsl:template>


  <xsl:template match="html:body">
    <xsl:copy>
      <xsl:if test="boolean($annotations)">
        <div style="position: absolute; top:0; right:0; margin-left:auto; width:33%; border:thin dotted red;"><p style="background:white;">This document has annotations on an <a href="{$annoteaServer}">Annotea server</a>: (<a href="javascript:void(document.getElementById('annotea').style.display=(document.getElementById('annotea').style.display=='none' ? 'block' : 'none'))">hide/show</a>)</p>
        <ul id="annotea"  style="background:white;">
          <xsl:for-each select="$annotations/*[@rdf:about]">
            <li>
              <xsl:choose>
                <xsl:when test="anno:body/@rdf:resource">
                  <a href="{anno:body/@rdf:resource}">
                    <xsl:if test="$tooltip">
                      <xsl:attribute name="title">
                        <xsl:value-of select="document(anno:body/@rdf:resource)/html:html/html:body"/>
                      </xsl:attribute>
                    </xsl:if>
                    <xsl:choose>
                      <xsl:when test="@dc:title or dc:title">
                        <xsl:value-of select="concat(@dc:title,dc:title)"/>
                      </xsl:when>
                      <xsl:otherwise>
                        <xsl:value-of select="local-name()"/>
                      </xsl:otherwise>
                    </xsl:choose>
                  </a>
                </xsl:when>
                <xsl:when test="anno:body/@rdf:parseType='Litteral'">
                  <xsl:variable name="target">
                    <xsl:variable name="id" select="translate(substring-before(substring-after(@anno:context,'#xpointer(id('),')'),$quotes,'')" />
                    <xsl:choose>
                      <xsl:when test="$id">
                        <xsl:value-of select="$id"/>
                      </xsl:when>
                      <xsl:when test="$inclusion">
                        <xsl:value-of select="'__unbound'"/>
                      </xsl:when>
                    </xsl:choose>
                  </xsl:variable>
                  <a>
                    <xsl:if test="$target">
                      <xsl:attribute name="href">
                        <xsl:value-of select="concat('#',$target)"/>
                      </xsl:attribute>
                    </xsl:if>
                    <xsl:if test="$tooltip">
                      <xsl:attribute name="title">
                        <xsl:value-of select="anno:body"/>
                      </xsl:attribute>
                    </xsl:if>
                    <xsl:choose>
                      <xsl:when test="@dc:title or dc:title">
                        <xsl:value-of select="concat(@dc:title,dc:title)"/>
                      </xsl:when>
                      <xsl:otherwise>
                        <xsl:value-of select="local-name()"/>
                      </xsl:otherwise>
                    </xsl:choose>

                  </a>
                </xsl:when>
              </xsl:choose>
              <xsl:text> by </xsl:text>
              <xsl:value-of select="concat(@dc:creator,dc:creator)"/>
            </li>
          </xsl:for-each>
        </ul>
      </div>
      </xsl:if>
      <xsl:apply-templates select="*|@*|comment()|text()"/>
    </xsl:copy>
    <xsl:if test="$inclusion">
      <div style='border:thin dotted red;'><p><a id='__unbound' name='__unbound'>Unbound annotations</a>:</p>
      <xsl:for-each select="$annotations/*[translate(substring-before(substring-after(@anno:context,'#xpointer(id('),')'),$quotes,'')='']">
        <blockquote class="annotation">
          
          <xsl:apply-templates select="anno:body/html:*"/>
          <address><xsl:value-of select="concat(@dc:title,dc:title)" /> from <xsl:value-of select="concat(@dc:creator,dc:creator)"/></address>
        </blockquote>

      </xsl:for-each>
      </div>
    </xsl:if>
  </xsl:template>
<!-- This does the right thing, but doesn't work with our XT servlet :(
  <xsl:template match="@href|@src">
    <xsl:attribute name="{local-name()}">
      <xsl:call-template name="uri:expand">
        <xsl:with-param name="base" select="$doc_uri"/>
        <xsl:with-param name="there" select="."/>
      </xsl:call-template>
    </xsl:attribute>
  </xsl:template>
-->
  <xsl:template match="html:*[@id]">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:if test="not($inclusion)">
      <xsl:for-each select="$annotations/*[translate(substring-before(substring-after(@anno:context,'#xpointer(id('),')'),$quotes,'')=current()/@id]">
        <a >
          <xsl:choose>
            <xsl:when test="anno:body/@rdf:resource">
              <xsl:attribute name="href">
                <xsl:value-of select="anno:body/@rdf:resource"/>
              </xsl:attribute>
              <xsl:attribute name="title">
                <xsl:value-of select="concat(dc:title,@dc:title)"/>
                <xsl:if test="$tooltip">
                  <xsl:text>: </xsl:text>
                  <xsl:value-of select="document(anno:body/@rdf:resource)/html:html/html:body"/>
                </xsl:if>
              </xsl:attribute>
            </xsl:when>
            <xsl:when test="anno:body/@rdf:parseType='Litteral'">
              <xsl:attribute name="title">
                <xsl:value-of select="concat(dc:title,@dc:title)"/>
                <xsl:if test="$tooltip">
                  <xsl:text>: </xsl:text>
                  <xsl:value-of select="anno:body"/>
                </xsl:if>
              </xsl:attribute>
            </xsl:when>
          </xsl:choose>
          <img src="http://www.w3.org/Amaya/images/annot.png" alt="Annotation"/>
        </a>
      </xsl:for-each>
      </xsl:if>
      <xsl:apply-templates select="*|comment()|text()"/>
    </xsl:copy>
    <xsl:if test="$inclusion">
      <xsl:for-each select="$annotations/*[translate(substring-before(substring-after(@anno:context,'#xpointer(id('),')'),$quotes,'')=current()/@id]">
        <blockquote class="annotation">
          
          <xsl:apply-templates select="anno:body/html:*"/>
          <address><xsl:value-of select="concat(@dc:title,dc:title)" /> from <xsl:value-of select="concat(@dc:creator,dc:creator)"/></address>
        </blockquote>
      </xsl:for-each>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>