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

<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(Kind,'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="Treasures"/>
    <xsl:apply-templates select="Alignment"/>
    <xsl:apply-templates select="Advancements"/>
    <xsl:call-template name="LevelAdjustment"/>
  </StatBlock>
</xsl:template>

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

<xsl:template name="HitDice">
    <strong>Hit Dice: </strong>
    <xsl:variable name="levels" select="ancestor-or-self::StatBlock/Levels"/>
    <xsl:for-each select="$levels/Level">
      <xsl:variable name="dice" select="."/>
      <xsl:variable name="die"><xsl:call-template name="finddie"/></xsl:variable>
      <xsl:variable name="conbonus"><xsl:call-template name="conbonus"/></xsl:variable>
      <xsl:variable name="hpbonus" select="$conbonus * ."/>

      <xsl:if test="position()!=1"> plus </xsl:if>

      <xsl:value-of select="$dice"/><xsl:text>d</xsl:text><xsl:value-of select="$die"/>+<xsl:value-of select="$hpbonus"/>
    </xsl:for-each> (<xsl:for-each select="$levels"><xsl:call-template name="TotalAverageHitpoints"/></xsl:for-each> hp)
    <br/>
</xsl:template>

<xsl:template name="Initiative">
    <xsl:variable name="dexbonus"><xsl:call-template name="dexbonus"/></xsl:variable>
  <xsl:variable name="extrabonus">
    0<xsl:if test="ancestor-or-self::StatBlock/Feats/Feat[@name='Improved Initiative']">4</xsl:if>
  </xsl:variable>
  <xsl:variable name="total"><xsl:value-of select="$dexbonus + $extrabonus"/></xsl:variable>
    <strong>Initiative: </strong><xsl:choose>
      <xsl:when test="0 > $total">-</xsl:when>
      <xsl:otherwise>+</xsl:otherwise>
    </xsl:choose><xsl:value-of select="$total"/>
    <br/>
</xsl:template>

<xsl:template match="Speeds">
  <strong>Speed: </strong>
    <xsl:for-each select="Speed">
      <xsl:if test="position()!=1">, </xsl:if>
      <xsl:if test="@type!='walk'"><xsl:value-of select="@type"/><xsl:text> </xsl:text></xsl:if><xsl:value-of select="@rate"/> ft.<xsl:if test="@type='walk'"> (<xsl:value-of select="floor(@rate div 5)"/> squares)</xsl:if>
    </xsl:for-each>
  <br/>
</xsl:template>

<xsl:template match="ArmorClass">
    <xsl:variable name="armorac" select="0"/>
    <xsl:variable name="defac" select="0"/>
    <xsl:variable name="sizeac"><xsl:call-template name="sizebonus"/></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="grapplesize"><xsl:call-template name="grapplebonus"/></xsl:variable>
  <xsl:variable name="grapplestr"><xsl:call-template name="strbonus"/></xsl:variable>
  <xsl:variable name="grapple" select="$base + $grapplesize + $grapplestr"/>
  <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[1]">
    <xsl:variable name="attack">
    <xsl:choose>
      <xsl:when test="@type='melee'"><xsl:call-template name="MeleeAttack"/></xsl:when>
      <xsl:when test="@type='ranged'"><xsl:call-template name="RangedAttack"/></xsl:when>
      <xsl:otherwise><xsl:call-template name="MeleeAttack"/></xsl:otherwise>
    </xsl:choose>
    </xsl:variable>
    <xsl:if test="position()!=1"> or </xsl:if>
    <xsl:value-of select="@name"/> +<xsl:value-of select="$attack"/><xsl:text> </xsl:text><xsl:value-of select="@type"/> (<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:variable name="attackname" select="@attack"/>
      <xsl:variable name="type" select="ancestor-or-self::StatBlock/Attacks/Attack[@name=$attackname]/@type"/>
      <xsl:variable name="attack">
      <xsl:choose>
        <xsl:when test="$type='melee'"><xsl:call-template name="MeleeAttack"/></xsl:when>
        <xsl:when test="$type='ranged'"><xsl:call-template name="RangedAttack"/></xsl:when>
        <xsl:otherwise><xsl:call-template name="MeleeAttack"/></xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
      <xsl:if test="position()>1"> and </xsl:if>
      <xsl:value-of select="@count"/><xsl:text> </xsl:text><xsl:value-of select="$attackname"/>s +<xsl:value-of select="$attack"/><xsl:text> </xsl:text><xsl:value-of select="$type"/> (<xsl:for-each select="ancestor-or-self::StatBlock/Attacks/Attack[@name=$attackname]"><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:variable name="type"><xsl:call-template name="Type"/></xsl:variable>
  <xsl:variable name="implicit1">
  <xsl:call-template name="ImplicitAttacks">
    <xsl:with-param name="type"><xsl:value-of select="$type"/></xsl:with-param>
  </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="implicit2">
  <xsl:for-each select="ancestor-or-self::StatBlock/Type/Subtype">
  <xsl:variable name="x">
  <xsl:call-template name="ImplicitAttacks">
    <xsl:with-param name="type"><xsl:value-of select="."/></xsl:with-param>
  </xsl:call-template>
  </xsl:variable>
  <xsl:if test="normalize-space($x) and position()!=1">, </xsl:if>
  <xsl:value-of select="normalize-space($x)"/>
  </xsl:for-each>
  </xsl:variable>
  <xsl:value-of select="$implicit1"/>
  <xsl:if test="normalize-space($implicit1) and normalize-space($implicit2)">, </xsl:if>
  <xsl:value-of select="$implicit2"/>
  <xsl:for-each select="SpecialAttack">
  <xsl:if test="normalize-space($implicit1) or normalize-space($implicit2) or 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:call-template name="ImplicitQualities">
    <xsl:with-param name="type"><xsl:value-of select="$type"/></xsl:with-param>
  </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="implicit2">
  <xsl:for-each select="ancestor-or-self::StatBlock/Type/Subtype">
  <xsl:variable name="x">
  <xsl:call-template name="ImplicitQualities">
    <xsl:with-param name="type"><xsl:value-of select="."/></xsl:with-param>
  </xsl:call-template>
  </xsl:variable>
  <xsl:if test="normalize-space($x) and position()!=1">, </xsl:if>
  <xsl:value-of select="normalize-space($x)"/>
  </xsl:for-each>
  </xsl:variable>
  <xsl:value-of select="$implicit1"/>
  <xsl:if test="normalize-space($implicit1) and normalize-space($implicit2)">, </xsl:if>
  <xsl:value-of select="$implicit2"/>
  <xsl:for-each select="SpecialQuality">
  <xsl:if test="normalize-space($implicit1) or normalize-space($implicit2) or position()!=1">, </xsl:if>
  <xsl:value-of select="."/>
  </xsl:for-each>
  <br/>
</xsl:template>

<xsl:template name="Saves">
  <xsl:variable name="fortbase"><xsl:call-template name="getbasesave"><xsl:with-param name="save">Fortitude</xsl:with-param></xsl:call-template></xsl:variable>
  <xsl:variable name="refbase"><xsl:call-template name="getbasesave"><xsl:with-param name="save">Reflex</xsl:with-param></xsl:call-template></xsl:variable>
  <xsl:variable name="willbase"><xsl:call-template name="getbasesave"><xsl:with-param name="save">Will</xsl:with-param></xsl:call-template></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 name="getbasesave">
<xsl:param name="save"/>
  <xsl:for-each select="ancestor-or-self::StatBlock/Levels/Level[1]">
    <xsl:call-template name="persave"><xsl:with-param name="save" select="$save"/></xsl:call-template>
  </xsl:for-each>
</xsl:template>

<xsl:template name="persave">
<xsl:param name="save"/>
  <xsl:variable name="rest">
    <xsl:choose>
      <xsl:when test="following-sibling::Level[1]">
        <xsl:for-each select="following-sibling::Level[1]">
          <xsl:call-template name="persave"><xsl:with-param name="save" select="$save"/></xsl:call-template>
        </xsl:for-each>
      </xsl:when>
      <xsl:otherwise>0</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="save"><xsl:call-template name="thissave"><xsl:with-param name="save" select="$save"/></xsl:call-template></xsl:variable>
  <xsl:value-of select="$save + $rest"/>
</xsl:template>

<xsl:template name="thissave">
<xsl:param name="save"/>
    <xsl:variable name="type"><xsl:call-template name="Type"/></xsl:variable>
    <xsl:choose>
    <xsl:when test="$save='Fortitude'">
      <xsl:choose>
        <xsl:when test="@class='monster'">
        <xsl:choose>
           <xsl:when test="$type='Animal' or $type='Dragon' or $type='Giant' or $type='Magical beast' or $type='Outsider' or $type='Plant' or $type='Vermin'"><xsl:call-template name="GoodSave"/></xsl:when>          <xsl:when test="$type='Elemental'">
           <xsl:choose>
             <xsl:when test="ancestor-or-self::StatBlock/Type/Subtype='Earth' or ancestor-or-self::StatBlock/Type/Subtype='Water'"><xsl:call-template name="GoodSave"/></xsl:when>
             <xsl:otherwise><xsl:call-template name="PoorSave"/></xsl:otherwise>
           </xsl:choose>
           </xsl:when>
           <xsl:otherwise>
           <xsl:choose>
             <xsl:when test="ancestor-or-self::StatBlock/Saves[@best='Fortitude']"><xsl:call-template name="GoodSave"/></xsl:when>
             <xsl:otherwise><xsl:call-template name="PoorSave"/></xsl:otherwise>
           </xsl:choose>
           </xsl:otherwise>
        </xsl:choose>
        </xsl:when>
        <xsl:when test="@class='Barbarian' or @class='Cleric' or @class='Druid' or @class='Fighter' or @class='Monk' or @class='Paladin' or @class='Ranger'"><xsl:call-template name="GoodSave"/></xsl:when>
        <xsl:otherwise><xsl:call-template name="PoorSave"/></xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:when test="$save='Reflex'">
      <xsl:choose>
        <xsl:when test="@class='monster'">
        <xsl:choose>
           <xsl:when test="$type='Animal' or $type='Dragon' or $type='Fey' or $type='Magical beast' or $type='Monstrous humanoid' or $type='Outsider'"><xsl:call-template name="GoodSave"/></xsl:when>
          <xsl:when test="$type='Elemental'">
           <xsl:choose>
             <xsl:when test="ancestor-or-self::StatBlock/Type/Subtype='Air' or ancestor-or-self::StatBlock/Type/Subtype='Fire'"><xsl:call-template name="GoodSave"/></xsl:when>
             <xsl:otherwise><xsl:call-template name="PoorSave"/></xsl:otherwise>
           </xsl:choose>
           </xsl:when>
           <xsl:otherwise>
           <xsl:choose>
             <xsl:when test="ancestor-or-self::StatBlock/Saves[@best='Reflex']"><xsl:call-template name="GoodSave"/></xsl:when>
             <xsl:otherwise><xsl:call-template name="PoorSave"/></xsl:otherwise>
           </xsl:choose>
           </xsl:otherwise>
        </xsl:choose>
        </xsl:when>
        <xsl:when test="@class='Bard' or @class='Monk' or @class='Ranger' or @class='Rogue'"><xsl:call-template name="GoodSave"/></xsl:when>
        <xsl:otherwise><xsl:call-template name="PoorSave"/></xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:when test="$save='Will'">
      <xsl:choose>
        <xsl:when test="@class='monster'">
        <xsl:choose>
          <xsl:when test="$type='Aberration' or $type='Dragon' or $type='Fey' or $type='Monstrous humanoid' or $type='Outsider' or $type='Undead'"><xsl:call-template name="GoodSave"/></xsl:when>
           <xsl:otherwise>
           <xsl:choose>
             <xsl:when test="ancestor-or-self::StatBlock/Saves[@best='Fortitude']"><xsl:call-template name="GoodSave"/></xsl:when>
             <xsl:otherwise><xsl:call-template name="PoorSave"/></xsl:otherwise>
           </xsl:choose>
           </xsl:otherwise>
        </xsl:choose>
        </xsl:when>
        <xsl:when test="@class='Bard' or @class='Cleric' or @class='Druid' or @class='Monk' or @class='Sorcerer' or @class='Wizard'"><xsl:call-template name="GoodSave"/></xsl:when>
        <xsl:otherwise><xsl:call-template name="PoorSave"/></xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    </xsl:choose>
</xsl:template>

<xsl:template match="Abilities">
  <xsl:variable name="Str"><xsl:value-of select="sum(Ability[@name='Strength']/*)"/></xsl:variable>
  <xsl:variable name="Dex"><xsl:value-of select="sum(Ability[@name='Dexterity']/*)"/></xsl:variable>
  <xsl:variable name="Con"><xsl:value-of select="sum(Ability[@name='Constitution']/*)"/></xsl:variable>
  <xsl:variable name="Int"><xsl:value-of select="sum(Ability[@name='Intelligence']/*)"/></xsl:variable>
  <xsl:variable name="Wis"><xsl:value-of select="sum(Ability[@name='Wisdom']/*)"/></xsl:variable>
  <xsl:variable name="Cha"><xsl:value-of select="sum(Ability[@name='Charisma']/*)"/></xsl:variable>
  
  <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="contains(@count,'-') or contains(@count,'d')"> (<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="Treasures">
  <strong>Treasure: </strong>
  <xsl:choose>
  <xsl:when test="Treasure">
    <xsl:for-each select="Treasure">
      <xsl:if test="position()!=1">, </xsl:if>
      <xsl:choose>
        <xsl:when test="@multiplier=2">double </xsl:when>
        <xsl:when test="@multiplier=3">triple </xsl:when>
        <xsl:when test="@multiplier=4">quadruple </xsl:when>
        <xsl:otherwise>standard </xsl:otherwise>
      </xsl:choose>
      <xsl:value-of select="."/>
    </xsl:for-each>
  </xsl:when>
  <xsl:otherwise>
    <xsl:choose>
    <xsl:when test="@multiplier=2">double standard</xsl:when>
    <xsl:when test="@multiplier=3">triple standard</xsl:when>
    <xsl:when test="@multiplier=4">quadruple standard</xsl:when>
    <xsl:otherwise>standard</xsl:otherwise>
    </xsl:choose>
  </xsl:otherwise>
  </xsl:choose>
  <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:choose>
  <xsl:when test="ancestor-or-self::StatBlock/Levels/Level[@class!='monster']">By character class</xsl:when>
  <xsl:otherwise>
    <xsl:for-each select="Advancement"><xsl:if test="position()!=1">; </xsl:if><xsl:value-of select="@lowrange"/>-<xsl:value-of select="@highrange"/> HD (<xsl:value-of select="@size"/>)</xsl:for-each>
  </xsl:otherwise>
  </xsl:choose>
  <br/>
</xsl:template>

<xsl:template name="LevelAdjustment">
  <xsl:variable name="la" select="ancestor-or-self::StatBlock/Levels/@LevelAdjustment"/>
  <strong>Level Adjustment: </strong>
    <xsl:if test="$la = 0">-</xsl:if>
    <xsl:if test="$la != 0"><xsl:value-of select="$la"/></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>

<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:for-each select="ancestor-or-self::StatBlock/Levels/Level[1]">
    <xsl:call-template name="perBAB"/>
  </xsl:for-each>
</xsl:template>

<xsl:template name="perBAB">
  <xsl:variable name="rest">
    <xsl:choose>
      <xsl:when test="following-sibling::Level[1]">
        <xsl:for-each select="following-sibling::Level[1]">
          <xsl:call-template name="perBAB"/>
        </xsl:for-each>
      </xsl:when>
      <xsl:otherwise>0</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="bab"><xsl:call-template name="thisBAB"/></xsl:variable>
  <xsl:value-of select="$bab + $rest"/>
</xsl:template>

<xsl:template name="thisBAB">
    <xsl:variable name="type"><xsl:call-template name="Type"/></xsl:variable>
    <xsl:choose>
      <xsl:when test="@class='Barbarian'"><xsl:value-of select="."/></xsl:when>
      <xsl:when test="@class='Bard'"><xsl:value-of select="floor((. * 3) div 4)"/></xsl:when>
      <xsl:when test="@class='Cleric'"><xsl:value-of select="floor((. * 3) div 4)"/></xsl:when>
      <xsl:when test="@class='Druid'"><xsl:value-of select="floor((. * 3) div 4)"/></xsl:when>
      <xsl:when test="@class='Fighter'"><xsl:value-of select="."/></xsl:when>
      <xsl:when test="@class='Monk'"><xsl:value-of select="floor((. * 3) div 4)"/></xsl:when>
      <xsl:when test="@class='Paladin'"><xsl:value-of select="."/></xsl:when>
      <xsl:when test="@class='Ranger'"><xsl:value-of select="."/></xsl:when>
      <xsl:when test="@class='Rogue'"><xsl:value-of select="floor((. * 3) div 4)"/></xsl:when>
      <xsl:when test="@class='Sorcerer'"><xsl:value-of select="floor(. div 2)"/></xsl:when>
      <xsl:when test="@class='Wizard'"><xsl:value-of select="floor(. div 2)"/></xsl:when>
      <xsl:when test="@class='monster'">
        <xsl:choose>
          <xsl:when test="$type='Aberration'"><xsl:value-of select="floor((. * 3) div 4)"/></xsl:when>
          <xsl:when test="$type='Animal'"><xsl:value-of select="floor((. * 3) div 4)"/></xsl:when>
          <xsl:when test="$type='Construct'"><xsl:value-of select="floor((. * 3) div 4)"/></xsl:when>
          <xsl:when test="$type='Dragon'"><xsl:value-of select="."/></xsl:when>
          <xsl:when test="$type='Elemental'"><xsl:value-of select="floor((. * 3) div 4)"/></xsl:when>
          <xsl:when test="$type='Fey'"><xsl:value-of select="floor(. div 2)"/></xsl:when>
          <xsl:when test="$type='Giant'"><xsl:value-of select="floor((. * 3) div 4)"/></xsl:when>
          <xsl:when test="$type='Humanoid'"><xsl:value-of select="floor((. * 3) div 4)"/></xsl:when>
          <xsl:when test="$type='Magical beast'"><xsl:value-of select="."/></xsl:when>
          <xsl:when test="$type='Monstrous humanoid'"><xsl:value-of select="."/></xsl:when>
          <xsl:when test="$type='Ooze'"><xsl:value-of select="floor((. * 3) div 4)"/></xsl:when>
          <xsl:when test="$type='Outsider'"><xsl:value-of select="."/></xsl:when>
          <xsl:when test="$type='Plant'"><xsl:value-of select="floor((. * 3) div 4)"/></xsl:when>
          <xsl:when test="$type='Undead'"><xsl:value-of select="floor(. div 2)"/></xsl:when>
          <xsl:when test="$type='Vermin'"><xsl:value-of select="floor((. * 3) div 4)"/></xsl:when>
          <xsl:otherwise><xsl:value-of select="floor((. * 3) div 4)"/></xsl:otherwise>
        </xsl:choose>
      </xsl:when>
    </xsl:choose>
</xsl:template>

<xsl:template name="strbonus"><xsl:value-of select="floor((sum(ancestor-or-self::StatBlock/Abilities/Ability[@name='Strength']/*) - 10) div 2)"/></xsl:template>

<xsl:template name="dexbonus"><xsl:value-of select="floor((sum(ancestor-or-self::StatBlock/Abilities/Ability[@name='Dexterity']/*) - 10) div 2)"/></xsl:template>

<xsl:template name="conbonus"><xsl:value-of select="floor((sum(ancestor-or-self::StatBlock/Abilities/Ability[@name='Constitution']/*) - 10) div 2)"/></xsl:template>

<xsl:template name="intbonus"><xsl:value-of select="floor((sum(ancestor-or-self::StatBlock/Abilities/Ability[@name='Intelligence']/*) - 10) div 2)"/></xsl:template>

<xsl:template name="wisbonus"><xsl:value-of select="floor((sum(ancestor-or-self::StatBlock/Abilities/Ability[@name='Wisdom']/*) - 10) div 2)"/></xsl:template>

<xsl:template name="chabonus"><xsl:value-of select="floor((sum(ancestor-or-self::StatBlock/Abilities/Ability[@name='Charisma']/*) - 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:call-template name="sizebonus"/></xsl:variable>
<xsl:value-of select="$bab + $strbonus + $sizeattack"/>
</xsl:template>

<xsl:template name="RangedAttack">
<xsl:variable name="bab"><xsl:call-template name="BAB"/></xsl:variable>
<xsl:variable name="dexbonus"><xsl:call-template name="dexbonus"/></xsl:variable>
<xsl:variable name="sizeattack"><xsl:call-template name="sizebonus"/></xsl:variable>
<xsl:value-of select="$bab + $dexbonus + $sizeattack"/>
</xsl:template>

<xsl:template name="AttackDamage">
  <xsl:choose>
  <xsl:when test="@damage"><xsl:value-of select="@damage"/></xsl:when>
  <xsl:when test="@weapon='natural'"><xsl:call-template name="NaturalDamage"/></xsl:when>
  <xsl:otherwise>unknown</xsl:otherwise>
  </xsl:choose>
  <xsl:if test="@extra"> plus <xsl:value-of select="@extra"/></xsl:if>
</xsl:template>

<xsl:template name="NaturalDamage">
  <xsl:variable name="size"><xsl:call-template name="Size"/></xsl:variable>
  <xsl:variable name="basedamage">
  <xsl:choose>
    <xsl:when test="$size='Fine'">0</xsl:when>
    <xsl:when test="$size='Diminuitive'">0</xsl:when>
    <xsl:when test="$size='Tiny'">1</xsl:when>
    <xsl:when test="$size='Small'">1d2</xsl:when>
    <xsl:when test="$size='Medium'">1d3</xsl:when>
    <xsl:when test="$size='Large'">1d4</xsl:when>
    <xsl:when test="$size='Huge'">1d6</xsl:when>
    <xsl:when test="$size='Gargantuam'">1d8</xsl:when>
    <xsl:when test="$size='Colossal'">2d6</xsl:when>
    <xsl:otherwise>0</xsl:otherwise>
  </xsl:choose>
  </xsl:variable>
  <xsl:value-of select="$basedamage"/>+<xsl:call-template name="strbonus"/>
</xsl:template>

<xsl:template name="PoorSave"><xsl:value-of select="floor(. div 3)"/></xsl:template>

<xsl:template name="GoodSave"><xsl:value-of select="floor(. div 2) + 2"/></xsl:template>

<xsl:template name="SkillPoints">
  <xsl:variable name="ability">
  <xsl:if test="@name='Climb' or @name='Jump' or @name='Swim'"><xsl:call-template name="strbonus"/></xsl:if>
  <xsl:if test="@name='Balance' or @name='Escape Artist' or @name='Hide' or @name='Move Silently' or @name='Open Lock' or @name='Ride' or @name='Sleight of Hand' or @name='Tumble' or @name='Use Rope'"><xsl:call-template name="dexbonus"/></xsl:if>
  <xsl:if test="@name='Concentration'"><xsl:call-template name="conbonus"/></xsl:if>
  <xsl:if test="@name='Appraise' or contains(@name,'Craft') or @name='Decipher Script' or @name='Disable Device' or @name='Forgery' or contains(@name,'Knowledge') or @name='Search' or @name='Spellcraft'"><xsl:call-template name="intbonus"/></xsl:if>
  <xsl:if test="@name='Heal' or @name='Listen' or contains(@name,'Profession') or @name='Sense Motive' or @name='Spot' or @name='Survival'"><xsl:call-template name="wisbonus"/></xsl:if>
  <xsl:if test="@name='Bluff' or @name='Diplomacy' or @name='Disguise' or @name='Gather Information' or @name='Handle Animal' or @name='Intimidate' or @name='erform' or @name='Use Magic Device'"><xsl:call-template name="chabonus"/></xsl:if>
  </xsl:variable>
  <xsl:variable name="extra">
    <xsl:call-template name="featskillpoints"/>
  </xsl:variable>
  <xsl:variable name="synergy">
    <xsl:call-template name="synergybonus"/>
  </xsl:variable>
  <xsl:value-of select="$ability + @ranks + $extra + $synergy"/>
</xsl:template>

<xsl:template name="featskillpoints">
<xsl:variable name="skill" select="@name"/>
  <xsl:for-each select="ancestor-or-self::StatBlock/Feats/Feat[1]">
    <xsl:call-template name="perskillpoints">
    <xsl:with-param name="skill" select="$skill"/>
    </xsl:call-template>
  </xsl:for-each>
</xsl:template>

<xsl:template name="perskillpoints">
<xsl:param name="skill"/>
  <xsl:variable name="rest">
    <xsl:choose>
      <xsl:when test="following-sibling::Feat[1]">
        <xsl:for-each select="following-sibling::Feat[1]">
          <xsl:call-template name="perskillpoints">
          <xsl:with-param name="skill" select="$skill"/>
          </xsl:call-template>
        </xsl:for-each>
      </xsl:when>
      <xsl:otherwise>0</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="skillpoints"><xsl:call-template name="thisskill"><xsl:with-param name="skill" select="$skill"/></xsl:call-template></xsl:variable>
  <xsl:value-of select="$skillpoints + $rest"/>
</xsl:template>

<xsl:template name="thisskill">
<xsl:param name="skill"/>
  <xsl:choose>
    <xsl:when test="@name='Acrobatic' and ($skill='Jump' or $skill='Tumble')">2</xsl:when>
    <xsl:when test="@name='Agile' and ($skill='Balance' or $skill='Escape Artist')">2</xsl:when>
    <xsl:when test="@name='Alertness' and ($skill='Listen' or $skill='Spot')">2</xsl:when>
    <xsl:when test="@name='Animal Affinity' and ($skill='Handle Animal' or $skill='Ride')">2</xsl:when>
    <xsl:when test="@name='Athletic' and ($skill='Climb' or $skill='Swim')">2</xsl:when>
    <xsl:when test="@name='Deceitful' and ($skill='Disguise' or $skill='Forgery')">2</xsl:when>
    <xsl:when test="@name='Deft Hands' and ($skill='Sleight of Hand' or $skill='Use Rope')">2</xsl:when>
    <xsl:when test="@name='Diligent' and ($skill='Appraise' or $skill='Decipher Script')">2</xsl:when>
    <xsl:when test="@name='Investigator' and ($skill='Gather Information' or $skill='Search')">2</xsl:when>
    <xsl:when test="@name='Magical Aptitude' and ($skill='Spellcraft' or $skill='Use Magic Device')">2</xsl:when>
    <xsl:when test="@name='Negotiator' and ($skill='Diplomacy' or $skill='Sense Motive')">2</xsl:when>
    <xsl:when test="@name='Nimble Fingers' and ($skill='Disable Device' or $skill='Open Lock')">2</xsl:when>
    <xsl:when test="@name='Persuasive' and ($skill='Bluff' or $skill='Intimidate')">2</xsl:when>
    <xsl:when test="@name='Self-Sufficient' and ($skill='Heal' or $skill='Survival')">2</xsl:when>
    <xsl:when test="@name='Stealthy' and ($skill='Hide' or $skill='Move Silently')">2</xsl:when>
    <xsl:otherwise>0</xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="synergybonus">
  <xsl:choose>
    <!-- Appraise -->
    <xsl:when test="@name='Balance' and parent::Skills/Skill[@name='Tumble']/@ranks >= 5">2</xsl:when>
    <!-- Climb -->
    <!-- Decipher Script -->
    <xsl:when test="@name='Diplomacy' and ((parent::Skills/Skill[@name='Bluff']/@ranks >= 5) or (parent::Skills/Skill[@name='Sense Motive']/@ranks >= 5) or (parent::Skills/Skill[@name='Knowledge (nobility and royalty)']/@ranks >= 5))">2</xsl:when> <!-- not additive -->
    <!-- Disguise -->
    <!-- Escape Artist -->
    
    <xsl:when test="@name='Gather Information' and parent::Skills/Skill[@name='Knowledge (local)']/@ranks >= 5">2</xsl:when>
    <xsl:when test="@name='Intimidate' and parent::Skills/Skill[@name='Bluff']/@ranks > 5">2</xsl:when>
    <xsl:when test="@name='Jump' and parent::Skills/Skill[@name='Tumble']/@ranks > 5">2</xsl:when>
    <xsl:when test="@name='Knowledge (nature)' and parent::Skills/Skill[@name='Survival']/@ranks > 5">2</xsl:when>
    <xsl:when test="@name='Ride' and parent::Skills/Skill[@name='Handle Animal']/@ranks > 5">2</xsl:when>
    <!-- Search -->
    <xsl:when test="@name='Sleight of Hand' and parent::Skills/Skill[@name='Bluff']/@ranks > 5">2</xsl:when>
    <xsl:when test="@name='Spellcraft' and parent::Skills/Skill[@name='Knowledge (arcana)']/@ranks > 5">2</xsl:when>
    <!-- Survival -->
    <xsl:when test="@name='Tumble' and parent::Skills/Skill[@name='Jump']/@ranks > 5">2</xsl:when>
    <!-- Use Magic Device -->
    <!-- Use Rope -->
    <xsl:otherwise>0</xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- context-based function == //StatBlock/Levels -->
<xsl:template name="TotalAverageHitpoints">
  <xsl:for-each select="Level[1]">
    <xsl:call-template name="AverageHitpoints"/>
  </xsl:for-each>
</xsl:template>

<!-- context-based function == //StatBlock/Levels/Level -->
<xsl:template name="AverageHitpoints">
  <xsl:variable name="rest">
    <xsl:choose>
      <xsl:when test="following-sibling::Level[1]">
        <xsl:for-each select="following-sibling::Level[1]">
          <xsl:call-template name="AverageHitpoints"/>
        </xsl:for-each>
      </xsl:when>
      <xsl:otherwise>0</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="hp"><xsl:call-template name="thisHP"/></xsl:variable>
  <xsl:value-of select="$hp + $rest"/>
</xsl:template>

<!-- context-based function == //StatBlock/Levels/Level -->
<xsl:template name="thisHP">
 <xsl:variable name="die"><xsl:call-template name="finddie"/></xsl:variable>
 <xsl:variable name="conbonus"><xsl:call-template name="conbonus"/></xsl:variable>
  <xsl:value-of select="floor(((($die + 1) div 2) + $conbonus) * .)"/>
</xsl:template>

<!-- context-based function == //StatBlock/Levels/Level -->
<xsl:template name="finddie">
  <xsl:variable name="type"><xsl:call-template name="Type"/></xsl:variable>
  <xsl:choose>
    <xsl:when test="@class='Barbarian'">12</xsl:when>
    <xsl:when test="@class='Bard'">6</xsl:when>
    <xsl:when test="@class='Cleric'">8</xsl:when>
    <xsl:when test="@class='Druid'">8</xsl:when>
    <xsl:when test="@class='Fighter'">10</xsl:when>
    <xsl:when test="@class='Monk'">8</xsl:when>
    <xsl:when test="@class='Paladin'">10</xsl:when>
    <xsl:when test="@class='Ranger'">10</xsl:when>
    <xsl:when test="@class='Rogue'">6</xsl:when>
    <xsl:when test="@class='Sorcerer'">4</xsl:when>
    <xsl:when test="@class='Wizard'">4</xsl:when>
    <xsl:when test="@class='monster'">
      <xsl:choose>
        <xsl:when test="$type='Aberration'">8</xsl:when>
        <xsl:when test="$type='Animal'">8</xsl:when>
        <xsl:when test="$type='Construct'">10</xsl:when>
        <xsl:when test="$type='Dragon'">12</xsl:when>
        <xsl:when test="$type='Elemental'">8</xsl:when>
        <xsl:when test="$type='Fey'">6</xsl:when>
        <xsl:when test="$type='Giant'">8</xsl:when>
        <xsl:when test="$type='Humanoid'">8</xsl:when>
        <xsl:when test="$type='Magical beast'">10</xsl:when>
        <xsl:when test="$type='Monstrous humanoid'">8</xsl:when>
        <xsl:when test="$type='Ooze'">10</xsl:when>
        <xsl:when test="$type='Outsider'">8</xsl:when>
        <xsl:when test="$type='Plant'">8</xsl:when>
        <xsl:when test="$type='Undead'">12</xsl:when>
        <xsl:when test="$type='Vermin'">8</xsl:when>
        <xsl:otherwise>1</xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:otherwise>1</xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="sizebonus">
  <xsl:variable name="size"><xsl:call-template name="Size"/></xsl:variable>
  <xsl:choose>
    <xsl:when test="$size='Fine'">8</xsl:when>
    <xsl:when test="$size='Diminuitive'">4</xsl:when>
    <xsl:when test="$size='Tiny'">2</xsl:when>
    <xsl:when test="$size='Small'">1</xsl:when>
    <xsl:when test="$size='Medium'">0</xsl:when>
    <xsl:when test="$size='Large'">-1</xsl:when>
    <xsl:when test="$size='Huge'">-2</xsl:when>
    <xsl:when test="$size='Gargantuam'">-4</xsl:when>
    <xsl:when test="$size='Colossal'">-8</xsl:when>
    <xsl:otherwise>0</xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="grapplebonus">
  <xsl:variable name="size"><xsl:call-template name="Size"/></xsl:variable>
  <xsl:choose>
    <xsl:when test="$size='Fine'">-16</xsl:when>
    <xsl:when test="$size='Diminuitive'">-12</xsl:when>
    <xsl:when test="$size='Tiny'">-8</xsl:when>
    <xsl:when test="$size='Small'">-4</xsl:when>
    <xsl:when test="$size='Medium'">0</xsl:when>
    <xsl:when test="$size='Large'">4</xsl:when>
    <xsl:when test="$size='Huge'">8</xsl:when>
    <xsl:when test="$size='Gargantuam'">12</xsl:when>
    <xsl:when test="$size='Colossal'">16</xsl:when>
    <xsl:otherwise>0</xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="ImplicitQualities">
<xsl:param name="type"/>
  <xsl:choose>
    <xsl:when test="$type='Aberration'">darkvision 60 ft.</xsl:when>
    <xsl:when test="$type='Animal'">low-light vision</xsl:when>
    <xsl:when test="$type='Construct'">construct traits</xsl:when>
    <xsl:when test="$type='Dragon'">darkvision 60 ft., low-light vision</xsl:when>
    <xsl:when test="$type='Elemental'">darkvision 60 ft., elemental traits</xsl:when>
    <xsl:when test="$type='Fey'">low-light vision</xsl:when>
    <xsl:when test="$type='Giant'">low-light vision</xsl:when>
    <xsl:when test="$type='Humanoid'"></xsl:when>
    <xsl:when test="$type='Magical beast'">darkvision 60 ft., low-light vision</xsl:when>
    <xsl:when test="$type='Monstrous humanoid'">darkvision 60 ft.</xsl:when>
    <xsl:when test="$type='Ooze'">blindsight 60 ft., ooze traits</xsl:when>
    <xsl:when test="$type='Outsider'">darkvision 60 ft.</xsl:when>
    <xsl:when test="$type='Plant'">low-light vision, plant traits</xsl:when>
    <xsl:when test="$type='Undead'">darkvision 60 ft., undead traits</xsl:when>
    <xsl:when test="$type='Vermin'">darkvision 60 ft., vermin traits</xsl:when>
    <xsl:when test="$type='Aquatic'">aquatic subtype</xsl:when>
    <xsl:when test="$type='Baatezu'">immunity to fire and poison, resistance to acid 10 and cold 10, see in darkness, telepathy 100 ft.</xsl:when>
  </xsl:choose>
</xsl:template>

<xsl:template name="ImplicitAttacks">
<xsl:param name="type"/>
  <xsl:choose>
    <xsl:when test="$type='Baatezu'">summon baatezu</xsl:when>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>

