
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
							  xmlns:proaiml="http://www.proai.net/schemas/proaiml/v1">

	<xsl:output method="xml" indent="yes" encoding="UTF-8"/>

	<xsl:template match="/">
		<proaiml:proaiml>
			<xsl:apply-templates/>
		</proaiml:proaiml>
	</xsl:template>

	<xsl:template match="aiml/topic">
		<xsl:for-each select="category">
			<proaiml:category> 
				<proaiml:condition>
					<proaiml:context name="topic">
						<xsl:variable name="topic">
							<xsl:value-of select="parent::*/@name"/>
						</xsl:variable>
						<proaiml:pattern>
							<xsl:value-of select="translate($topic,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
					    </proaiml:pattern>
					</proaiml:context>
					<xsl:apply-templates select="that"/>
					<xsl:apply-templates select="pattern"/>
				</proaiml:condition>
				<xsl:apply-templates select="template"/>
			</proaiml:category>   
		</xsl:for-each>
	</xsl:template>

	<xsl:template match="aiml/category">
		<proaiml:category>
			<proaiml:condition>
				<xsl:apply-templates select="that"/>
				<xsl:apply-templates select="pattern"/>
			</proaiml:condition>
			<xsl:apply-templates select="template"/>  
		</proaiml:category>
	</xsl:template>

	<xsl:template match="that">
		<proaiml:context name="that">
			<xsl:for-each select="child::node()">
			   <proaiml:pattern>
					<xsl:copy-of select="."/>
				</proaiml:pattern>	
			</xsl:for-each>
		</proaiml:context>
	</xsl:template>

	<xsl:template match="pattern">
		<proaiml:context name="pattern">
			<xsl:for-each select="child::node()">
				<xsl:choose>
					<xsl:when test="local-name()='bot'">
						<proaiml:concept name="bot"><xsl:value-of select="self::*/@name"/></proaiml:concept>
					</xsl:when>
					<xsl:otherwise>
					    <proaiml:pattern>
							<xsl:copy-of select="."/>
						</proaiml:pattern>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:for-each>
		</proaiml:context>
	</xsl:template>

	<xsl:template match="template">
		<proaiml:action>
			<xsl:for-each select="child::node()">
				<xsl:copy-of select="."/>
			</xsl:for-each>
		</proaiml:action>
	</xsl:template>
	
</xsl:stylesheet>

  