<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dyn="http://exslt.org/dynamic"
xmlns:str="http://exslt.org/strings" exclude-result-prefixes="exslt">
	<xsl:output method="xml" indent="yes"/>
	<xsl:template match="/">
		<html>
			<head>
				<style type="text/css">
				<xsl:comment>
					A.Selected {color:#FFFFFF}
					
					
					A {color: #003192}
					A.balken {color: #FFFFFF; text-decoration:none}
					A:visited {color: #666666}
					A:visited.balken {color: #FFFFFF; font-size:small; text-decoration:none}
					
					
					BODY {background-color: #EEEEF0; margin-left:10; font-size:12}
					
					#P {margin-left:0; margin-right:0}
					H1 {font-family:Helvetica,sans-serif;}
					H2 {font-family:Helvetica,sans-serif;}
					H3 {font-family:Helvetica,sans-serif; }
					H4 {font-family:Helvetica,sans-serif; }
					P {font-family:Helvetica,sans-serif; font-size:16}
					DL {font-family:Helvetica,sans-serif; }
					HR { shading:no}
					LI,UL,OL {font-family:Helvetica,sans-serif;}
					
					#TABLE {padding:0}
					TD {font-family:Helvetica,sans-serif; vertical-align:top; padding:0; font-size:12}
					TD.logo {vertical-align:middle; width:100} 
					TD.header {vertical-align:middle; margin:0; padding:0; font-size:small; }
					TD.balken {vertical-align:middle;  color:#003192; margin:0; padding:0; font-size:small}
					
					
					TH {font-family:Helvetica,sans-serif;}
					
					DT {font-family:Helvetica,sans-serif; font-weight:bold}
				</xsl:comment>
				</style>
				<title>C-EPC Validator</title>
			</head>
			<body>
			<table>
				<tr>
					<td class="balken"><h1>C-EPC Validator</h1><br/> </td>
				</tr>
				<tr>
					<td> </td>
				</tr>
			</table>
			<table border="0">
				<tbody>
					<xsl:apply-templates select="//*[name()='epc']"/>
				</tbody>
			</table>
			</body>
		</html>
	</xsl:template>
	
	<xsl:template match="*">
		<xsl:variable name="epcId" select="@epcId"/>
		<tr>
			<td align="left" class="balken"><h2>Configuration Nodes</h2></td>
		</tr>
		<tr>
		<td>
			<table>
				<xsl:for-each select="*[./*/configuration]">
					<xsl:variable name="id" select="@id" /> 
					<xsl:variable name="name" select="*[name() = 'name']"/>
					<xsl:choose>
						<xsl:when test="name()='function'">
							<xsl:variable name="value" select="./*/configuration/@value"/>
							<tr>
								<td><xsl:value-of select="name()"/></td>
								<td><i><xsl:value-of select="$name"/></i></td>
								<td><xsl:value-of select="concat('(id=',$id,')')"/></td>
								<xsl:choose>
									<xsl:when test="$value='on' or $value='off' or $value='opt'">
										<td><font color="green"><xsl:value-of select="$value"/></font></td>
									</xsl:when>
									<xsl:otherwise>
										<td><font color="red"><xsl:value-of select="$value"/></font></td>
									</xsl:otherwise>
								</xsl:choose>
								<td/>
							</tr>
						</xsl:when>
						<xsl:when test="name()='and'">
							<xsl:variable name="value" select="./*/configuration/@value"/>
							<tr>
								<td><xsl:value-of select="name()"/></td>
								<td><i><xsl:value-of select="$name"/></i></td>
								<td><xsl:value-of select="concat('(id=',$id,')')"/></td>
								<xsl:choose>
									<xsl:when test="$value='and'">
										<td><font color="green"><xsl:value-of select="$value"/></font></td>
									</xsl:when>
									<xsl:otherwise>
										<td><font color="red"><xsl:value-of select="$value"/></font></td>
									</xsl:otherwise>
								</xsl:choose>
								<td/>
							</tr>
						</xsl:when>
						<xsl:when test="name()='or'">
							<xsl:variable name="value" select="./*/configuration/@value"/>
							<tr>
								<td><xsl:value-of select="name()"/></td>
								<td><i><xsl:value-of select="$name"/></i></td>
								<td><xsl:value-of select="concat('(id=',$id,')')"/></td>
								<xsl:choose>
									<xsl:when test="$value='and' or $value='or' or $value='xor'">
										<td><font color="green"><xsl:value-of select="$value"/></font></td>
									</xsl:when>
									<xsl:when test="$value='seq'">
										<xsl:variable name="goto" select="./*/configuration/@goto"/>
										<td><font color="green"><xsl:value-of select="$value"/></font>
										<xsl:choose>
											<xsl:when test="../*[name()='arc']/*[@source=$id][@target=$goto]">
												+ <font color="green"> goto (id=<xsl:value-of select="$goto"/>)</font>
											</xsl:when>
											<xsl:otherwise>
												+ <font color="red"> goto (id=<xsl:value-of select="$goto"/>)</font>
											</xsl:otherwise>
										</xsl:choose>
										</td>
									</xsl:when>
									<xsl:otherwise>
										<td><font color="red"><xsl:value-of select="$value"/></font></td>
									</xsl:otherwise>
								</xsl:choose>
								<td/>
							</tr>
						</xsl:when>
						<xsl:when test="name()='xor'">
							<xsl:variable name="value" select="./*/configuration/@value"/>
							<tr>
								<td><xsl:value-of select="name()"/></td>
								<td><i><xsl:value-of select="$name"/></i></td>
								<td><xsl:value-of select="concat('(id=',$id,')')"/></td>
								<xsl:choose>
									<xsl:when test="$value='xor'">
										<td><font color="green"><xsl:value-of select="$value"/></font></td>
									</xsl:when>
									<xsl:when test="$value='seq'">
										<xsl:variable name="goto" select="./*/configuration/@goto"/>
										<td><font color="green"><xsl:value-of select="$value"/></font>
										<xsl:choose>
											<xsl:when test="../*[name()='arc']/*[@source=$id][@target=$goto]">
												+ <font color="green"> goto (id=<xsl:value-of select="$goto"/>)</font>
											</xsl:when>
											<xsl:otherwise>
												+ <font color="red"> goto (id=<xsl:value-of select="$goto"/>)</font>
											</xsl:otherwise>
										</xsl:choose>
										</td>
									</xsl:when>
									<xsl:otherwise>
										<td><font color="red"><xsl:value-of select="$value"/></font></td>
									</xsl:otherwise>
								</xsl:choose>
								<td/>
							</tr>
						</xsl:when>
					</xsl:choose>
				</xsl:for-each>
				<tr>
					<td><br/><br/><br/></td>
				</tr>
			</table>
		</td>
		</tr>
		
		<tr>
			<td align="left" class="balken"><h2>Configuration Requirements</h2></td>
		</tr>
		<xsl:for-each select="*[name()='configurationRequirement']">
			<tr>
				<td>
				<xsl:if test="position()>'1'"><br/></xsl:if>
				<b><font color="#003192"><xsl:value-of select="position()"/>. Requirement involving the EPC elements:</font></b><br/>
				<xsl:variable name="idRefs" select="@idRefs"/>
				<table>
					<xsl:call-template name="involvednodes">
						<xsl:with-param name="list" select="$idRefs"/>
						<xsl:with-param name="epcId" select="$epcId"/>
					</xsl:call-template>
					</table>
				<b>Condition:</b><br/>
				<xsl:variable name="if-xpath" select="*[name()='if']/@xpath"/>
				<xsl:variable name="if">
					<xsl:choose>
						<xsl:when test="starts-with($if-xpath,'/')"><xsl:value-of select="$if-xpath"/></xsl:when>
						<xsl:otherwise><xsl:value-of select="concat('../',$if-xpath)"/></xsl:otherwise>
					</xsl:choose>
				</xsl:variable>
				<xsl:variable name="then-xpath" select="*[name()='then']/@xpath"/>
				<xsl:variable name="then">
					<xsl:choose>
						<xsl:when test="starts-with($then-xpath,'/')"><xsl:value-of select="$then-xpath"/></xsl:when>
						<xsl:otherwise><xsl:value-of select="concat('../',$then-xpath)"/></xsl:otherwise>
					</xsl:choose>
				</xsl:variable>
				<xsl:value-of select="concat(*[name()='if']/@xpath,' ')"/>
				<xsl:choose>
					<xsl:when test="dyn:evaluate($if)"><font color="green"><b>true</b><br/></font>
						<b>Implication:<br/></b>
						<xsl:value-of select="concat(*[name()='then']/@xpath,' ')"/>
						<xsl:choose>
							<xsl:when test="dyn:evaluate($then)"><font color="green"><b>true</b></font></xsl:when>
							<xsl:otherwise><font color="red"><b>false</b></font></xsl:otherwise>
						</xsl:choose>
					</xsl:when>
					<xsl:otherwise><font color="black">false<br/></font>
						<b>Implication:<br/></b>
						<xsl:value-of select="concat(*[name()='then']/@xpath,' ')"/>
						<xsl:choose>
							<xsl:when test="dyn:evaluate($then)"><font color="black"><b>true</b></font></xsl:when>
							<xsl:otherwise><font color="black"><b>false</b></font></xsl:otherwise>
						</xsl:choose>
					</xsl:otherwise>
				</xsl:choose>
				<br/>
				</td>
			</tr>
		</xsl:for-each>
		<tr>
			<td><br/><br/><br/></td>
		</tr>
		<tr>
			<td align="left" class="balken"><h2>Configuration Guidelines</h2></td>
		</tr>
		<xsl:for-each select="*[name()='configurationGuideline']">
			<tr>
				<td>
				<xsl:if test="position()>'1'"><br/></xsl:if>
				<b><font color="#003192"><xsl:value-of select="position()"/>. Guideline involving the EPC elements:</font></b><br/>
				<xsl:variable name="idRefs" select="@idRefs"/>
				<table>
					<xsl:call-template name="involvednodes">
						<xsl:with-param name="list" select="$idRefs"/>
						<xsl:with-param name="epcId" select="$epcId"/>
					</xsl:call-template>
					</table>
				<b>Condition:</b><br/>
				<xsl:variable name="if-xpath" select="*[name()='if']/@xpath"/>
				<xsl:variable name="if">
					<xsl:choose>
						<xsl:when test="starts-with($if-xpath,'/')"><xsl:value-of select="$if-xpath"/></xsl:when>
						<xsl:otherwise><xsl:value-of select="concat('../',$if-xpath)"/></xsl:otherwise>
					</xsl:choose>
				</xsl:variable>
				<xsl:variable name="then-xpath" select="*[name()='then']/@xpath"/>
				<xsl:variable name="then">
					<xsl:choose>
						<xsl:when test="starts-with($then-xpath,'/')"><xsl:value-of select="$then-xpath"/></xsl:when>
						<xsl:otherwise><xsl:value-of select="concat('../',$then-xpath)"/></xsl:otherwise>
					</xsl:choose>
				</xsl:variable>
				<xsl:value-of select="concat(*[name()='if']/@xpath,' ')"/>
				<xsl:choose>
					<xsl:when test="dyn:evaluate($if)"><font color="green"><b>true</b><br/></font>
						<b>Implication:<br/></b>
						<xsl:value-of select="concat(*[name()='then']/@xpath,' ')"/>
						<xsl:choose>
							<xsl:when test="dyn:evaluate($then)"><font color="green"><b>true</b></font></xsl:when>
							<xsl:otherwise><font color="orange"><b>false</b></font></xsl:otherwise>
						</xsl:choose>
					</xsl:when>
					<xsl:otherwise><font color="black"><b>false</b><br/></font>
						<b>Implication:<br/></b>
						<xsl:value-of select="concat(*[name()='then']/@xpath,' ')"/>
						<xsl:choose>
							<xsl:when test="dyn:evaluate($then)"><font color="black"><b>true</b></font></xsl:when>
							<xsl:otherwise><font color="black"><b>false</b></font></xsl:otherwise>
						</xsl:choose>
					</xsl:otherwise>
				</xsl:choose>
				<br/>
				</td>
			</tr>
		</xsl:for-each>
		<tr>
			<td> </td>
		</tr>
	</xsl:template>
	
	<xsl:template name="involvednodes">
		<xsl:param name="list"/>
		<xsl:param name="epcId"/>
		<xsl:choose>
			<xsl:when test="0 >= string-length($list)"/> 
			<xsl:otherwise>
				<tr>
					<xsl:variable name="id" select="substring-before(concat($list,' '),' ')" /> 
					<xsl:variable name="type" select="name(//*[@epcId = $epcId]/*[@id = $id] )"/>
					<xsl:variable name="name" select="//*[@epcId = $epcId]/*[@id = $id]/*[name() = 'name']"/>
					<td><xsl:value-of select="concat($type,' ')"/></td>
					<td><i><xsl:value-of select="$name"/></i></td>
					<td><xsl:value-of select="concat('(id=',$id,')')"/></td>
				</tr>
				<xsl:call-template name="involvednodes">
					<xsl:with-param name="list" select="substring-after($list,' ')" /> 
					<xsl:with-param name="epcId" select="$epcId" /> 
				</xsl:call-template>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>

