<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" exclude-result-prefixes="i" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
 xmlns:i="http://docs.oasis-open.org/ws-sx/issues/Issues.xsd" 
 xmlns="http://www.w3.org/1999/xhtml"
 xmlns:h="http://www.w3.org/1999/xhtml"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://docs.oasis-open.org/ws-sx/issues/Issues.xsd Issues.xsd"
>
  <xsl:strip-space elements="*"/>
  <xsl:output 
	 method="xml" 
	 encoding="utf-8" 
	 indent="yes" 
	 doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" 
	 doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" 
	 media-type="text/html" 
	 omit-xml-declaration="no"
	 />

  <xsl:template match="i:issues">
    <html>
      <head>
        <title>
          <xsl:value-of select="i:head/i:title"></xsl:value-of>
        </title>
        <style type="text/css">
          body {color: black; background: white; font-family: sans-serif; }
          <xsl:for-each select="i:head/i:states/i:state">
            <xsl:if test="@color">
              .<xsl:value-of select="."/> { background-color: <xsl:value-of select="@color"/>; }
            </xsl:if>
          </xsl:for-each>
          dt { color: #666; font-weight: bold; }
          dd { white-space: normal; }
          .meta { background-color: #eee; }
          .date { font-weight: lighter; font-size: 0.8em; padding-left: 2em; }
          .marker { font-weight: bold; }
          .cvs:first-letter { color: transparent; }
          dl { padding: 0pt 24pt 12pt 0pt; }
          td, th { vertical-align: top; margin: 0px; padding: 3px; }
          th, th a { background-color: #666; color: #fff; text-align: left; }
          td.title { background-color: #fec; border-top: 1px solid black; page-break-after: avoid; }
          .summary td, .stats td { border-color: #eee; border-style: solid; border-width: 0 0 2px 0; }
          .stats td, .stats th { text-align: right; }
          @media print {
          a.ext:after {
          <!--content: " <xsl:text disable-output-escaping="yes">&lt;" attr(href) "&gt; </xsl:text>";-->
          font-size: 75%;
          font-weight: lighter;
          }
          .nav { display: none; }
          }

        </style>
      </head>
      <body>
        <a name="Top"/>
        <h1>
          <xsl:value-of select="i:head/i:title"></xsl:value-of>
        </h1>
        <p class="cvs">
          <xsl:value-of select="i:head/i:date"></xsl:value-of> - <xsl:value-of select="i:head/i:revision"></xsl:value-of>
        </p>

        <!-- TOC -->
        <h2>
          Contents
        </h2>
        <p>
          <dl>
            <dt>Active issue summary by protocol</dt>
            <dd>
              In this view an issue may show up more than once, each will be listed in the summary for every protocol
              it is logged against.
              <ul>
                <xsl:for-each select="i:head/i:protocols/i:protocol/@name">
                  <xsl:sort/>
                  <li>
                    <a href="#{.}-Active">
                      <xsl:value-of select="."/>
                    </a>
                  </li>
                </xsl:for-each>
              </ul>
            </dd>
            <dt>
              <a href="#AI">Action Items</a>
            </dt>
            <dd>
              Open:
              <ul>
                <xsl:apply-templates select="//i:action[@status='open']" mode="list"/>
              </ul>
            </dd>
            <dt>Issue summary list by status</dt>
            <dd>
              <ul>
                <xsl:for-each select="i:head/i:states/i:state">
                  <li>
                    <a href="#{.}">
                      <xsl:value-of select="."/>
                    </a>
                  </li>
                </xsl:for-each>
              </ul>
            </dd>
            <dt>
              <a href="#Stats">Statistics</a>
            </dt>
            <dd>
              Overall issue counts by related artifact type and broken out by each protocol.
              <ul>
                <xsl:for-each select="i:head/i:protocols/i:protocol/@name">
                  <xsl:sort/>
                  <li>
                    <a href="#{.}-stats">
                      <xsl:value-of select="."/>
                    </a>
                  </li>
                </xsl:for-each>
              </ul>
            </dd>
            <dt>
              <a href="#Detail">Detailed issue listing</a>
            </dt>
            <dd>
              Complete details in order of issue number
              <ul>
                <xsl:apply-templates select="i:issue" mode="list"/>
              </ul>
            </dd>
          </dl>
        </p>

        <!-- Issue summaries (active by spec (with dupes)) -->
        <xsl:for-each select="i:head/i:protocols/i:protocol/@name">
          <xsl:sort/>
          <xsl:call-template name="summary">
            <xsl:with-param name="type">Active</xsl:with-param>
            <xsl:with-param name="protocol">
              <xsl:value-of select="."/>
            </xsl:with-param>
          </xsl:call-template>
        </xsl:for-each>

        <!-- Issue summaries (all types) -->
        <xsl:for-each select="i:head/i:states/i:state">
          <xsl:call-template name="summary">
            <xsl:with-param name="type">
              <xsl:value-of select="."/>
            </xsl:with-param>
          </xsl:call-template>
        </xsl:for-each>

        <!-- Statistics -->
        <h2>
          <a name="Stats" accesskey="S" tabindex="0">
            Statistics
          </a>
        </h2>
        <xsl:apply-templates select="/i:issues" mode="stats"/>

        <!-- Issue detail listings (all statuses) -->
        <h2>
          <a name="Detail" accesskey="D" tabindex="2">
            Detailed Listing
          </a>
        </h2>
        <table cellspacing="0" width="95%">
          <xsl:apply-templates select="i:issue" mode="full"/>
        </table>

        <!-- Action items complete listings (all statuses) -->
        <h2>
          <a name="AI">
            Action Item Complete Listing
          </a>
        </h2>
        <table cellspacing="0" width="95%">
          <xsl:apply-templates select="i:action" mode="complete"/>
        </table>
      </body>
    </html>
  </xsl:template>

  <!-- Stats -->
  <xsl:template match="/i:issues" mode="stats">
    <xsl:for-each select="i:head/i:protocols/i:protocol/@name">
      <xsl:sort/>
      <xsl:variable name="protocol">
        <xsl:value-of select="."/>
      </xsl:variable>
      <h3>
        <a name="{$protocol}-stats">
          <xsl:value-of select="$protocol"/> issue stats
        </a>
      </h3>
      <table cellspacing="0" class="stats">
        <tbody>
          <tr>
            <th>
            </th>
            <xsl:for-each select="//i:head/i:targets/i:target">
              <th>
                <xsl:value-of select="."></xsl:value-of>
              </th>
            </xsl:for-each>
            <th>total</th>
          </tr>
          <xsl:for-each select="//i:head/i:states/i:state">
            <xsl:variable name="state">
              <xsl:value-of select="."/>
            </xsl:variable>
            <tr>
              <th>
                <xsl:value-of select="$state"/>
              </th>
              <xsl:for-each select="//i:head/i:targets/i:target">
                <xsl:variable name="target">
                  <xsl:value-of select="."/>
                </xsl:variable>
                <td>
                  <xsl:value-of select="count(//i:issue[@status=$state][i:protocol=$protocol][i:target=$target])"></xsl:value-of>
                </td>
              </xsl:for-each>
              <td>
                <xsl:value-of select="count(//i:issue[@status=$state][i:protocol=$protocol])"></xsl:value-of>
              </td>
            </tr>
          </xsl:for-each>
          <tr>
            <th>total</th>
            <xsl:for-each select="//i:head/i:targets/i:target">
              <xsl:variable name="target">
                <xsl:value-of select="."></xsl:value-of>
              </xsl:variable>
              <td>
                <xsl:value-of select="count(//i:issue[i:target=$target][i:protocol=$protocol])"></xsl:value-of>
              </td>
            </xsl:for-each>
            <td>
              <xsl:value-of select="count(//i:issue[i:protocol=$protocol])"></xsl:value-of>
            </td>
          </tr>
        </tbody>
      </table>
    </xsl:for-each>
  </xsl:template>

  <!-- key for summaries -->
  <!--<xsl:key name="type" match="//i:states/i:state" use="."/>-->

  <!-- summary -->
  <xsl:template name="summary">
    <xsl:param name="type"/>
    <xsl:param name="protocol"/>
    <xsl:choose>
      <xsl:when test="($protocol)">
        <h2>
          <a name="{$protocol}-{$type}">
            <xsl:value-of select="$type"/> Issues Summary for <xsl:value-of select="$protocol"/>
          </a>
        </h2>
      </xsl:when>
      <xsl:otherwise>
        <h2>
          <a name="{$type}">
            <xsl:value-of select="$type"/> Issues Summary
          </a>
        </h2>
      </xsl:otherwise>
    </xsl:choose>

    <table cellspacing="0" width="95%" class="summary">
      <tr>
        <th>id</th>
        <th>owner</th>
        <th>title</th>
        <th>protocol</th>
        <th>type</th>
      </tr>
      <xsl:choose>
        <xsl:when test="($protocol)">
          <xsl:apply-templates select="//i:issue[@status=$type][i:protocol=$protocol]" mode="summaryitem"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="//i:issue[@status=$type]" mode="summaryitem"/>
        </xsl:otherwise>
      </xsl:choose>

    </table>
  </xsl:template>

  <!-- summary item -->
  <xsl:template match="i:issue" mode="summaryitem">
    <tr class="{@status}">
      <th>
        <a href="#{@id}">
          <xsl:value-of select="@id"></xsl:value-of>
        </a>
      </th>
      <td>
        <small>
          <xsl:apply-templates select="i:owner" mode="summary"></xsl:apply-templates>
        </small>
      </td>
      <td>
        <small>
          <xsl:value-of select="i:title"></xsl:value-of>
        </small>
      </td>
      <td>
        <small>
          <xsl:for-each select="i:protocol">
            <xsl:value-of select="."/>
            <xsl:if test="position()!=last()">, </xsl:if>
          </xsl:for-each>
        </small>
      </td>
      <td>
        <small>
          <xsl:value-of select="i:type"></xsl:value-of>
        </small>
      </td>
      <td>
        <xsl:if test="@status='Active' and count(i:proposal)>0">
          <span class="marker">*</span>
        </xsl:if>
      </td>
    </tr>
  </xsl:template>

  <!-- summary owner -->
  <xsl:template match="i:owner" mode="summary">
    <xsl:choose>
      <xsl:when test="@href">
        <a href="{@href}">
          <xsl:value-of select="."/>
        </a>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="."/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- issue listing -->
  <xsl:template match="i:issue" mode="list">
    <li>
      <a href="#{@id}">
        <xsl:value-of select="@id"></xsl:value-of> -
        <xsl:value-of select="i:title"></xsl:value-of>
      </a>
    </li>
  </xsl:template>

  <!-- full issue -->
  <xsl:template match="i:issue" mode="full">
    <xsl:variable name="status">
      <xsl:value-of select="@status"/>
    </xsl:variable>
    <xsl:if test="count(//i:head/i:states/i:state[text()=$status])=0">
      <xsl:message>
        <xsl:value-of select="@status"/> not a valid status.
      </xsl:message>
    </xsl:if>
    <tbody class="{@status}">
      <tr class="title">
        <td valign="top" align="right" class="title">
          <a name="{@id}">
            <xsl:value-of select="@id"></xsl:value-of>
          </a>
        </td>
        <td class="title">
          <strong>
            <xsl:value-of select="i:title"></xsl:value-of>
          </strong>
        </td>
        <td class="title" align="right">
          <xsl:for-each select="i:target">
            <xsl:value-of select="."/> -
          </xsl:for-each>
          <xsl:if test="i:type">
            <xsl:value-of select="i:type"></xsl:value-of> -
          </xsl:if>
          <xsl:value-of select="@status"></xsl:value-of>
        </td>
      </tr>
      <tr>
        <td>
        </td>
        <td colspan="2">
          <dl>
            <dt>Description</dt>
            <dd>
              <xsl:apply-templates select="i:description" mode="text"/>
            </dd>
            <dt>Raised against / Related drafts</dt>
            <dd>
              <xsl:apply-templates select="i:protocol"/>
            </dd>
            <xsl:apply-templates select="i:justification"/>
            <dt>Related Issues</dt>
            <dd>
              <xsl:apply-templates select="i:relid"/>
            </dd>
            <xsl:apply-templates select="i:origin"/>
            <xsl:apply-templates select="i:owner"/>
            <xsl:apply-templates select="i:proposal|i:resolution" mode="issue-body"/>
          </dl>
        </td>
      </tr>
    </tbody>
  </xsl:template>

  <!-- protocol revision -->
  <xsl:template match="i:protocol">
    <xsl:variable name="protocol">
      <xsl:value-of select="text()"/>
    </xsl:variable>
    <xsl:variable name="revision">
      <xsl:value-of select="@revision"/>
    </xsl:variable>
    <xsl:variable name="link">
      <xsl:for-each select="//i:head/i:protocols/i:protocol">
        <xsl:if test="@name=$protocol">
          <xsl:for-each select="i:revision">
            <xsl:if test="@artifact=$revision">
              <xsl:value-of select="@href"/>
            </xsl:if>
          </xsl:for-each>
        </xsl:if>
      </xsl:for-each>
    </xsl:variable>
    <a href="{$link}">
      <xsl:apply-templates select="." mode="text"/> revision: <xsl:value-of select="@revision"/>
    </a>
    <br/>
  </xsl:template>

  <!-- justification -->
  <xsl:template match="i:justification">
    <dt>Justification</dt>
    <dd>
      <xsl:apply-templates select="." mode="text"/>
    </dd>
  </xsl:template>

  <!-- related issues -->
  <xsl:template match="i:relid">
    <xsl:variable name="relid">
      <xsl:value-of select="."/>
    </xsl:variable>
    <a href="#{$relid}">
      <xsl:apply-templates select="." mode="text"/>
    </a>
  </xsl:template>

  <!-- origin -->
  <xsl:template match="i:origin">
    <dt>Origin</dt>
    <dd>
      <xsl:choose>
        <xsl:when test="@href">
          <a href="{@href}">
            <xsl:value-of select="."/>
          </a>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="."/>
        </xsl:otherwise>
      </xsl:choose>
    </dd>
  </xsl:template>

  <!-- owner -->
  <xsl:template match="i:owner">
    <dt>Owner</dt>
    <dd>
      <xsl:choose>
        <xsl:when test="@href">
          <a href="{@href}">
            <xsl:value-of select="."/>
          </a>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="."/>
        </xsl:otherwise>
      </xsl:choose>
    </dd>
  </xsl:template>

  <!-- proposal -->
  <xsl:template match="i:proposal" mode="issue-body">
    <xsl:variable name="num">
      <xsl:value-of select="count(preceding-sibling::i:proposal)+1"/>
    </xsl:variable>
    <dt class="proposal">
      <xsl:choose>
        <xsl:when test="@href">
          <a class="ext" href="{@href}">
            Proposal <xsl:value-of select="$num"/>
          </a>
        </xsl:when>
        <xsl:otherwise>
          Proposal <xsl:value-of select="$num"/>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:if test="@date">
        <span class="date">
          <xsl:value-of select="@date"/>
        </span>
      </xsl:if>
    </dt>
    <dd class="proposal">
      <xsl:apply-templates select="i:protocol"/>
      <xsl:apply-templates mode="text"/>
    </dd>
  </xsl:template>

  <!-- resolution -->
  <xsl:template match="i:resolution" mode="issue-body">
    <dt class="resolution">
      <xsl:choose>
        <xsl:when test="@href">
          <a class="ext" href="{@href}">Resolution</a>
        </xsl:when>
        <xsl:otherwise>Resolution</xsl:otherwise>
      </xsl:choose>
      <xsl:if test="@date">
        <span class="date">
          <xsl:value-of select="@date"/>
        </span>
      </xsl:if>
    </dt>
    <dd class="resolution">
      <xsl:apply-templates select="i:protocol"/>
      <xsl:apply-templates mode="text"/>
    </dd>
  </xsl:template>

  <!-- AI open items list -->
  <xsl:template match="i:action" mode="list">
    <li>
      <a href="#{@id}">
        <xsl:value-of select="@id"/> - <xsl:value-of select="i:title"/>
      </a>
    </li>
  </xsl:template>
  
  <!-- AI complete list-->
  <xsl:template match="i:action" mode="complete">
    <tbody class="{@status}">
      <tr class="{@status}">
        <td valign="top" align="right" class="title">
          <a name="{@id}">
            <xsl:value-of select="@id"/> : <xsl:value-of select="@date"/>
          </a>
        </td>
        <td class="{@status}">
          <strong>
            <xsl:value-of select="i:title"></xsl:value-of>
          </strong>
        </td>
        <td class="{@status}">
          <xsl:value-of select="@status"/>
        </td>
      </tr>
      <xsl:if test="i:description | i:owner">
        <tr>
          <td>
          </td>
          <td colspan="2">
            <dl>
              <xsl:if test="i:description">
                <dt>Description</dt>
                <dd>
                  <xsl:apply-templates select="i:description" mode="text"/>
                </dd>
              </xsl:if>
              <xsl:if test="i:owner">
                <dt>Owner</dt>
                <dd>
                  <xsl:choose>
                    <xsl:when test="@href">
                      <a href="{@href}">
                        <xsl:value-of select="i:owner"/>
                      </a>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="i:owner"/>
                    </xsl:otherwise>
                  </xsl:choose>
                </dd>
              </xsl:if>
            </dl>
          </td>
        </tr>
      </xsl:if>
    </tbody>
  </xsl:template>

  <!-- text -->
  <xsl:template match="h:p" mode="text">
    <p>
      <xsl:apply-templates mode="text"/>
    </p>
  </xsl:template>
  <xsl:template match="h:br" mode="text">
    <br/>
    <xsl:apply-templates mode="text"/>
  </xsl:template>
  <xsl:template match="h:ol" mode="text">
    <ol>
      <xsl:apply-templates mode="text"/>
    </ol>
  </xsl:template>
  <xsl:template match="h:ul" mode="text">
    <ul>
      <xsl:apply-templates mode="text"/>
    </ul>
  </xsl:template>
  <xsl:template match="h:li" mode="text">
    <li>
      <xsl:apply-templates mode="text"/>
    </li>
  </xsl:template>
  <xsl:template match="h:dl" mode="text">
    <dl>
      <xsl:apply-templates mode="text"/>
    </dl>
  </xsl:template>
  <xsl:template match="h:dd" mode="text">
    <dd>
      <xsl:apply-templates mode="text"/>
    </dd>
  </xsl:template>
  <xsl:template match="h:dt" mode="text">
    <dt>
      <xsl:apply-templates mode="text"/>
    </dt>
  </xsl:template>
  <xsl:template match="h:i" mode="text">
    <i>
      <xsl:apply-templates mode="text"/>
    </i>
  </xsl:template>
  <xsl:template match="h:em" mode="text">
    <em>
      <xsl:apply-templates mode="text"/>
    </em>
  </xsl:template>
  <xsl:template match="h:b" mode="text">
    <b>
      <xsl:apply-templates mode="text"/>
    </b>
  </xsl:template>
  <xsl:template match="h:strong" mode="text">
    <strong>
      <xsl:apply-templates mode="text"/>
    </strong>
  </xsl:template>
  <xsl:template match="h:code" mode="text">
    <code>
      <xsl:apply-templates mode="text"/>
    </code>
  </xsl:template>
  <xsl:template match="h:pre" mode="text">
    <pre>
      <xsl:apply-templates mode="text"/>
    </pre>
  </xsl:template>
  <xsl:template match="h:a" mode="text">
    <a class="ext" href="{@href}">
      <xsl:apply-templates mode="text"/>
    </a>
  </xsl:template>

  <!--<xsl:template match="i:description">
    -->
  <!-- The below always copied the elements as the default namespace of the other doc 
    even though they were qualified otherwise-->
  <!--
    <xsl:copy-of select="@*|*|text()"/>
  </xsl:template>-->
</xsl:stylesheet>
