<?xml version="1.0"?>
<!DOCTYPE document [
	<!ENTITY bull "&#x2022;">
	<!ENTITY mdash "&#x2014;">
	<!ENTITY nbsp "&#x00A0;">
]>
<xsl:stylesheet
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xhtml="http://www.w3.org/1999/xhtml"
	xmlns:xlink="http://www.w3.org/1999/xlink"
	xmlns:svg="http://www.w3.org/2000/svg"
	version="1.0">

	<!--
		Author: james.ojaste@ec.gc.ca
		License: Creative Commons Attribution-ShareAlike License
					http://creativecommons.org/licenses/by-sa/2.0/
	-->

	<xsl:output method="html"/>

	<xsl:template match="/character[@system='srd']">
		<html>
		<head>
			<title><xsl:value-of select="name"/></title>
			<style type="text/css">
				body { font-size: 9pt; padding: 0; }
				table { border: 1px solid black; }
				table.container { border: none; }
				tr { vertical-align: top; }
				td, th { padding: 0 3px 0 3px; }
				th { border-bottom: 1px solid black; }
				#abilities .description { font-size: 7pt; }
				#abilities tr td { border-bottom: 1px solid gray; }

				#weapon ability { padding-right: 1ex; }

				.page { page-break-before: always; }
			</style>
		</head>
		<body>
			<h3><xsl:value-of select="name"/></h3>

			<table class="container" border="0" cellspacing="0" cellpadding="0" width="100%">
			<tr>
				<td width="35%">
					<xsl:apply-templates select="skills"/>
				</td>
				<td width="25%">
					<xsl:apply-templates select="stats"/>
				</td>
				<td width="10%">
					<table id="saves" border="0" cellspacing="0" cellpadding="0" width="100%">
						<tr><th colspan="2">Saves</th></tr>
						<xsl:call-template name="saves"/>
					</table>
				</td>
				<td width="20%">
					<table id="misc" border="0" cellspacing="0" cellpadding="0" width="100%">
						<tr><th colspan="2">Misc</th></tr>
						<tr><td width="30%">Level</td><td><xsl:value-of select="count(levels/level)"/></td></tr>
						<tr><td>Experience</td><td><xsl:value-of select="sum(log/award/@xp)"/></td></tr>
						<tr><td>Alignment</td><td><xsl:value-of select="alignment"/></td></tr>
						<tr><td>BAB</td><td><xsl:value-of select="sum (levels/level/@bab)"/></td></tr>
						<tr><td>HP</td><td><xsl:call-template name="hp"/></td></tr>
					</table>
				</td>
			</tr>
			<tr>
				<td colspan="4">
					<xsl:apply-templates select="equipment/weapons"/>
				</td>
			</tr>
			</table>

			<div class="page">
				<xsl:apply-templates select="equipment"/>
			</div>

			<div class="page">
				<xsl:apply-templates select="levels"/>
			</div>
		</body>
		</html>
	</xsl:template>


	<xsl:template match="equipment">
		<table id="worn" border="0" cellspacing="0" cellpadding="0" width="100%">
			<tr><th>W?</th><th>T</th><th>#</th><th width="20%">Items</th><th>Notes</th></tr>
			<xsl:apply-templates select="worn/item|carried/item">
				<xsl:sort select="name(..)" order="descending"/>
				<xsl:sort select="@type"/>
				<xsl:sort select="text()"/>
			</xsl:apply-templates>
		</table>
	</xsl:template>

	<xsl:template match="equipment/weapons">
		<table id="weapons" border="0" cellspacing="0" cellpadding="0">
			<tr><th>Weapon</th><th>H</th><th>Atk</th><th>Dmg</th><th>Crit</th><th>Abilities</th></tr>
			<xsl:apply-templates select="*">
				<xsl:sort select="@bonus" order="descending"/>
			</xsl:apply-templates>
		</table>
	</xsl:template>

	<xsl:template match="equipment/weapons/weapon">
		<xsl:variable name="value">
			<xsl:call-template name="statValue">
				<xsl:with-param name="name" select="'str'"/>
			</xsl:call-template>
		</xsl:variable>

		<xsl:variable name="strBonus">
			<xsl:call-template name="statMod">
				<xsl:with-param name="stat" select="$value"/>
			</xsl:call-template>
		</xsl:variable>
		<xsl:variable name="hands">
			<xsl:choose>
				<xsl:when test="@h!=''"><xsl:value-of select="@h"/></xsl:when>
				<xsl:when test="@type='greatsword'">2</xsl:when>
				<xsl:otherwise>1</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<tr>
			<td>
				<xsl:if test="@bonus != ''">
					+<xsl:value-of select="@bonus"/>&nbsp;
				</xsl:if>
				<xsl:value-of select="@type"/></td>
			<td><xsl:value-of select="$hands"/>h</td>
			<td><xsl:value-of select="sum(/character/levels/level/@bab) + $strBonus + sum(@bonus)"/></td>
			<td>
				<xsl:value-of select="sum(@bonus) + floor ($strBonus * ($hands div 2 + 0.5))"/>
				+
				<xsl:choose>
					<xsl:when test="@dmg!=''"><xsl:value-of select="@dmg"/></xsl:when>
					<xsl:when test="@type='greatsword'">2d6</xsl:when>
					<xsl:when test="@type='dagger'">1d4</xsl:when>
				</xsl:choose>
			</td>
			<td><xsl:value-of select="@crit"/></td>
			<td><xsl:apply-templates select="ability"/></td>
		</tr>
	</xsl:template>

	<xsl:template match="equipment/weapons/weapon/ability">
		<xsl:if test="position()&gt;1"><xsl:text>, </xsl:text></xsl:if>
		<xsl:value-of select="text()"/>
		<xsl:copy-of select="*"/>
	</xsl:template>


	<xsl:template match="equipment/worn/item|equipment/carried/item">
		<tr>
			<td><xsl:if test="name(..)='worn'">*</xsl:if></td>
			<td><xsl:value-of select="@type"/></td>
			<td><xsl:if test="@q!=''"><xsl:value-of select="@q"/></xsl:if></td>
			<td><xsl:value-of select="text()"/></td>
			<td><xsl:copy-of select="description"/></td>
		</tr>
	</xsl:template>


	<xsl:template name="hp">
		<xsl:variable name="conValue">
			<xsl:call-template name="statValue">
				<xsl:with-param name="name" select="'con'"/>
			</xsl:call-template>
		</xsl:variable>

		<xsl:variable name="conBonus">
			<xsl:call-template name="statMod">
				<xsl:with-param name="stat" select="$conValue"/>
			</xsl:call-template>
		</xsl:variable>
		<xsl:value-of select="sum(levels/level/@hp) + count(levels/level) * $conBonus"/><br/>
	</xsl:template>


	<xsl:template match="levels">
		<table id="abilities" border="0" cellspacing="0" cellpadding="0">
			<tr><th width="20%">Ability</th><th>Description</th></tr>
			<xsl:apply-templates select="level/ability">
				<xsl:sort name="text()"/>
			</xsl:apply-templates>
		</table>
	</xsl:template>

	<xsl:template match="levels/level/ability">
		<tr>
			<td><xsl:value-of select="text()"/></td>
			<td class="description"><xsl:copy-of select="description"/></td>
		</tr>
	</xsl:template>


	<xsl:template name="saves">
		<xsl:variable name="conBonus">
			<xsl:call-template name="statMod">
				<xsl:with-param name="name" select="'con'"/>
			</xsl:call-template>
		</xsl:variable>
		<xsl:variable name="dexBonus">
			<xsl:call-template name="statMod">
				<xsl:with-param name="name" select="'dex'"/>
			</xsl:call-template>
		</xsl:variable>
		<xsl:variable name="wisBonus">
			<xsl:call-template name="statMod">
				<xsl:with-param name="name" select="'wis'"/>
			</xsl:call-template>
		</xsl:variable>
		<tr><td width="30%">Fort</td><td><xsl:value-of select="sum(levels/level/@fort) + $conBonus"/></td></tr>
		<tr><td>Ref</td><td><xsl:value-of select="sum(levels/level/@ref) + $dexBonus"/></td></tr>
		<tr><td>Will</td><td><xsl:value-of select="sum(levels/level/@will) + $wisBonus"/></td></tr>
		<tr><td>AC</td><td><xsl:value-of select="10 + $dexBonus + sum(race/ac[@source='natural']) + sum(equipment/worn/item/@ac)"/></td></tr>
		<tr><td>Touch</td><td><xsl:value-of select="10 + $dexBonus + sum(race/ac[@source!='natural']) + sum(equipment/worn/item/@touch)"/></td></tr>
	</xsl:template>


	<xsl:template match="skills">
		<table id="skills" border="0" cellspacing="0" cellpadding="0" width="100%">
			<tr><th>Bonus</th><th>Ranks</th><th>Skill</th><th>Stat</th></tr>
			<xsl:apply-templates select="*"/>
		</table>
	</xsl:template>

	<xsl:template match="skills/skill">
		<tr>
			<td><xsl:call-template name="skillBonus"/></td>
			<td><xsl:value-of select="@ranks"/></td>
			<td><xsl:value-of select="."/></td>
			<td><xsl:value-of select="@stat"/></td>
		</tr>
	</xsl:template>

	<xsl:template name="skillBonus">
		<xsl:variable name="stat">
			<xsl:value-of select="@stat"/>
		</xsl:variable>

		<xsl:variable name="statBonus">
			<xsl:call-template name="statMod">
				<xsl:with-param name="name" select="$stat"/>
			</xsl:call-template>
		</xsl:variable>

		<xsl:value-of select="$statBonus + @ranks"/>
	</xsl:template>


	<xsl:template match="stats">
		<table id="stats" border="0" cellspacing="0" cellpadding="0" width="100%">
			<tr><th>Stat</th><th>Score</th><th>Mod</th><th>Temp</th><th>Mod</th></tr>
			<xsl:apply-templates select="*"/>
		</table>
	</xsl:template>

	<xsl:template match="stats/*">
		<xsl:variable name="name">
			<xsl:value-of select="name()"/>
		</xsl:variable>

		<xsl:variable name="value">
			<xsl:call-template name="statValue">
				<xsl:with-param name="name" select="$name"/>
			</xsl:call-template>
		</xsl:variable>

		<tr><td><xsl:value-of select="$name"/></td>
			<td><xsl:value-of select="$value"/></td>
			<td><xsl:call-template name="statMod">
					<xsl:with-param name="name" select="$name"/>
				</xsl:call-template></td>
			<td>&#160;</td><td>&#160;</td></tr>
	</xsl:template>

	<xsl:template name="statValue">
		<xsl:param name="name" select="''"/>
		<xsl:value-of select="/character/stats/*[name()=$name] + sum(/character/race/*[name()=$name]) + sum(level/*[name()=$name])"/>
	</xsl:template>

	<xsl:template name="statMod">
		<xsl:param name="name" select="''"/>

		<xsl:variable name="value">
			<xsl:call-template name="statValue">
				<xsl:with-param name="name" select="$name"/>
			</xsl:call-template>
		</xsl:variable>

		<xsl:value-of select="floor (($value div 2) - 5)"/>
	</xsl:template>

</xsl:stylesheet>

