<?xml version="1.0" encoding="utf-8"?>

<xslt:stylesheet version="1.0"
				xmlns:xslt="http://www.w3.org/1999/XSL/Transform"
				xmlns="http://www.w3.org/1999/xhtml"
                >

   <!-- ====================================================
        xml2html.xslt, (c) 2005 Linkwerk.com, Stefan Mintert, All rights reserved.
        Check http://www.linkwerk.com/pub/xml/xml2html/ for legal information.
        In no case may this copyright notice be removed.

        
        This XSLT stylesheet converts an input XML instance to HTML.
        
        Known issues:
        - Handling of namespaces is experimental. May not work properly.
          . Do not use more than one ns prefix for one ns.
          . Namespaces in the XPath data model are "visible" 
            only where they are used, not where they are declared.
        - Empty elements are written as starttag/endtag, not as empty element tag.
        - Whitespace is not preserved, but normalized. This may be a problem
          depending on the semantics of some element types.
        - Everything that is not accessible with XPath is dropped, i.e.
          DOCTYPE declaration, XML declaration.
        - The ampersand is not escaped. Therefore the character referenced
          by a character entity is written, instead of the entity itself.

        $Revision: 1.7 $ $Date: 2006/10/19 11:47:31 $
        ====================================================
   -->


        <!-- ==================================================== -->
        <!-- = Set output method to XML, but mime-type to HTML. = -->
        <!-- ==================================================== -->
	<xslt:output method="xml" encoding="utf-8" media-type="text/html"/>

	<!-- ==================================================== -->
	<!-- = Control of output: verbose (default) or short    = -->
	<!-- = ("short" = no html, head and body elements)      = -->
	<!-- ==================================================== -->
	<xslt:param name="htmloutput">verbose</xslt:param>

        <!-- ==================================================== -->
        <!-- = How to handle comments: drop or show             = -->
        <!-- ==================================================== -->
        <xslt:param name="comments">show</xslt:param>


	<!-- ==================================================== -->
	<!-- = START with/without HTML container elements       = -->
	<!-- ==================================================== -->
	<xslt:template 	match="/">
		<xslt:message>WARNING: Handling of namespace declarations is experimental. Please check for correct output.</xslt:message>
		
		<xslt:choose>
			<xslt:when test="$htmloutput = 'short'">
				<xslt:comment> 
    Generated by xml2html.xslt, (c) Linkwerk.com 2005, Do not remove this copyright note, www.linkwerk.com/pub/xml/xml2html/ 
				</xslt:comment>
				<xslt:comment> 
    Include this file in an HTML page and link to xml2html.css, available at www.linkwerk.com/pub/xml/xml2html/, to get an approriate rendering 
				</xslt:comment>
				<xslt:call-template name="xml2html-start"/>
			</xslt:when>
			<xslt:otherwise>
				<html>
					<head>
						<title>HTML rendering of a XML instance</title>
						<link rel="stylesheet" type="text/css" href="xml2html.css" />
						<meta name="generator" content="xml2html.xslt by Linkwerk, www.linkwerk.com/pub/xml/xml2html/"/>
					</head>
					<body>
						<xslt:comment> Generated by xml2html.xslt, (c) Linkwerk.com 2005, Do not remove this copyright note, www.linkwerk.com/pub/xml/xml2html/ </xslt:comment>
						<xslt:call-template name="xml2html-start"/>
					</body>
				</html>
			</xslt:otherwise>
		</xslt:choose>
	</xslt:template>


	<!-- ==================================================== -->
	<!-- = This is where it really starts                   = -->
	<!-- ==================================================== -->
	<xslt:template name="xml2html-start">
		<div class="xml2html root" xml:space="default" >
			<xslt:apply-templates select="*"/>
			<div class="xml2html comment copyright">&lt;!-- Generated with <a class="xml2html" href="http://www.linkwerk.com/pub/xml/xml2html/">xml2html.xslt</a>, Linkwerk.com --&gt;</div>
		</div>
	</xslt:template>

	<!-- ==================================================== -->
	<!-- = Processing of elements                           = -->
	<!-- ==================================================== -->
	<xslt:template match="*">
		<!-- Elements from known namespace are linked to their specification -->
		<xslt:variable name="url">
			<xslt:choose>
				<xslt:when test="namespace-uri() = 'http://www.w3.org/1999/XSL/Transform'">
					<xslt:choose>
						<xslt:when test="(ancestor-or-self::xslt:stylesheet[@version = '2.0']) or (ancestor-or-self::xslt:transform[@version = '2.0'])">
							<xslt:text>http://www.w3.org/TR/xslt20/#element-</xslt:text>
						</xslt:when>
						<xslt:otherwise>
							<!-- Default: Link with XSLT 1.0 spec -->
							<xslt:text>http://www.w3.org/TR/1999/REC-xslt-19991116.html#element-</xslt:text>
						</xslt:otherwise>
					</xslt:choose>
					<xslt:value-of select="local-name()"/>
				</xslt:when>
				<xslt:when test="namespace-uri() = 'http://exslt.org/functions'">
					<xslt:text>http://exslt.org/func/elements/</xslt:text>
					<xslt:value-of select="local-name()"/>
				</xslt:when>
				<xslt:when test="namespace-uri() = 'http://exslt.org/dates-and-times'">
					<xslt:text>http://exslt.org/date/elements/</xslt:text>
					<xslt:value-of select="local-name()"/>
				</xslt:when>
				<xslt:when test="namespace-uri() = 'http://exslt.org/common'">
					<xslt:text>http://exslt.org/exsl/elements/</xslt:text>
					<xslt:value-of select="local-name()"/>
				</xslt:when>
				<xslt:when test="namespace-uri() = 'http://exslt.org/common'">
					<xslt:text>http://exslt.org/exsl/elements/</xslt:text>
					<xslt:value-of select="local-name()"/>
				</xslt:when>
				<xslt:otherwise/>
			</xslt:choose>
		</xslt:variable>
		<div class="element xml2html">
			<span class="starttag xml2html">
				<xslt:choose>
					<xslt:when test="$url != ''">
						&lt;<a class="xml2html" href="{$url}"><xslt:value-of select="name()"/></a>
					</xslt:when>
					<xslt:otherwise>
						&lt;<xslt:value-of select="name()"/>
					</xslt:otherwise>
				</xslt:choose>
				<xslt:apply-templates select="." mode="process-ns-declarations"/><xslt:apply-templates select="@*"/>&gt;</span><xslt:if test="*|comment()|processing-instruction()|text()"><div class="elementcontent xml2html"><xslt:apply-templates select="*|comment()|processing-instruction()|text()"/></div></xslt:if><span class="endtag xml2html">&lt;/<xslt:value-of select="name()"/>&gt;</span>
        </div>
    </xslt:template>





	<!-- ==================================================== -->
	<!-- = Processing of attributes                         = -->
	<!-- ==================================================== -->
    <xslt:template     match="@*">
        <xslt:text> </xslt:text><span class="attributename xml2html"><xslt:value-of select="name()"/></span><xslt:text>="</xslt:text><span class="attributevalue xml2html"><xslt:value-of select="."/></span><xslt:text>"</xslt:text>
    </xslt:template>

	<!-- ==================================================== -->
	<!-- = Processing of PIs                                = -->
	<!-- ==================================================== -->
    <xslt:template     match="processing-instruction()">
        <div class="pi xml2html">&lt;?<xslt:value-of select="local-name()"/><xslt:text> </xslt:text><xslt:value-of select="."/>?&gt;</div>
	</xslt:template>
	<!-- ==================================================== -->
	<!-- = Processing of comments                           = -->
	<!-- ==================================================== -->
	<xslt:template 	match="comment()">
	  <xslt:if test="$comments != 'drop'">
		<div class="comment xml2html">&lt;!-- <xslt:value-of select="."/> --&gt;</div>
	  </xslt:if>
	</xslt:template>

	<!-- ==================================================== -->
	<!-- = Processing of namespace declarations             = -->
	<!-- ==================================================== -->
	<xslt:template 	match="*|@*"
					mode="process-ns-declarations">
		<!-- Process namespace of the element -->
		<xslt:if test="namespace-uri() != ''">
			<xslt:variable name="ns-prefix">
				<xslt:value-of select="substring-before(substring-before(name(),local-name()),':')"/>
			</xslt:variable>
			<xslt:variable name="ns-prefix-withcolon">
				<xslt:choose>
					<xslt:when test="$ns-prefix = ''"/>
					<xslt:otherwise>
						<xslt:text>:</xslt:text>
						<xslt:value-of select="$ns-prefix"/>
					</xslt:otherwise>
				</xslt:choose>
			</xslt:variable>
			<xslt:if test="not(ancestor::*[(namespace-uri() = namespace-uri(current())) and (substring-before(name(),local-name()) = substring-before(name(current()),local-name(current())))])">
        		<xslt:text> </xslt:text><span class="attributename xml2html">xmlns<xslt:value-of select="$ns-prefix-withcolon"/></span><xslt:text>="</xslt:text><span class="attributevalue xml2html"><xslt:value-of select="namespace-uri()"/></span><xslt:text>"</xslt:text>
			</xslt:if>
		</xslt:if>
		<!-- Process namespaces of the attributes-->
		<xslt:for-each select="./@*">
			<xslt:apply-templates select="." mode="process-ns-declarations"/>
		</xslt:for-each>
	</xslt:template>

	<!-- The following elements have no meaning to the XSLT enginge.
         Their purpose is to test this program. Run it with itself as
         the input XML instance -->
	<a:b xmlns:a="bla" xmlns:c="ns-c" c:att="val">
		<b xmlns="http://ns.example.com/"/>
	</a:b>
</xslt:stylesheet>
