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

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

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

<xsl:template match="Monster">
  <Monster>
  <Name><xsl:value-of select="Name"/></Name>
  <xsl:apply-templates select="StatBlock"/>
  <xsl:apply-templates select="Description"/>
  </Monster>
</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="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:element 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>
    <xsl:attribute name="Size"><xsl:value-of select="$size"/></xsl:attribute>
    <xsl:attribute name="Type"><xsl:value-of select="$type"/></xsl:attribute>
    <xsl:attribute name="Subtypes"><xsl:value-of select="$subtypes"/></xsl:attribute>
    <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>
  </xsl:element>
</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']"/>
    <HitDice><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)</HitDice>
</xsl:template>

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

<xsl:template match="Speeds">
    <Speed><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>
</Speed>
</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"/>
    <ArmorClass><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"/></ArmorClass>
</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"/>
  <BaseAttackAndGrapple>+<xsl:value-of select="$base"/>/+<xsl:value-of select="$grapple"/></BaseAttackAndGrapple>
</xsl:template>

<xsl:template match="Attacks">
  <Attack>
  <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></Attack>
</xsl:template>

<xsl:template match="FullAttacks">
  <FullAttack>
  <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/Attack[@name=$attack]"><xsl:call-template name="AttackDamage"/></xsl:for-each>)</xsl:for-each>
  </xsl:for-each>
  </FullAttack>
</xsl:template>

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

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

<xsl:template match="SpecialQualities">
  <SpecialQualities>
  <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 and $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>
  </SpecialQualities>
</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>

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

<xsl:template match="Abilities">
  <xsl:element name="Abilities">
    <xsl:attribute name="Str"><xsl:value-of select="@Str"/></xsl:attribute>
    <xsl:attribute name="Dex"><xsl:value-of select="@Dex"/></xsl:attribute>
    <xsl:attribute name="Con"><xsl:value-of select="@Con"/></xsl:attribute>
    <xsl:attribute name="Int"><xsl:value-of select="@Int"/></xsl:attribute>
    <xsl:attribute name="Wis"><xsl:value-of select="@Wis"/></xsl:attribute>
    <xsl:attribute name="Cha"><xsl:value-of select="@Cha"/></xsl:attribute>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"/>
  </xsl:element>
</xsl:template>

<xsl:template match="Skills">
  <Skills>
  <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>
  </Skills>
</xsl:template>

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

<xsl:template match="Organizations">
  <Organization>
  <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>
  </Organization>
</xsl:template>

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

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

<xsl:template match="Alignment">
  <Alignment>
  <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"/>
  </Alignment>
</xsl:template>

<xsl:template match="Advancements">
  <Advancement>
    <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>
  </Advancement>
</xsl:template>

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

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

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

<xsl:template match="Combat">
  <Combat><xsl:text>
</xsl:text>
  <xsl:value-of select="Text"/>
  <xsl:for-each select="SpecialAbility">
<xsl:text>
</xsl:text>
  <xsl:element name="SpecialAbility">
  <xsl:attribute name="Name"><xsl:value-of select="@Name"/></xsl:attribute>
  <xsl:attribute name="Type"><xsl:value-of select="@Type"/></xsl:attribute>
  <xsl:value-of select="@Name"/> (<xsl:value-of select="@Type"/>): <xsl:value-of select="Text"/>
  </xsl:element>
  </xsl:for-each>

  <xsl:if test="RacialSkill">
  Skills: <xsl:for-each select="RacialSkill">
    <xsl:value-of select="."/>
  </xsl:for-each><xsl:text>
</xsl:text>
  </xsl:if>
  </Combat>
</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>

