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

<xsl:template match="xmld20">
  <html>
  <xsl:apply-templates match="Monsters"/>
  </html>
</xsl:template>

<xsl:template match="Monsters">
  <xsl:apply-templates match="Monster"/>
</xsl:template>

<xsl:template match="Monster">
  <h2>
  <xsl:value-of select="translate(Name,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWYXZ')"/>
  </h2>
  <hr/>
  <xsl:apply-templates select="StatBlock"/>
  <xsl:apply-templates select="Description"/>
  <hr/>
</xsl:template>

<xsl:template match="StatBlock">
  <StatBlock>
    <xsl:call-template name="SizeAndType"/>
    <xsl:call-template name="HitDice"/>
    <xsl:call-template name="Initiative"/>
    <xsl:apply-templates select="Speeds"/>
    <xsl:apply-templates select="ArmorClass"/>
    <xsl:call-template name="BaseAttack"/>
    <xsl:apply-templates select="Attacks"/>
    <xsl:apply-templates select="FullAttacks"/>
    <xsl:call-template name="SpaceReach"/>
    <xsl:apply-templates select="SpecialAttacks"/>
    <xsl:apply-templates select="SpecialQualities"/>
    <xsl:call-template name="Saves"/>
    <xsl:apply-templates select="Abilities"/>
    <xsl:apply-templates select="Skills"/>
    <xsl:apply-templates select="Feats"/>
    <xsl:apply-templates select="Environment"/>
    <xsl:apply-templates select="Organizations"/>
    <xsl:call-template name="CR"/>
    <xsl:apply-templates select="Treasure"/>
    <xsl:apply-templates select="Alignment"/>
    <xsl:apply-templates select="Advancements"/>
    <xsl:call-template name="ECL"/>
  </StatBlock>
</xsl:template>

<xsl:template name="SizeAndType">
    <xsl:variable name="subtypes"><xsl:call-template name="Subtypes"/></xsl:variable>
    <strong>
    <xsl:call-template name="Size"/><xsl:text> </xsl:text><xsl:call-template name="Type"/><xsl:if test="$subtypes"> (<xsl:value-of select="$subtypes"/>)</xsl:if>
    </strong>
    <br/>
</xsl:template>

<xsl:template name="HitDice">
    <xsl:variable name="con"><xsl:call-template name="conbonus"/></xsl:variable>
    <xsl:variable name="dice" select="ancestor-or-self::StatBlock/Levels/Level[@class='monster']"/>
    <xsl:variable name="type"><xsl:call-template name="Type"/></xsl:variable>
    <xsl:variable name="die"><xsl:if test="$type='Aberration'">8</xsl:if></xsl:variable>
    <xsl:variable name="hpbonus" select="$con * Levels/Level[@class='monster']"/>
    <strong>Hit Dice: </strong>

    <xsl:value-of select="$dice"/><xsl:text>d</xsl:text><xsl:value-of select="$die"/>+<xsl:value-of select="$hpbonus"/> (<xsl:value-of select="floor(($dice * ($die + 1)) div 2) + $hpbonus"/>hp)
    <br/>
</xsl:template>

<xsl:template name="Initiative">
    <xsl:variable name="dexbonus"><xsl:call-template name="dexbonus"/></xsl:variable>
    <strong>Initiative:</strong>
    +<xsl:value-of select="$dexbonus"/>
    <br/>
</xsl:template>

<xsl:template match="Speeds">
    <strong>Speed:</strong>
    <xsl:value-of select="Speed[@type='walk']/@rate"/> ft. (<xsl:value-of select="floor(Speed[@type='walk']/@rate div 5)"/> squares)<xsl:if test="Speed[@type='swim']">, swim <xsl:value-of select="Speed[@type='swim']/@rate"/> ft.</xsl:if>
    <br/>
</xsl:template>

<xsl:template match="ArmorClass">
    <xsl:variable name="armorac" select="0"/>
    <xsl:variable name="defac" select="0"/>
    <xsl:variable name="size"><xsl:call-template name="Size"/></xsl:variable>
    <xsl:variable name="sizeac"><xsl:if test="$size='Huge'">-2</xsl:if></xsl:variable>
    <xsl:variable name="dexac"><xsl:call-template name="dexbonus"/></xsl:variable>
    <xsl:variable name="natac" select="@value"/>
    <strong>Armor Class:</strong>
    <xsl:value-of select="10+$armorac+$sizeac+$dexac+$natac"/> (<xsl:value-of select="$sizeac"/> size, +<xsl:value-of select="$dexac"/> Dex, +<xsl:value-of select="$natac"/> natural), touch <xsl:value-of select="10+$sizeac+$dexac+$defac"/>, flat-footed <xsl:value-of select="10+$armorac+$sizeac+$natac"/>
    <br/>
</xsl:template>

<xsl:template name="BaseAttack">
  <xsl:variable name="base"><xsl:call-template name="BAB"/></xsl:variable>
  <xsl:variable name="size"><xsl:call-template name="Size"/></xsl:variable>
  <xsl:variable name="grapsize"><xsl:if test="$size='Huge'">8</xsl:if></xsl:variable>
  <xsl:variable name="grapstr"><xsl:call-template name="strbonus"/></xsl:variable>
  <xsl:variable name="grapple" select="$base + $grapsize + $grapstr"/>
  <strong>Base Attack/Grapple:</strong>
  +<xsl:value-of select="$base"/>/+<xsl:value-of select="$grapple"/>
  <br/>
</xsl:template>

<xsl:template match="Attacks">
  <strong>Attack: </strong>
  <xsl:for-each select="Attack">
  <xsl:if test="position()!=1"> or </xsl:if>
  <xsl:value-of select="@name"/> +<xsl:call-template name="MeleeAttack"/> melee (<xsl:call-template name="AttackDamage"/>)
  </xsl:for-each>
  <br/>
</xsl:template>

<xsl:template match="FullAttacks">
  <strong>Full Attack: </strong>
  <xsl:for-each select="FullAttack">
    <xsl:if test="position()>1">; or </xsl:if>
    <xsl:for-each select="Attack">
      <xsl:if test="position()>1"> and </xsl:if>
      <xsl:variable name="attack" select="@attack"/>
      <xsl:value-of select="@count"/><xsl:text> </xsl:text><xsl:value-of select="$attack"/>s +<xsl:call-template name="MeleeAttack"/> melee (<xsl:for-each select="ancestor-or-self::StatBlock/Attacks/Attack[@name=$attack]"><xsl:call-template name="AttackDamage"/></xsl:for-each>)</xsl:for-each>
  </xsl:for-each>
  <br/>
</xsl:template>

<xsl:template name="SpaceReach">
  <strong>Space/Reach: </strong>
  <xsl:value-of select="ancestor-or-self::StatBlock/Size/@space"/> ft./<xsl:value-of select="ancestor-or-self::StatBlock/Size/@reach"/> ft.
  <br/>
</xsl:template>

<xsl:template match="SpecialAttacks">
  <strong>Special Attacks: </strong>
  <xsl:for-each select="SpecialAttack">
  <xsl:if test="position()!=1">, </xsl:if>
  <xsl:value-of select="."/>
  </xsl:for-each>
  <br/>
</xsl:template>

<xsl:template match="SpecialQualities">
  <strong>Special Qualities: </strong>
  <xsl:variable name="type"><xsl:call-template name="Type"/></xsl:variable>
  <xsl:variable name="implicit1">
  <xsl:if test="ancestor-or-self::StatBlock/Type/Subtype = 'Aquatic'">Aquatic subtype</xsl:if>
  </xsl:variable>
  <xsl:variable name="implicit2">
  <xsl:if test="$type='Aberration'">darkvision 60 ft.</xsl:if>
  </xsl:variable>
  <xsl:value-of select="$implicit1"/>
  <xsl:if test="$implicit1 or $implicit2">, </xsl:if>
  <xsl:value-of select="$implicit2"/>
  <xsl:for-each select="SpecialQuality">
  <xsl:if test="$implicit1 or $implicit2 or position()!=1">, </xsl:if>
  <xsl:value-of select="@quality"/>
  </xsl:for-each>
  <br/>
</xsl:template>

<xsl:template name="Saves">
  <xsl:variable name="type"><xsl:call-template name="Type"/></xsl:variable>
  <xsl:variable name="fortbase"><xsl:if test="$type='Aberration'"><xsl:call-template name="PoorSave"/></xsl:if></xsl:variable>
  <xsl:variable name="refbase"><xsl:if test="$type='Aberration'"><xsl:call-template name="PoorSave"/></xsl:if></xsl:variable>
  <xsl:variable name="willbase"><xsl:if test="$type='Aberration'"><xsl:call-template name="GoodSave"/></xsl:if></xsl:variable>
  <xsl:variable name="fortbonus"><xsl:call-template name="conbonus"/></xsl:variable>
  <xsl:variable name="refbonus"><xsl:call-template name="dexbonus"/></xsl:variable>
  <xsl:variable name="willbonus"><xsl:call-template name="wisbonus"/></xsl:variable>
  <xsl:variable name="fortextra"><xsl:choose><xsl:when test="ancestor-or-self::StatBlock/Feats/Feat[@name='Great Fortitude']">2</xsl:when><xsl:otherwise>0</xsl:otherwise></xsl:choose></xsl:variable>
  <xsl:variable name="refextra"><xsl:choose><xsl:when test="ancestor-or-self::StatBlock/Feats/Feat[@name='Lightning Reflexes']">2</xsl:when><xsl:otherwise>0</xsl:otherwise></xsl:choose></xsl:variable>
  <xsl:variable name="willextra"><xsl:choose><xsl:when test="ancestor-or-self::StatBlock/Feats/Feat[@name='Iron Will']">2</xsl:when><xsl:otherwise>0</xsl:otherwise></xsl:choose></xsl:variable>
  <xsl:variable name="fort"><xsl:value-of select="$fortbase + $fortbonus + $fortextra"/></xsl:variable>
  <xsl:variable name="ref"><xsl:value-of select="$refbase + $refbonus + $refextra"/></xsl:variable>
  <xsl:variable name="will"><xsl:value-of select="$willbase + $willbonus + $willextra"/></xsl:variable>

  <strong>Saves: </strong>Fort +<xsl:value-of select="$fort"/>, Ref +<xsl:value-of select="$ref"/>, Will +<xsl:value-of select="$will"/>
  <br/>
</xsl:template>

<xsl:template match="Abilities">
  <strong>Abilities: </strong>Str <xsl:value-of select="@Str"/>, Dex <xsl:value-of select="@Dex"/>, Con <xsl:value-of select="@Con"/>, Int <xsl:value-of select="@Int"/>, Wis <xsl:value-of select="@Wis"/>, Cha <xsl:value-of select="@Cha"/>
  <br/>
</xsl:template>

<xsl:template match="Skills">
  <strong>Skills </strong>
  <xsl:for-each select="Skill">
  <xsl:if test="position()!=1">, </xsl:if>
  <xsl:value-of select="@name"/> +<xsl:call-template name="SkillPoints"/>
  </xsl:for-each>
  <xsl:if test="ancestor-or-self::Monster/Description/Combat/RacialSkill">, </xsl:if>
  <xsl:for-each select="ancestor-or-self::Monster/Description/Combat/RacialSkill">
    <xsl:if test="position()!=1">, </xsl:if>
    <xsl:value-of select="@skill"/> +<xsl:value-of select="@bonus"/>
  </xsl:for-each>
  <br/>
</xsl:template>

<xsl:template match="Feats">
  <strong>Feats: </strong>
  <xsl:for-each select="Feat">
     <xsl:if test="position()!=1">, </xsl:if>
     <xsl:value-of select="@name"/>
  </xsl:for-each>
  <br/>
</xsl:template>

<xsl:template match="Environment">
  <strong>Environment: </strong>
  <xsl:value-of select="."/>
  <br/>
</xsl:template>

<xsl:template match="Organizations">
  <strong>Organization: </strong>
  <xsl:for-each select="Organization">
    <xsl:if test="position()!=1">, </xsl:if>
    <xsl:if test="position()=last()">or </xsl:if>
    <xsl:value-of select="@name"/><xsl:if test="@count!='1'"> (<xsl:value-of select="@count"/><xsl:if test="Extras"> plus <xsl:value-of select="Extras/@count"/><xsl:text> </xsl:text><xsl:value-of select="Extras/@name"/></xsl:if>)</xsl:if>
  </xsl:for-each>
  <br/>
</xsl:template>

<xsl:template name="CR">
  <strong>Challenge Rating: </strong>
  <xsl:value-of select="ancestor-or-self::StatBlock/Levels/@cr"/>
  <br/>
</xsl:template>

<xsl:template match="Treasure">
  <strong>Treasure: </strong>
  <xsl:if test="@multiplier=2">Double standard</xsl:if>
  <br/>
</xsl:template>

<xsl:template match="Alignment">
  <strong>Alignment: </strong>
  <xsl:if test="@flexibility"><xsl:value-of select="@flexibility"/><xsl:text> </xsl:text></xsl:if>
  <xsl:value-of select="@LawChaos"/><xsl:text> </xsl:text><xsl:value-of select="@GoodEvil"/>
  <br/>
</xsl:template>

<xsl:template match="Advancements">
  <strong>Advancement: </strong>
    <xsl:for-each select="Advancement"><xsl:if test="position()!=1">; </xsl:if><xsl:value-of select="@range"/> HD (<xsl:value-of select="@size"/>)</xsl:for-each>
  <br/>
</xsl:template>

<xsl:template name="ECL">
  <xsl:variable name="ecl" select="ancestor-or-self::StatBlock/Levels/@ecl"/>
  <strong>Level Adjustment: </strong>
    <xsl:if test="$ecl = 0">-</xsl:if>
    <xsl:if test="$ecl != 0"><xsl:value-of select="$ecl"/></xsl:if>
  <br/>
</xsl:template>

<xsl:template match="Description">
  <P/>
  <xsl:apply-templates select="General"/>
  <xsl:apply-templates select="Combat"/>
</xsl:template>

<xsl:template match="General">
  <xsl:value-of select="."/>
</xsl:template>

<xsl:template match="Combat">
  <h3>Combat</h3>
  <xsl:value-of select="Text"/>
  <P/>
  <xsl:for-each select="SpecialAbility">
  <strong><xsl:value-of select="@Name"/> (<xsl:value-of select="@Type"/>): </strong><xsl:value-of select="Text"/>
  <br/>
  </xsl:for-each>
  <xsl:if test="RacialSkill">
  <strong>Skills: </strong><xsl:for-each select="RacialSkill">
    <xsl:value-of select="."/>
  </xsl:for-each><xsl:text>
</xsl:text>
  </xsl:if>
  <br/>
</xsl:template>

<!-- MACROS -->

<xsl:template name="Size"><xsl:value-of select="ancestor-or-self::StatBlock/Size/@category"/></xsl:template>

<xsl:template name="Type"><xsl:value-of select="ancestor-or-self::StatBlock/Type/@base"/></xsl:template>

<xsl:template name="Subtypes">
<xsl:for-each select="ancestor-or-self::StatBlock/Type/Subtype">
<xsl:if test="position()!=1">, </xsl:if>
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:template>

<xsl:template name="BAB"><xsl:if test="ancestor-or-self::StatBlock/Type/@base='Aberration'"><xsl:value-of select="floor((ancestor-or-self::StatBlock/Levels/Level * 3) div 4)"/></xsl:if></xsl:template>

<xsl:template name="strbonus"><xsl:value-of select="floor((ancestor-or-self::StatBlock/Abilities/@Str - 10) div 2)"/></xsl:template>

<xsl:template name="intbonus"><xsl:value-of select="floor((ancestor-or-self::StatBlock/Abilities/@Int - 10) div 2)"/></xsl:template>

<xsl:template name="dexbonus"><xsl:value-of select="floor((ancestor-or-self::StatBlock/Abilities/@Dex - 10) div 2)"/></xsl:template>

<xsl:template name="conbonus"><xsl:value-of select="floor((ancestor-or-self::StatBlock/Abilities/@Con - 10) div 2)"/></xsl:template>

<xsl:template name="wisbonus"><xsl:value-of select="floor((ancestor-or-self::StatBlock/Abilities/@Wis - 10) div 2)"/></xsl:template>

<xsl:template name="chabonus"><xsl:value-of select="floor((ancestor-or-self::StatBlock/Abilities/@Cha - 10) div 2)"/></xsl:template>

<xsl:template name="MeleeAttack">
<xsl:variable name="bab"><xsl:call-template name="BAB"/></xsl:variable>
<xsl:variable name="strbonus"><xsl:call-template name="strbonus"/></xsl:variable>
<xsl:variable name="sizeattack"><xsl:if test="ancestor-or-self::StatBlock/Size/@category='Huge'">-2</xsl:if></xsl:variable>
<xsl:value-of select="$bab + $strbonus + $sizeattack"/>
</xsl:template>

<xsl:template name="AttackDamage">
<xsl:call-template name="NaturalDamage"/>
<xsl:if test="@extra"> plus <xsl:value-of select="@extra"/></xsl:if>
</xsl:template>

<xsl:template name="NaturalDamage"><xsl:if test="ancestor-or-self::StatBlock/Size/@category='Huge'">1d6</xsl:if>+<xsl:call-template name="strbonus"/></xsl:template>

<xsl:template name="PoorSave"><xsl:value-of select="floor(ancestor-or-self::StatBlock/Levels/Level div 3)"/></xsl:template>

<xsl:template name="GoodSave"><xsl:value-of select="floor(ancestor-or-self::StatBlock/Levels/Level div 2) + 2"/></xsl:template>

<xsl:template name="SkillPoints">
  <xsl:variable name="ability">
  <xsl:if test="@name='Concentration' or @name='Listen' or @name='Spot'"><xsl:call-template name="wisbonus"/></xsl:if>
  <xsl:if test="contains(@name,'Knowledge')"><xsl:call-template name="intbonus"/></xsl:if>
  </xsl:variable>
  <xsl:variable name="extra">
    <xsl:choose>
    <xsl:when test="ancestor-or-self::StatBlock/Feats/Feat[@name='Alertness'] and @name='Listen'">2</xsl:when>
    <xsl:when test="ancestor-or-self::StatBlock/Feats/Feat[@name='Alertness'] and @name='Spot'">2</xsl:when>
    <xsl:otherwise>0</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:value-of select="$ability + @ranks + $extra"/>
</xsl:template>

</xsl:stylesheet>

