<?xml version='1.0' encoding='utf-8'?>
<xsl:stylesheet
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:rs='urn:schemas-microsoft-com:rowset'
 	xmlns:z='#RowsetSchema'
	xmlns:date="http://www.ora.com/XSLTCookbook/NS/dates"
    xml:space="default" 
    xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" 
	version="1.0">

	<xsl:strip-space elements="true" />
	<xsl:output omit-xml-declaration="yes" />
	<xsl:param name="path"></xsl:param>
	<xsl:param name="url"></xsl:param>

	<xsl:template match='//rs:data'>
		<script type="text/javascript" src="/xslt/advertenties.js"></script>	
		<link rel="stylesheet" type="text/css" href="/xslt/advertenties.css"/>
		
		<ul>
			<xsl:apply-templates select="*[position()]" mode='main' />
		</ul>
	</xsl:template>

	<xsl:template match='*' mode="main">
		<li>
			<xsl:attribute name="class">
				<xsl:call-template name="tagitems">
					<xsl:with-param name="list"><xsl:value-of select="@ows_Categorie"/></xsl:with-param>
					<xsl:with-param name="listcode"><xsl:value-of select="@ows_Categorie_x003a_Code"/></xsl:with-param>
					<xsl:with-param name="delimiter">;#</xsl:with-param>
				</xsl:call-template>
			</xsl:attribute>
			<a>
				<xsl:choose>
					<xsl:when test="@ows_Pagina = 1">
						<xsl:attribute name="href">/adverteerders/detail.aspx?item=<xsl:value-of select="@ows_Code" disable-output-escaping="yes"/></xsl:attribute>
					</xsl:when>
					<xsl:otherwise>
						<xsl:attribute name="target">_blank</xsl:attribute>
						<xsl:attribute name="href"><xsl:value-of select="@ows_URL" disable-output-escaping="yes"/></xsl:attribute>
					</xsl:otherwise>
				</xsl:choose>
				<img>
					<xsl:attribute name="src">/advertentielogo/<xsl:value-of select="@ows_Logo" disable-output-escaping="yes" /></xsl:attribute>
				</img>
			</a>
		</li>
	</xsl:template>

	<xsl:template name="cleanup">
		<xsl:param name="text"/>
		<xsl:choose>
			<xsl:when test="contains($text, '_')">
				<xsl:value-of select="substring-after($text,'_')"></xsl:value-of>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$text"></xsl:value-of>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="tagitems">
		<xsl:param name="list" />
		<xsl:param name="listcode" />
		<xsl:param name="delimiter" />
		<xsl:variable name="newlist">
			<xsl:choose>
				<xsl:when test="contains($list, $delimiter)">
					<xsl:value-of select="normalize-space($list)" />
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="concat(normalize-space($list), $delimiter)"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="newlistcode">
			<xsl:choose>
				<xsl:when test="contains($listcode, $delimiter)">
					<xsl:value-of select="normalize-space($listcode)" />
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="concat(normalize-space($listcode), $delimiter)"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="first" select="substring-before($newlist, $delimiter)" />
		<xsl:variable name="firstcode" select="substring-before($newlistcode, $delimiter)" />
		<xsl:variable name="remaining" select="substring-after($newlist, $delimiter)" />
		<xsl:variable name="remainingcode" select="substring-after($newlistcode, $delimiter)" />
		<xsl:if test="$firstcode != '' and string(number($firstcode)) = 'NaN'"><xsl:value-of select="concat($firstcode, ' ')" /></xsl:if>
		<xsl:if test="$remaining">
			<xsl:call-template name="tagitems">
				<xsl:with-param name="list" select="$remaining" />
				<xsl:with-param name="listcode" select="$remainingcode" />
				<xsl:with-param name="delimiter">
					<xsl:value-of select="$delimiter"/>
				</xsl:with-param>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>


</xsl:stylesheet>