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

   <!--
      (c) 2004 Eric Kemp-Benedict

      This stylesheet extracts the qualitative information from a ScenML document and formats
      it as HTML.

      You may use this stylesheet and modify it. The author is not liable for any damages
      that might be caused by the use of this stylesheet.
   -->

   <xsl:output method="html"/>

   <xsl:key name="bibentries" match="ScenMLdoc/bibliography/entry" use="@key"/>

   <!-- Get aliases, if they exist -->
   <xsl:variable name="rname" select="/ScenMLdoc/items/alias[@item='R']/@use"/>
   <xsl:variable name="tname" select="/ScenMLdoc/items/alias[@item='T']/@use"/>
   <xsl:variable name="dfname" select="/ScenMLdoc/items/alias[@item='F']/@use"/>
   <xsl:variable name="aname" select="/ScenMLdoc/items/alias[@item='A']/@use"/>
   <xsl:variable name="sname" select="/ScenMLdoc/items/alias[@item='S']/@use"/>

   <xsl:template match="ScenMLdoc">
      <html>
      <head>
      <title><xsl:value-of select="@name"/></title>
      </head>
      <body>
      <h1><xsl:value-of select="@name"/></h1>

         <!-- Introduction: summarize scenarios, years, regions and themes -->

         <h2>Introduction</h2>

         <!-- Main introductory paragraph -->
         <p>The <xsl:choose><xsl:when test="count(/ScenMLdoc/items/scenario)=1">scenario begins</xsl:when><xsl:otherwise>scenarios begin</xsl:otherwise></xsl:choose> in the year <xsl:value-of select="/ScenMLdoc/years/baseyr/@val"/> and
            <xsl:choose><xsl:when test="count(/ScenMLdoc/items/scenario)=1">extends</xsl:when><xsl:otherwise>extend</xsl:otherwise></xsl:choose> to 
            <xsl:apply-templates select="/ScenMLdoc/years/scenyr/@val"><xsl:sort data-type="number"/></xsl:apply-templates>.
         <xsl:choose>
            <xsl:when test="count(/ScenMLdoc/items/scenario)=1">
               There is one scenario, <i><xsl:value-of select="/ScenMLdoc/items/scenario/@name"/></i>.
            </xsl:when>
            <xsl:otherwise>
               <xsl:choose>
                  <xsl:when test="count(/ScenMLdoc/items/scenario)=2">Two</xsl:when>
                  <xsl:when test="count(/ScenMLdoc/items/scenario)=3">Three</xsl:when>
                  <xsl:when test="count(/ScenMLdoc/items/scenario)=4">Four</xsl:when>
                  <xsl:when test="count(/ScenMLdoc/items/scenario)=5">Five</xsl:when>
                  <xsl:when test="count(/ScenMLdoc/items/scenario)=6">Six</xsl:when>
                  <xsl:when test="count(/ScenMLdoc/items/scenario)=7">Seven</xsl:when>
                  <xsl:when test="count(/ScenMLdoc/items/scenario)=8">Eight</xsl:when>
                  <xsl:when test="count(/ScenMLdoc/items/scenario)=9">Nine</xsl:when>
                  <xsl:when test="count(/ScenMLdoc/items/scenario)=10">Ten</xsl:when>
                  <xsl:when test="count(/ScenMLdoc/items/scenario)=11">Eleven</xsl:when>
                  <xsl:when test="count(/ScenMLdoc/items/scenario)=12">Twelve</xsl:when>
                  <xsl:otherwise><xsl:value-of select="count(/ScenMLdoc/items/scenario)"/></xsl:otherwise>
               </xsl:choose>
                  scenarios are presented:
                  <xsl:for-each select="/ScenMLdoc/items/scenario">
                     <xsl:choose>
                        <xsl:when test="position()=1"></xsl:when>
                        <xsl:when test="position()=last()"> and </xsl:when>
                        <xsl:otherwise>, </xsl:otherwise>
                     </xsl:choose>
                     <i><xsl:value-of select="@name"/></i>
                  </xsl:for-each>.
            </xsl:otherwise>
         </xsl:choose>
         <xsl:choose><xsl:when test="count(/ScenMLdoc/items/scenario)=1">The scenario is</xsl:when><xsl:otherwise>Scenarios are</xsl:otherwise></xsl:choose> developed for
         <xsl:choose>
            <xsl:when test="count(/ScenMLdoc/items/region)=1">
                one region, <xsl:value-of select="region/@name"/>.
            </xsl:when>
            <xsl:otherwise>
               <xsl:choose>
                  <xsl:when test="count(/ScenMLdoc/items/region)=2">two</xsl:when>
                  <xsl:when test="count(/ScenMLdoc/items/region)=3">three</xsl:when>
                  <xsl:when test="count(/ScenMLdoc/items/region)=4">four</xsl:when>
                  <xsl:when test="count(/ScenMLdoc/items/region)=5">five</xsl:when>
                  <xsl:when test="count(/ScenMLdoc/items/region)=6">six</xsl:when>
                  <xsl:when test="count(/ScenMLdoc/items/region)=7">seven</xsl:when>
                  <xsl:when test="count(/ScenMLdoc/items/region)=8">eight</xsl:when>
                  <xsl:when test="count(/ScenMLdoc/items/region)=9">nine</xsl:when>
                  <xsl:when test="count(/ScenMLdoc/items/region)=10">ten</xsl:when>
                  <xsl:when test="count(/ScenMLdoc/items/region)=11">eleven</xsl:when>
                  <xsl:when test="count(/ScenMLdoc/items/region)=12">twelve</xsl:when>
                  <xsl:otherwise><xsl:value-of select="count(/ScenMLdoc/items/region)"/></xsl:otherwise>
               </xsl:choose>
                  regions:
                  <xsl:for-each select="/ScenMLdoc/items/region">
                     <xsl:choose>
                        <xsl:when test="position()=1"></xsl:when>
                        <xsl:when test="position()=last()"> and </xsl:when>
                        <xsl:otherwise>, </xsl:otherwise>
                     </xsl:choose>
                     <xsl:value-of select="@name"/>
                  </xsl:for-each>.
            </xsl:otherwise>
         </xsl:choose>
         The main themes explored in the
            <xsl:choose><xsl:when test="count(/ScenMLdoc/items/scenario)=1">scenario</xsl:when><xsl:otherwise>scenarios</xsl:otherwise></xsl:choose> are
            <xsl:for-each select="/ScenMLdoc/items/theme">
               <xsl:choose>
                  <xsl:when test="position()=1"><i><xsl:value-of select="@name"/></i></xsl:when>
                  <xsl:when test="position()=last()"> and <i><xsl:value-of select="@name"/>.</i></xsl:when>
                  <xsl:otherwise>, <i><xsl:value-of select="@name"/></i></xsl:otherwise>
               </xsl:choose>
            </xsl:for-each>
         The forces driving the
            <xsl:choose><xsl:when test="count(/ScenMLdoc/items/scenario)=1">scenario</xsl:when><xsl:otherwise>scenarios</xsl:otherwise></xsl:choose> forward are
            <xsl:for-each select="/ScenMLdoc/items/drvforce">
               <xsl:choose>
                  <xsl:when test="position()=1"><i><xsl:value-of select="@name"/></i></xsl:when>
                  <xsl:when test="position()=last()"> and <i><xsl:value-of select="@name"/>.</i></xsl:when>
                  <xsl:otherwise>, <i><xsl:value-of select="@name"/></i></xsl:otherwise>
               </xsl:choose>
            </xsl:for-each></p>

         <!-- End of introduction -->

         <!-- Regions -->
         <xsl:if test="/ScenMLdoc/items/region/text">
            <h3><xsl:choose><xsl:when test="$rname"><xsl:value-of select="$rname"/></xsl:when><xsl:otherwise>Region</xsl:otherwise></xsl:choose></h3>
            <xsl:for-each select="/ScenMLdoc/items/region">
               <xsl:if test="text">
                  <h4><xsl:value-of select="@name"/></h4>
                  <xsl:apply-templates select="text"/>
               </xsl:if>
            </xsl:for-each>
         </xsl:if>

         <!-- Themes -->
         <xsl:if test="/ScenMLdoc/items/theme/text">
            <h3><xsl:choose><xsl:when test="$tname"><xsl:value-of select="$tname"/></xsl:when><xsl:otherwise>Theme</xsl:otherwise></xsl:choose></h3>
            <xsl:for-each select="/ScenMLdoc/items/theme">
               <xsl:if test="text">
                  <h4><xsl:value-of select="@name"/></h4>
                  <xsl:apply-templates select="text"/>
               </xsl:if>
            </xsl:for-each>
         </xsl:if>

         <!-- Driving Forces -->
         <xsl:if test="/ScenMLdoc/items/drvforce/text">
            <h3><xsl:choose><xsl:when test="$dfname"><xsl:value-of select="$dfname"/></xsl:when><xsl:otherwise>Driving Force</xsl:otherwise></xsl:choose></h3>
            <xsl:for-each select="/ScenMLdoc/items/drvforce">
               <xsl:if test="text">
                  <h4><xsl:value-of select="@name"/></h4>
                  <xsl:apply-templates select="text"/>
               </xsl:if>
            </xsl:for-each>
         </xsl:if>

         <!-- Actors -->
         <xsl:if test="/ScenMLdoc/items/actor/text">
            <h3><xsl:choose><xsl:when test="$aname"><xsl:value-of select="$aname"/></xsl:when><xsl:otherwise>Actor</xsl:otherwise></xsl:choose></h3>
            <xsl:for-each select="/ScenMLdoc/items/actor">
               <xsl:if test="text">
                  <h4><xsl:value-of select="@name"/></h4>
                  <xsl:apply-templates select="text"/>
               </xsl:if>
            </xsl:for-each>
         </xsl:if>

         <!-- Scenarios -->
         <xsl:if test="/ScenMLdoc/items/scenario/text">
            <h3><xsl:choose><xsl:when test="$sname"><xsl:value-of select="$sname"/></xsl:when><xsl:otherwise>Scenario</xsl:otherwise></xsl:choose></h3>
            <xsl:for-each select="/ScenMLdoc/items/scenario">
               <xsl:if test="text">
                  <h4><xsl:value-of select="@name"/></h4>
                  <xsl:apply-templates select="text"/>
               </xsl:if>
            </xsl:for-each>
         </xsl:if>

         <!-- Notes -->
         <h2>Notes</h2>
         <xsl:for-each select="/ScenMLdoc/notes/note">
            <!-- Awkward, but have to check all possible combinations of attributes for the note -->
            <h3>
            <xsl:if test="@region and @scenario and @actor and @year">
               For <xsl:value-of select="@actor"/> in <xsl:value-of select="@region"/> in <em><xsl:value-of select="@scenario"/></em> for <xsl:value-of select="@year"/>
            </xsl:if>
            <xsl:if test="@region and @scenario and @actor and not(@year)">
               For <xsl:value-of select="@actor"/> in <xsl:value-of select="@region"/> in <em><xsl:value-of select="@scenario"/></em>
            </xsl:if>
            <xsl:if test="@region and @scenario and not(@actor) and @year">
               For <xsl:value-of select="@region"/> in <em><xsl:value-of select="@scenario"/></em> for <xsl:value-of select="@year"/>
            </xsl:if>
            <xsl:if test="@region and @scenario and not(@actor) and not(@year)">
               For <xsl:value-of select="@region"/> in <em><xsl:value-of select="@scenario"/></em>
            </xsl:if>
            <xsl:if test="@region and not(@scenario) and @actor and @year">
               For <xsl:value-of select="@actor"/> in <xsl:value-of select="@region"/> for <xsl:value-of select="@year"/>
            </xsl:if>
            <xsl:if test="@region and not(@scenario) and @actor and not(@year)">
               For <xsl:value-of select="@actor"/> in <xsl:value-of select="@region"/>
            </xsl:if>
            <xsl:if test="@region and not(@scenario) and not(@actor) and @year">
               For <xsl:value-of select="@region"/> in <xsl:value-of select="@year"/>
            </xsl:if>
            <xsl:if test="@region and not(@scenario) and not(@actor) and not(@year)">
               For <xsl:value-of select="@region"/>
            </xsl:if>
            <xsl:if test="not(@region) and @scenario and @actor and @year">
               For <xsl:value-of select="@actor"/> in <em><xsl:value-of select="@scenario"/></em> for <xsl:value-of select="@year"/>
            </xsl:if>
            <xsl:if test="not(@region) and @scenario and @actor and not(@year)">
               For <xsl:value-of select="@actor"/> in <em><xsl:value-of select="@scenario"/></em>
            </xsl:if>
            <xsl:if test="not(@region) and @scenario and not(@actor) and @year">
               For <em><xsl:value-of select="@scenario"/></em> in <xsl:value-of select="@year"/>
            </xsl:if>
            <xsl:if test="not(@region) and @scenario and not(@actor) and not(@year)">
               For <em><xsl:value-of select="@scenario"/></em>
            </xsl:if>
            <xsl:if test="not(@region) and not(@scenario) and @actor and @year">
               For <xsl:value-of select="@actor"/> in <xsl:value-of select="@year"/>
            </xsl:if>
            <xsl:if test="not(@region) and not(@scenario) and @actor and not(@year)">
               For <xsl:value-of select="@actor"/>
            </xsl:if>
            <xsl:if test="not(@region) and not(@scenario) and not(@actor) and @year">
               For <xsl:value-of select="@year"/>
            </xsl:if>
            <xsl:if test="not(@region) and not(@scenario) and not(@actor) and not(@year)">
               General
            </xsl:if>
            </h3>
            <xsl:apply-templates/>
         </xsl:for-each>

         <!-- Bibliography -->
         <h2>Bibliography</h2>
         <!-- Generate a special "bibentry" class paragraph for use with CSS -->
         <xsl:for-each select="/ScenMLdoc/bibliography/entry/p">
            <p class="bibentry"><a name="{../@key}"/><xsl:apply-templates/></p>
         </xsl:for-each>

      </body>
      </html>
   </xsl:template>


   <!-- Generic templates -->

   <!-- Find last scenario year -->
   <xsl:template match="scenyr/@val">
      <xsl:choose><xsl:when test="position()=last()"> <xsl:value-of select="."/> </xsl:when></xsl:choose>
   </xsl:template>

   <!-- Except in bibentries, p elements act just like in standard HTML -->
   <xsl:template match="p">
      <p><xsl:apply-templates/></p>
   </xsl:template>

   <!-- Pass b, i, sup and sub right along: this is standard HTML syntax -->
   <xsl:template match="b">
      <b><xsl:value-of select="."/></b>
   </xsl:template>
   <xsl:template match="i">
      <i><xsl:value-of select="."/></i>
   </xsl:template>
   <xsl:template match="sup">
      <sup><xsl:value-of select="."/></sup>
   </xsl:template>
   <xsl:template match="sub">
      <sub><xsl:value-of select="."/></sub>
   </xsl:template>

   <!-- Handle bib references -->
   <xsl:template match="bibref">
      (<a href="#{@key}"><xsl:value-of select="key('bibentries', @key)/@cite"/></a>)
   </xsl:template>

   <!-- Hyperlinks -->
   <xsl:template match="link">
      <a href="{@uri}"><xsl:apply-templates/></a>
   </xsl:template>

</xsl:stylesheet>