<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<!--
// // See license.txt for license information. // // xml2html.xsl // // NOTES // Generic tool for pretty printing an XML file to HTML. // // 7-Feb-2006 phamilton Created //
-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html" indent="no"/>
    <xsl:param name="csspath"/>
    <xsl:variable name="indent_increment">
        <xsl:text>    </xsl:text>
    </xsl:variable>
    <xsl:variable name="amp">&</xsl:variable>
    <xsl:variable name="lt"><</xsl:variable>
    <xsl:variable name="qt">"</xsl:variable>
    <xsl:template match="/">
        <xsl:apply-templates select="." mode="xmlverb">
            <xsl:with-param name="indent" select="''"/>
        </xsl:apply-templates>
    </xsl:template>
<!--
root
-->

    <xsl:template match="/" mode="xmlverb">
        <html>
            <head>
                <link rel="stylesheet" type="text/css">
                    <xsl:attribute name="href">
                        <xsl:value-of select="$csspath"/>
                    </xsl:attribute>
                </link>
            </head>
            <body>
                <div class="xmlverb-default">
                    <span class="xmlverb-pi-content">
<?                        <xsl:text>xml version="1.0" encoding="UTF-8" standalone="yes"</xsl:text>
?>                         <br/>
                        <br/>
                    </span>
                    <xsl:apply-templates mode="xmlverb">
                        <xsl:with-param name="indent" select="$indent"/>
                    </xsl:apply-templates>
                </div>
            </body>
        </html>
    </xsl:template>
<!--
wrapper
-->

<!--
xsl:template match="verb:wrapper"> <xsl:apply-templates mode="xmlverb"> <xsl:with-param name="indent" select="$indent"/> </xsl:apply-templates> </xsl:template> <xsl:template match="verb:wrapper" mode="xmlverb"> <xsl:apply-templates mode="xmlverb"> <xsl:with-param name="indent" select="$indent"/> </xsl:apply-templates> </xsl:template
-->

<!--
element nodes
-->

    <xsl:template match="*" mode="xmlverb">
        <xsl:value-of select="$indent"/>
        <xsl:text><</xsl:text>
        <xsl:variable name="ns-prefix" select="substring-before(name(),':')"/>
        <xsl:if test="$ns-prefix != ''">
            <span class="xmlverb-element-nsprefix">
                <xsl:value-of select="$ns-prefix"/>
            </span>
            <xsl:text>:</xsl:text>
        </xsl:if>
        <span class="xmlverb-element-name">
            <xsl:value-of select="local-name()"/>
        </span>
        <xsl:variable name="pns" select="../namespace::*"/>
        <xsl:if test="$pns[name()=''] and not(namespace::*[name()=''])">
            <span class="xmlverb-ns-name">
                <xsl:text> xmlns</xsl:text>
            </span>
            <xsl:text>=""</xsl:text>
        </xsl:if>
        <xsl:for-each select="namespace::*">
            <xsl:if test="not($pns[name()=name(current()) and .=current()])">
                <xsl:call-template name="xmlverb-ns"/>
            </xsl:if>
        </xsl:for-each>
        <xsl:for-each select="@*">
            <xsl:call-template name="xmlverb-attrs"/>
        </xsl:for-each>
        <xsl:choose>
            <xsl:when test="count(*)=0 and .=''">
                <xsl:text>/></xsl:text>
            </xsl:when>
            <xsl:otherwise>
                <xsl:text>></xsl:text>
                <xsl:if test="count(./*)">
                    <br/>
                </xsl:if>
                <xsl:apply-templates mode="xmlverb">
                    <xsl:with-param name="indent" select="concat($indent, $indent_increment)"/>
                </xsl:apply-templates>
                <xsl:if test="count(./*)">
                    <xsl:value-of select="$indent"/>
                </xsl:if>
                <xsl:text></</xsl:text>
                <xsl:if test="$ns-prefix != ''">
                    <span class="xmlverb-element-nsprefix">
                        <xsl:value-of select="$ns-prefix"/>
                    </span>
                    <xsl:text>:</xsl:text>
                </xsl:if>
                <span class="xmlverb-element-name">
                    <xsl:value-of select="local-name()"/>
                </span>
                <xsl:text>></xsl:text>
            </xsl:otherwise>
        </xsl:choose>
        <br/>
    </xsl:template>
<!--
attribute nodes
-->

    <xsl:template name="xmlverb-attrs">
        <xsl:text> </xsl:text>
        <span class="xmlverb-attr-name">
            <xsl:value-of select="name()"/>
        </span>
        <xsl:text>="</xsl:text>
        <span class="xmlverb-attr-content">
            <xsl:call-template name="html-replace-entities">
                <xsl:with-param name="text" select="normalize-space(.)"/>
                <xsl:with-param name="attrs" select="true()"/>
            </xsl:call-template>
        </span>
        <xsl:text>"</xsl:text>
    </xsl:template>
<!--
namespace nodes
-->

    <xsl:template name="xmlverb-ns">
        <xsl:if test="name()!='xml'">
            <span class="xmlverb-ns-name">
                <xsl:text> xmlns</xsl:text>
                <xsl:if test="name()!=''">
                    <xsl:text>:</xsl:text>
                </xsl:if>
                <xsl:value-of select="name()"/>
            </span>
            <xsl:text>="</xsl:text>
            <span class="xmlverb-ns-uri">
                <xsl:value-of select="."/>
            </span>
            <xsl:text>"</xsl:text>
        </xsl:if>
    </xsl:template>
<!--
text nodes
-->

    <xsl:template match="text()" mode="xmlverb">
        <span class="xmlverb-text">
            <xsl:call-template name="preformatted-output">
                <xsl:with-param name="text">
                    <xsl:call-template name="html-replace-entities">
                        <xsl:with-param name="text" select="."/>
                    </xsl:call-template>
                </xsl:with-param>
            </xsl:call-template>
        </span>
    </xsl:template>
<!--
comments
-->

    <xsl:template match="comment()" mode="xmlverb">
        <xsl:text><!--</xsl:text>
        <br/>
        <span class="xmlverb-comment">
            <xsl:value-of select="."/>
        </span>
        <br/>
        <xsl:text>--></xsl:text>
        <br/>
        <br/>
    </xsl:template>
<!--
processing instructions
-->

    <xsl:template match="processing-instruction()" mode="xmlverb">
        <xsl:text><?</xsl:text>
        <span class="xmlverb-pi-name">
            <xsl:value-of select="name()"/>
        </span>
        <xsl:if test=".!=''">
            <span class="xmlverb-pi-content">
                <xsl:value-of select="."/>
            </span>
        </xsl:if>
        <xsl:text>?></xsl:text>
        <br/>
    </xsl:template>
<!--
===========================================================
-->

<!--
Procedures / Functions
-->

<!--
===========================================================
-->

<!--
generate entities by replacing &, ", < and > in $text
-->

    <xsl:template name="html-replace-entities">
        <xsl:param name="text"/>
        <xsl:param name="attrs"/>
        <xsl:variable name="tmp">
            <xsl:call-template name="replace-substring">
                <xsl:with-param name="from" select="'>'"/>
                <xsl:with-param name="to" select="'>'"/>
                <xsl:with-param name="value">
                    <xsl:call-template name="replace-substring">
                        <xsl:with-param name="from" select="$lt"/>
                        <xsl:with-param name="to" select="'<'"/>
                        <xsl:with-param name="value">
                            <xsl:call-template name="replace-substring">
                                <xsl:with-param name="from" select="$amp"/>
                                <xsl:with-param name="to" select="'&'"/>
                                <xsl:with-param name="value" select="$text"/>
                            </xsl:call-template>
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:with-param>
            </xsl:call-template>
        </xsl:variable>
        <xsl:choose>
<!--
$text is an attribute value
-->

            <xsl:when test="$attrs">
                <xsl:call-template name="replace-substring">
                    <xsl:with-param name="from" select="' '"/>
                    <xsl:with-param name="to" select="' '"/>
                    <xsl:with-param name="value">
                        <xsl:call-template name="replace-substring">
                            <xsl:with-param name="from" select="$qt"/>
                            <xsl:with-param name="to" select="'"'"/>
                            <xsl:with-param name="value" select="$tmp"/>
                        </xsl:call-template>
                    </xsl:with-param>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$tmp"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
<!--
replace in $value substring $from with $to
-->

    <xsl:template name="replace-substring">
        <xsl:param name="value"/>
        <xsl:param name="from"/>
        <xsl:param name="to"/>
        <xsl:choose>
            <xsl:when test="contains($value,$from)">
                <xsl:value-of select="substring-before($value,$from)"/>
                <xsl:value-of select="$to"/>
                <xsl:call-template name="replace-substring">
                    <xsl:with-param name="value" select="substring-after($value,$from)"/>
                    <xsl:with-param name="from" select="$from"/>
                    <xsl:with-param name="to" select="$to"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$value"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
<!--
preformatted output:
-->

    <xsl:template name="preformatted-output">
        <xsl:param name="text"/>
<!--
pre
-->

        <xsl:value-of select="$text"/>
<!--
/pre
-->

<!--
xsl:call-template name="output-nl"> <xsl:with-param name="text"> <xsl:call-template name="replace-substring"> <xsl:with-param name="from" select="' '" /> <xsl:with-param name="to" select="' '" /> <xsl:with-param name="value" select="$text" /> </xsl:call-template> </xsl:with-param> </xsl:call-template
-->

    </xsl:template>
<!--
output nl as <br>
-->

    <xsl:template name="output-nl">
        <xsl:param name="text"/>
        <xsl:choose>
            <xsl:when test="contains($text,' ')">
                <xsl:value-of select="substring-before($text,' ')"/>
                <br/>
                <xsl:call-template name="output-nl">
                    <xsl:with-param name="text" select="substring-after($text,' ')"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$text"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>