<?xml version="1.0" encoding="utf-8"?>
<!--
	XSL Transformation for Cork'd Wine page.
	
	Make sure you have valid XML before, applying this stylesheet.

	http://www.ibm.com/developerworks/library/x-tiptidy.html
--> 
<xsl:stylesheet
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:html="http://www.w3.org/1999/xhtml"
	xmlns:vin="http://krono.act.uji.es/Links/ontologies/wine.owl#"
	xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xml:base="http://cork.org/"
	omit="html xsl"
	version = "1.0">
<xsl:output method="xml" indent="yes" />

<!--
	Character to remove from text to create an ID (via translate)
-->
<xsl:variable name="translate">"' -_
</xsl:variable>

<xsl:template match = "/" >
	<rdf:RDF>
		<xsl:apply-templates select = "//html:div[@id = 'content']" name="content" />
	</rdf:RDF>
</xsl:template>

<xsl:template match="html:div" name="content">
	<vin:Wine>
		<xsl:attribute name="rdf:ID">
			<xsl:text>Wine</xsl:text>
			<xsl:value-of select="translate(html:h2[@id = 'wine-name'], $translate, '')" />
		</xsl:attribute>
		<dc:title>
			<xsl:value-of select="html:h2[@id = 'wine-name']" />
		</dc:title>
		
		<vin:hasColor>
			<xsl:attribute name="rdf:resource">
			<xsl:choose>
				<xsl:when test="html:div[@id ='label-bottle']/html:h4[contains(@class, 'red')]">
					<xsl:text>http://krono.act.uji.es/Links/ontologies/wine.owl#Red</xsl:text>
				</xsl:when>
				<xsl:when test="html:div[@id = 'label-bottle']/html:h4[contains(@class, 'white')]">
					<xsl:text>http://krono.act.uji.es/Links/ontologies/wine.owl#White</xsl:text>
				</xsl:when>
				<xsl:when test="html:div[@id = 'label-bottle']/html:h4[contains(@class, 'rose')]">
					<xsl:text>http://krono.act.uji.es/Links/ontologies/wine.owl#Rose</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:text>http://krono.act.uji.es/Links/ontologies/wine.owl#Red</xsl:text>
				</xsl:otherwise>
			</xsl:choose>
			</xsl:attribute>
		</vin:hasColor>
		
		<vin:hasVintageYear>
			<vin:VintageYear>
				<xsl:attribute name="rdf:ID">
					<xsl:text>Year</xsl:text>
					<xsl:value-of select="html:div[@id = 'label-bottle']/html:h4" />
				</xsl:attribute>
				<vin:yearValue>
					<xsl:value-of select="html:div[@id = 'label-bottle']/html:h4" />
				</vin:yearValue>
			</vin:VintageYear>
		</vin:hasVintageYear>
		
		<xsl:apply-templates select = "html:table[contains(@class, 'wine')]" mode="wine" />
	</vin:Wine>
</xsl:template>

<xsl:template match="html:table" mode="wine">
	<xsl:variable name="winery">
		<xsl:apply-templates select="html:tr/html:th[contains(string(.), 'Winery')]" mode="following-td" />
	</xsl:variable>
	<xsl:variable name="region">
		<xsl:apply-templates select="html:tr/html:th[contains(string(.), 'Region')]" mode="following-td" />
	</xsl:variable>
	<xsl:variable name="country">
		<xsl:apply-templates select="html:tr/html:th[contains(string(.), 'Country')]" mode="following-td" />
	</xsl:variable>
	<xsl:variable name="varietal">
		<xsl:apply-templates select="html:tr/html:th[contains(string(.), 'Varietal')]" mode="following-td" />
	</xsl:variable>
	
	<vin:hasMaker>
		<vin:Winery>
			<xsl:attribute name="rdf:ID">
				<xsl:value-of select="translate($winery, $translate, '')" />
				<xsl:text>Winery</xsl:text>
			</xsl:attribute>
			<dc:title>
				<xsl:value-of select="$winery" />
			</dc:title>
			<vin:LocatedIn>
				<vin:Region>
					<xsl:attribute name="rdf:ID">
						<xsl:value-of select="translate($region, $translate, '')" />
						<xsl:text>Region</xsl:text>
					</xsl:attribute>
					<dc:title>
						<xsl:value-of select="$region" />
					</dc:title>
					<xsl:if test="$region != $country">
						<vin:LocatedIn>
							<vin:Region>
								<xsl:attribute name="rdf:ID">
									<xsl:value-of select="translate($country, $translate, '')" />
									<xsl:text>Region</xsl:text>
								</xsl:attribute>
								<dc:title>
									<xsl:value-of select="$country" />
								</dc:title>
							</vin:Region>
						</vin:LocatedIn>
					</xsl:if>
				</vin:Region>
			</vin:LocatedIn>
		</vin:Winery>
	</vin:hasMaker>
	
	<vin:madeFromGrape>
		<vin:WineGrape>
			<xsl:attribute name="rdf:ID">
				<xsl:value-of select="translate($varietal, $translate, '')" />
				<xsl:text>Grape</xsl:text>
			</xsl:attribute>
			<dc:title>
				<xsl:value-of select="$varietal" />
			</dc:title>
			<vin:hasMaker>
				<xsl:attribute name="rdf:resource">
					<xsl:text>#</xsl:text>
					<xsl:value-of select="translate($winery, $translate, '')" />
					<xsl:text>Winery</xsl:text>
				</xsl:attribute>
			</vin:hasMaker>
		</vin:WineGrape>
	</vin:madeFromGrape>
</xsl:template>

<xsl:template match="html:th" mode="following-td">
	<xsl:value-of select="following-sibling::html:td" />
</xsl:template>

</xsl:stylesheet>

