<?xml version="1.0" encoding="UTF-8"?>
<!-- 
	Stylesheet to convert an iEat Recipe into the RecipeML XML format.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" 
	xmlns:eat="http://msqr.us/xsd/ieat"
	xmlns:xweb="http://msqr.us/xsd/jaxb-web"
	xmlns:rml="http://www.formatdata.com/recipeml/recipeml.dtd"
	exclude-result-prefixes="eat rml xweb">
	
	<xsl:output method="xml" omit-xml-declaration="no" indent="yes" 
		doctype-public="-//FormatData//DTD RecipeML 0.5//EN"
		doctype-system="http://www.formatdata.com/recipeml/recipeml.dtd"/>
	
	<xsl:template match="/">
		<recipeml version="0.5">
			<xsl:apply-templates select="(//eat:recipe)[1]"/>
		</recipeml>
	</xsl:template>
	
	<xsl:template match="eat:recipe">
		<!-- We'll use the $pos value to keep the ID values unique within a 
		     document of multiple recipes -->
		<recipe>
			<xsl:choose>
				<xsl:when test="(eat:ingredient/eat:unit)[1]/@systemId = '1'">
					<xsl:attribute name="system">
						<xsl:text>US</xsl:text>
					</xsl:attribute>
				</xsl:when>
				<xsl:when test="(eat:ingredient/eat:unit)[1]/@systemId = '2'">
					<xsl:attribute name="system">
						<xsl:text>metric</xsl:text>
					</xsl:attribute>				
				</xsl:when>
			</xsl:choose>
			
			<xsl:if test="@recipeId">
				<xsl:attribute name="id">
					<xsl:text>recipe-</xsl:text>
					<xsl:value-of select="@recipeId"/>
				</xsl:attribute>
			</xsl:if>
			
			<!-- head element -->
			<head>
				<title>
					<xsl:value-of select="eat:name"/>
				</title>
				<xsl:if test="string-length(eat:source) &gt; 0">
					<source>
						<xsl:value-of select="eat:source"/>
					</source>
				</xsl:if>
				<xsl:if test="eat:base | eat:course | eat:ethnicity | eat:difficulty | eat:owner">
					<categories>
						<xsl:if test="eat:base/@baseId">
							<cat type="base" id="base-{eat:base/@baseId}"><xsl:value-of select="eat:base"/></cat>
						</xsl:if>
						<xsl:if test="eat:course/@courseId">
							<cat type="course" id="course-{eat:course/@courseId}"><xsl:value-of select="eat:course"/></cat>
						</xsl:if>
						<xsl:if test="eat:ethnicity/@ethnicityId">
							<cat type="ethnicity" id="ethnicity-{eat:ethnicity/@ethnicityId}"><xsl:value-of select="eat:ethnicity"/></cat>
						</xsl:if>
						<xsl:if test="eat:difficulty/@difficultyId">
							<cat type="difficulty" id="difficulty-{eat:difficulty/@difficultyId}"><xsl:value-of select="eat:difficulty"/></cat>
						</xsl:if>
						<xsl:if test="eat:owner/eat:email">
							<cat type="created-by" id="owner-{eat:owner/@userId}"><xsl:value-of select="eat:owner/eat:email"/></cat>
						</xsl:if>
					</categories>
				</xsl:if>
				<xsl:if test="eat:prep-time/@prepTimeId">
					<xsl:variable name="q1" select="substring-before(eat:prep-time,'-')"/>
					<xsl:variable name="units" select="substring-after(eat:prep-time,' ')"/>
					<preptime type="prepare" id="preptime-{eat:prep-time/@prepTimeId}">
						<time>
							<xsl:choose>
								<xsl:when test="string-length($q1) &gt; 0">
									<range>
										<q1><xsl:value-of select="$q1"/></q1>
										<q2><xsl:value-of select="substring-after(substring-before(
											eat:prep-time,' '),'-')"/></q2>
									</range>
								</xsl:when>
								<xsl:otherwise>
									<qty>
										<xsl:value-of select="substring-before(eat:prep-time,' ')"/>
									</qty>
								</xsl:otherwise>
							</xsl:choose>
							<timeunit><xsl:value-of select="$units"/></timeunit>
						</time>
					</preptime>
				</xsl:if>
				<xsl:if test="@servingSize &gt; 0">
					<yield>
						<xsl:value-of select="@servingSize"/>
					</yield>
				</xsl:if>
			</head>
			
			<!-- description element -->
			<xsl:if test="string-length(eat:excerpt) &gt; 0">
				<description>
					<xsl:value-of select="eat:excerpt"/>
				</description>
			</xsl:if>
			
			<!-- ingrediends element -->
			<ingredients>
				<xsl:apply-templates select="eat:ingredient"/>
			</ingredients>
			
			<!-- directions element -->
			<directions>
				<!-- insert overall directions as first step -->
				<xsl:if test="string(normalize-space(eat:directions))">
					<step><xsl:value-of select="eat:directions"/></step>
				</xsl:if>
				<xsl:apply-templates select="eat:step"/>
			</directions>
		</recipe>
	</xsl:template>
	
	<xsl:template match="eat:ingredient">
		<xsl:variable name="ipos">
			<xsl:text>ing-</xsl:text>
			<xsl:value-of select="position()"/>
			<xsl:text>-</xsl:text>
		</xsl:variable>
		<ing>
			<xsl:if test="@optional">
				<xsl:attribute name="optional">
					<xsl:choose>
						<xsl:when test="@optional = 'true'">
							<xsl:text>yes</xsl:text>
						</xsl:when>
						<xsl:otherwise>
							<xsl:text>no</xsl:text>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:attribute>
			</xsl:if>
			<amt>
				<qty><xsl:value-of select="@quantity"/></qty>
				<xsl:if test="eat:unit">
					<unit id="{$ipos}u-{eat:unit/@unitId}">
						<xsl:value-of select="eat:unit/@abbreviation"/>
					</unit>
				</xsl:if>
			</amt>
			<xsl:if test="string-length(@qualifier) &gt; 0">
				<modifier>
					<xsl:value-of select="@qualifier"/>
				</modifier>
			</xsl:if>
			<item>
				<xsl:attribute name="id">
					<xsl:value-of select="$ipos"/>
					<xsl:choose>
						<xsl:when test="eat:recipe">
							<xsl:text>r-</xsl:text>
							<xsl:value-of select="eat:recipe/@recipeId"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:text>i-</xsl:text>
							<xsl:value-of select="eat:ingredient/@ingredientId"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:attribute>
				<xsl:choose>
					<xsl:when test="eat:recipe">
						<xsl:value-of select="eat:recipe/eat:name"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="eat:ingredient/eat:name"/>
					</xsl:otherwise>
				</xsl:choose>
			</item>
		</ing>
	</xsl:template>
	
	<xsl:template match="eat:step">
		<step>
			<xsl:value-of select="."/>
		</step>
	</xsl:template>

</xsl:stylesheet>