<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:xhtml="http://www.w3.org/1999/xhtml"
	xmlns:exsl="http://exslt.org/common"
	xmlns:dr="http://documenta.rudolphina.org/"
	exclude-result-prefixes="xhtml exsl dr"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Copyright 2008 Manfred Staudinger, Vienna, Austria -->

<xsl:variable name="non-IE-text" select="'ABCDFGHJKLMNOPQRSTUVWXYZabcdfhijkmnopqrsuvwxyz'"/>
<xsl:variable name="vendor" select="system-property('xsl:vendor')"/>
<xsl:variable name="node-set" select="function-available('exsl:node-set')"/>
<xsl:variable name="browser">
	<xsl:choose>
		<xsl:when test="$vendor='Microsoft' and $ASV">
			<xsl:value-of select="'IE, SVG'"/>
		</xsl:when>
		<xsl:when test="$vendor='Microsoft'">
			<xsl:value-of select="'IE, noSVG'"/>
		</xsl:when>
		<xsl:when test="$vendor='Transformiix' and not(element-available('xsl:fallback'))">
			<xsl:value-of select="'Firefox 1, FF 1, Firefox, Gecko 1.7, Mozilla 1.7, Camino 0.8, Camino, Netscape, noSVG'"/>
		</xsl:when>
		<xsl:when test="$vendor='Transformiix' and not($node-set)">
			<xsl:value-of select="'Firefox 2, FF 2, Firefox, Gecko 1.8, SeaMonkey 1, SeaMonkey, Camino 1, Camino, Epiphany 2.14, Epiphany 2.20, Epiphany, Flock, K-Meleon, Kazehakase, Netscape, Navigator, SVG'"/>
		</xsl:when>
		<xsl:when test="$vendor='Transformiix' and $node-set">
			<xsl:value-of select="'Firefox 3, FF 3, Firefox, Gecko 1.9, SeaMonkey 2, SeaMonkey, Camino 2, Camino, Epiphany 2.22, Epiphany, Minefield, SVG'"/>
		</xsl:when>
		<xsl:when test="$vendor='libxslt' and not($node-set)">
			<xsl:value-of select="'Safari 1, Safari 2, Safari, noSVG'"/>
		</xsl:when>
		<xsl:when test="$vendor='libxslt' and $node-set">
			<xsl:value-of select="'Safari 3, Safari, SVG'"/>
		</xsl:when>
		<xsl:when test="$vendor='Opera' and not(function-available('document'))">
			<xsl:value-of select="'Opera 9, Opera, SVG'"/>
		</xsl:when>
		<xsl:when test="$vendor='Opera' and function-available('document')">
			<xsl:value-of select="'Opera 9.50, Opera, SVG'"/><!-- Opera 9.50 beta -->
		</xsl:when>
		<xsl:when test="$vendor='Apache Software Foundation'">
			<xsl:value-of select="'X-Smiles, SVG'"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="'unknown'"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:variable>

<!-- template to select and copy the browser relevant HTML -->
<!-- in case of IE add Conditional Comments -->
<xsl:template match="*[@dr:select]">
	<xsl:variable name="use-css">
		<xsl:call-template name="use-css">
			<xsl:with-param name="browser" select="$browser"/>
			<xsl:with-param name="class" select="@dr:select"/>
		</xsl:call-template>
	</xsl:variable>
	<xsl:if test="$use-css='true'">
		<xsl:variable name="IE-exp" select="starts-with($browser, 'IE') and string-length(@dr:select)=string-length(translate(@dr:select, $non-IE-text, ''))"/>
		<xsl:if test="$IE-exp">
			<xsl:comment><![CDATA[[if IE]><![if ]]><xsl:value-of select="@dr:select"/><![CDATA[]><![endif]]]></xsl:comment>
		</xsl:if>
		<xsl:copy>
			<xsl:apply-templates select="@*|node()"/>
		</xsl:copy>
		<xsl:if test="$IE-exp">
			<xsl:comment><![CDATA[[if IE]><![endif]><![endif]]]></xsl:comment>
		</xsl:if>
	</xsl:if>
</xsl:template>
<xsl:template match="@dr:select"/>

<!-- named template to decide which elements are relevant for the browser -->
<xsl:template name="use-css">
<xsl:param name="browser"/>
<xsl:param name="class"/>
	<xsl:variable name="bname" select="substring-before(concat($browser, ','), ',')"/>
	<xsl:choose>
		<xsl:when test="not($bname)">
			<xsl:value-of select="false()"/>
		</xsl:when>
		<xsl:when test="contains(concat($class, ','), concat($bname, ',')) or
				contains(translate($class, 'eglt( ', ''), '!IE') or
				($bname='IE' and contains($class, 'IE'))">
			<xsl:value-of select="true()"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:call-template name="use-css">
				<xsl:with-param name="browser" select="normalize-space(substring($browser, string-length($bname)+2))"/>
				<xsl:with-param name="class" select="$class"/>
			</xsl:call-template>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

</xsl:stylesheet>