<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:altova="http://www.altova.com">
	<xsl:output version="4.0" method="html" indent="yes" encoding="UTF-8" use-character-maps="no-control-characters"/>
	
	<!-- to avoid illegal HTML character error messages on transformation -->
	<xsl:character-map name="no-control-characters">
	   <xsl:output-character character="&#127;" string=" "/>
	   <xsl:output-character character="&#128;" string=" "/>
	   <xsl:output-character character="&#129;" string=" "/>
	   <xsl:output-character character="&#130;" string=" "/>
	   <xsl:output-character character="&#131;" string=" "/>
	   <xsl:output-character character="&#132;" string=" "/>
	   <xsl:output-character character="&#133;" string=" "/>
	   <xsl:output-character character="&#134;" string=" "/>
	   <xsl:output-character character="&#135;" string=" "/>
	   <xsl:output-character character="&#136;" string=" "/>
	   <xsl:output-character character="&#137;" string=" "/>
	   <xsl:output-character character="&#138;" string=" "/>
	   <xsl:output-character character="&#139;" string=" "/>
	   <xsl:output-character character="&#140;" string=" "/>
	   <xsl:output-character character="&#141;" string=" "/>
	   <xsl:output-character character="&#142;" string=" "/>
	   <xsl:output-character character="&#143;" string=" "/>
	   <xsl:output-character character="&#144;" string=" "/>
	   <xsl:output-character character="&#145;" string=" "/>
	   <xsl:output-character character="&#146;" string=" "/>
	   <xsl:output-character character="&#147;" string=" "/>
	   <xsl:output-character character="&#148;" string=" "/>
	   <xsl:output-character character="&#149;" string=" "/>
	   <xsl:output-character character="&#150;" string=" "/>
	   <xsl:output-character character="&#151;" string=" "/>
	   <xsl:output-character character="&#152;" string=" "/>
	   <xsl:output-character character="&#153;" string=" "/>
	   <xsl:output-character character="&#154;" string=" "/>
	   <xsl:output-character character="&#155;" string=" "/>
	   <xsl:output-character character="&#156;" string=" "/>
	   <xsl:output-character character="&#157;" string=" "/>
	   <xsl:output-character character="&#158;" string=" "/>
	   <xsl:output-character character="&#159;" string=" "/>
	</xsl:character-map>
	
	<xsl:variable name="XML" select="/"/>
	
	<xsl:template match="/">
		<html>
			<head>
				<title>XML validation report</title>
			</head>
			<body>
				<xsl:for-each select="$XML">
					<table>
						<tr>
							<td valign="bottom">
								<span style="font-family:@Arial Unicode MS; font-size:large; ">
									<xsl:text>XML validation report</xsl:text>
								</span>								
							</td>
						</tr>
						<tr>
							<td>
								<span style="font-family:@Arial Unicode MS; font-size:medium; ">
									<xsl:text>generated with XML ValidatorBuddy</xsl:text>
								</span>							
							</td>
						</tr>
					</table>
					
					<div style="height: 20px"></div>
					
					<table style="border-collapse:collapse; empty-cells:show; table-layout:fixed; " border="0">
						<tr>
							<td style="padding-bottom: 1em" width="200">
								Starting folder:
							</td>
							<td style="padding-bottom: 1em">
								<xsl:value-of select="log/batch_unit/folder/@path"/>
							</td>
						</tr>					
						<tr>
							<td>
								Number of files processed:
							</td>
							<td>
								<xsl:value-of select="count(//file)"/>
							</td>
						</tr>
						<tr>
							<td>
								Total files reported:
							</td>
							<td>
								<xsl:value-of select="count(//file[./@done/string() = 'true'])"/>
							</td>
						</tr>
						<tr>
							<td>
								Valid files:
							</td>
							<td>
								<xsl:value-of select="count(//file[(./@done/string() = 'true') and (./@valid/string() = 'true')])"/>
							</td>
						</tr>
						<tr>
							<td>
								Invalid files:
							</td>
							<td>
								<xsl:value-of select="count(//file[(./@done/string() = 'true') and (./@valid/string() = 'false')])"/>
							</td>
						</tr>						
					</table>					
					
					<div style="height: 20px"></div>
					
					<table style="border-collapse:collapse; empty-cells:show; table-layout:fixed; " border="1" width="1000">
						<thead>
							<tr style="background-color:lightgrey">
								<th>
									<span>
										<xsl:text>File</xsl:text>
									</span>
								</th>
								<th width="50">
									<span>
										<xsl:text>Valid</xsl:text>
									</span>
								</th>
								<th width="150">
									<span>
										<xsl:text>Time</xsl:text>
									</span>
								</th>
								<!--<th width="200">
									<span>
										<xsl:text>General result</xsl:text>
									</span>
								</th>-->
								<th width="300">
									<span>
										<xsl:text>Details</xsl:text>
									</span>
								</th>
							</tr>
						</thead>
						<tbody>
							<xsl:for-each select="log">
								<xsl:for-each select="batch_unit">
									<xsl:for-each select="file">
										<xsl:choose>
											<xsl:when test="./@done/string() = &apos;true&apos;">
												<xsl:if test="./@valid/string() = &apos;false&apos;">
													<tr style="background-color:pink;">
														<xsl:call-template name="file"/>
													</tr>
												</xsl:if>
												<xsl:if test="./@valid/string() = &apos;true&apos;">
													<tr style="background-color:palegreen;">
														<xsl:call-template name="file"/>
													</tr>											
												</xsl:if>
											</xsl:when>
											<xsl:otherwise>
												<tr>
													<xsl:call-template name="file"/>
												</tr>												
											</xsl:otherwise>
										</xsl:choose>
									</xsl:for-each>
								</xsl:for-each>
							</xsl:for-each>
						</tbody>
					</table>
				</xsl:for-each>
			</body>
		</html>
	</xsl:template>
	
	<xsl:template name="file">
		<td valign="top">
			<xsl:for-each select="@path">
				<span>
					<!-- <xsl:value-of select="string(.)"/> -->
					<xsl:value-of select="substring-after(.,/log/batch_unit/folder/@path)"/>
				</span>
			</xsl:for-each>
		</td>
		<td valign="top" width="50">
			<xsl:if test="./@done/string() = &apos;true&apos;">
				<xsl:for-each select="@valid">
					<xsl:value-of select="string(.)"/>
				</xsl:for-each>
			</xsl:if>
		</td>
		<td valign="top" width="150">
			<xsl:for-each select="@time">
				<span>
					<xsl:value-of select="string(.)"/>
				</span>
			</xsl:for-each>
		</td>
		<!--<td style="empty-cells:show; " valign="top" width="200">
			<xsl:for-each select="general">
				<xsl:apply-templates/>
			</xsl:for-each>
		</td>-->
		<td style="color:black; " width="300">
			<xsl:for-each select="results">
				<xsl:for-each select="result">
					<xsl:for-each select="description">
						<p>
							<xsl:apply-templates/>
						</p>
					</xsl:for-each>
				</xsl:for-each>
			</xsl:for-each>
		</td>	
	</xsl:template>
</xsl:stylesheet>
