<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:gdm="http://gdmxml.fugal.net/beta">
  <xsl:output method="html"/>
  <xsl:key name="index" match="gdm:*" use="@id"/>

  <xsl:template match="/">
    <html>
      <body>
	<h1>gdmxml XSLT stylesheet example</h1>
	  <a href="gdmxml.rnc">gdmxml.rnc</a>,
	  <a href="gdmxml.rng">gdmxml.rng</a>,
	  <a href="fugl.xml">fugl.xml</a>,
	  <a href="gdmxml.xsl">gdmxml.xsl</a>
	  <xsl:apply-templates select="gdm:gdmxml"/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="gdm:gdmxml">
    <h2>Assertions</h2>
    <xsl:apply-templates select="gdm:assertion"/>
    <h2>Sources</h2>
    <xsl:apply-templates select="gdm:source"/>
  </xsl:template>

  <xsl:template match="gdm:assertion">
    <p>
      <h3><a name="#{@id}">[<xsl:value-of select="@id"/>]</a></h3>
      <a href="#{@researcher-ref}"><xsl:value-of select="key('index', @researcher-ref)/gdm:name"/></a>
      asserts 
      <ul><li><xsl:apply-templates select="gdm:assertion-subject[1]"/></li>
	<li><xsl:apply-templates select="gdm:assertion-subject[2]"/></li>
	<li><xsl:apply-templates select="gdm:value"/></li>
      </ul>
      <b>Rationale</b>: 
      <xsl:value-of select="gdm:rationale"/>
      <br/>
      <b>Surety</b>: <a href="#{@surety-scheme-part-ref}"><xsl:value-of select="key('index', @surety-scheme-part-ref)/gdm:name"/></a>
      <br/>
      <b>Based on</b>: 
      <xsl:choose>
	<xsl:when test="@source-ref">
	  <a href="#{@source-ref}"><xsl:value-of select="@source-ref"/></a>
	</xsl:when>
	<xsl:when test="gdm:assertion">
	  <xsl:for-each select="gdm:assertion">
	    <xsl:if test="position() > 1">, </xsl:if>
	    <xsl:apply-templates select="." mode="link"/>
	  </xsl:for-each>
	</xsl:when>
	<xsl:otherwise>??</xsl:otherwise>
      </xsl:choose>
    </p>
    </xsl:template>

    <xsl:template match="gdm:assertion-subject">
      <xsl:value-of select="@type"/>
      /
      <a href="#{@ref}">
	<xsl:value-of select="key('index',@ref)//gdm:name"/>
      </a>
    </xsl:template>

    <xsl:template match="gdm:value">
      <xsl:choose>
	<xsl:when test="gdm:role">
	  <xsl:choose>
	    <xsl:when test="gdm:role/@type = 'group-type'">
	      <a href="#{gdm:role/@ref}"><xsl:value-of select="key('index',gdm:role/@ref)/gdm:name"/></a>
	    </xsl:when>
	    <xsl:when test="gdm:role/@type = 'event-type'">
	      <a href="#{gdm:role/@ref}"><xsl:value-of select="key('index',gdm:role/@ref)/gdm:name"/></a>
	    </xsl:when>
	    <xsl:otherwise>
	      ??
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:value-of select="."/>
	</xsl:otherwise>
      </xsl:choose>
    </xsl:template>

    <xsl:template match="gdm:assertion" mode="link">
      <a href="#{@ref}"><xsl:value-of select="@ref"/></a>
    </xsl:template>

    <xsl:template match="gdm:source">
      <h3><a name="{@id}">[<xsl:value-of select="@id"/>]</a></h3>
      <xsl:if test="@higher-source-ref">
	<b>Higher source</b>: <a href="#{@higher-source-ref}"><xsl:value-of select="@higher-source-ref"/></a><br/>
      </xsl:if>
      <xsl:if test="@subject-place-ref">
	<b>Subject place</b>: <a href="#{@subject-place-ref}"><xsl:value-of select="@subject-place-ref"/></a><br/>
      </xsl:if>
      <xsl:if test="@subject-place-ref">
	<b>Jurisdiction place</b>: <a href="#{@jurisdiction-place-ref}"><xsl:value-of select="@jurisdiction-place-ref"/></a><br/>
      </xsl:if>
      <xsl:if test="@researcher-ref">
	<b>Researcher</b>: <a href="#{@researcher-ref}"><xsl:value-of select="@researcher-ref"/></a><br/>
      </xsl:if>
      <xsl:if test="gdm:citation-part">
	<b>Citation</b>: <xsl:value-of select="gdm:citation-part"/><br/>
      </xsl:if>
      <xsl:if test="gdm:subject-date">
	<b>Subject date</b>: <xsl:value-of select="gdm:subject-date"/><br/>
      </xsl:if>
      <xsl:if test="gdm:comments">
	<b>Comments</b>: <xsl:value-of select="gdm:comments"/>
      </xsl:if>
    </xsl:template>

</xsl:stylesheet>
