﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
	version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:am="http://www.ani-mayhem.com/"
	xmlns:db="http://www.ani-mayhem.com/database"
	exclude-result-prefixes="am db">
	<xsl:output method="xml" indent="yes" encoding="utf-8" omit-xml-declaration="yes"/>
	<xsl:variable name="cgi">http://www.ani-mayhem.com/cardcatalog.aspx?xmlid=</xsl:variable>
	<xsl:template match="/">
		<xsl:apply-templates select="am:ani-mayhem"/>
	</xsl:template>
	<xsl:template match="am:ani-mayhem">
		<xsl:apply-templates select="am:card[1]"/>
	</xsl:template>
	<xsl:template match="am:card">
		<tr id="{@xml:id}">
			<xsl:apply-templates/>
		</tr>
	</xsl:template>
	<xsl:template match="db:Name">
		<td>
			<a href="{$cgi}{../@xml:id}">
				<xsl:value-of select="substring(., 1, string-length() - string-length(substring-after(string(current()[contains(text(),' / ')]), substring-before(current()[contains(text(),' / ')], ' / '))))"/>
			</a>
			<xsl:if test="contains(../db:Type, 'Combat')">
				<xsl:text> / </xsl:text>
				<a href="{$cgi}{//am:card[substring-before(db:Name, ' / ') = substring-after(current(), ' / ')]/@xml:id}">
					<xsl:value-of select="substring-after(., ' / ')"/>
				</a>
			</xsl:if>
		</td>
	</xsl:template>
	<xsl:template match="db:Type | db:Rarity">
		<td class="{normalize-space(translate(., 'CDEFGHILMPRU ', 'cdefghilmpru'))}">
			<xsl:apply-templates/>
		</td>
	</xsl:template>
	<xsl:template match="db:Series">
		<td>
			<xsl:apply-templates/>
			<xsl:if test="contains(../db:Type, 'Combat')">
				<xsl:text> / </xsl:text>
				<xsl:value-of select="//am:card[substring-before(db:Name, ' / ') = substring-after(current()/../db:Name, ' / ')]/db:Series"/>
			</xsl:if>
		</td>
	</xsl:template>
	<xsl:template match="db:Set">
		<td>
			<xsl:apply-templates/>
		</td>
	</xsl:template>
</xsl:stylesheet>