RE: [xsl] msxml default stylesheet - now in XSLT

Subject: RE: [xsl] msxml default stylesheet - now in XSLT
From: "Trace Wilson" <trace_wilson@xxxxxxxxx>
Date: Tue, 22 May 2001 13:34:14 +1000
If you do not want a DHTML version, please give this script a try, that was
originally written by Jason Patterson at SnippetCentral at
http://www.vbxml.com/snippetcentral/main.asp?view=viewsnippet&lang=&id=v2001
0305161458

Its great for documenting your code.  You need to insert your XSL as your
XML code in which ever transforming tool you are using.  The output is a
replica of what is in your XSLT but written in HTML. You can change the
colour output by changing the parameters passed into the formatXML template.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
	<xsl:output omit-xml-declaration="yes" method="xml"/>
	<xsl:param name="includexml" select="'yes'" />
	<xsl:template match="/">
		<blockquote>
			<xsl:if test="$includexml = 'yes'">
				<font style="color:green;font:10pt 'Verdana';">
					<xsl:text>&lt;?xml version=&quot;1.0&quot;?&gt;</xsl:text>
				</font>
			</xsl:if>
			<xsl:for-each select="node()">
				<xsl:call-template name="formatxml">
					<xsl:with-param name="selnode" select="."/>
				</xsl:call-template>
			</xsl:for-each>
		</blockquote>
	</xsl:template>
	<xsl:template name="formatxml">
		<xsl:param name="selnode"/>
		<xsl:param name="template-style">font:10pt 'Verdana';</xsl:param>
		<xsl:param name="indent">0em;</xsl:param>
		<xsl:param name="punctuation-style">color:blue;</xsl:param>
		<xsl:param name="node-name-style">color:#990000;</xsl:param>
		<xsl:param name="comment-style">color:gray;</xsl:param>
		<xsl:param name="pi-style">color:green;</xsl:param>
		<xsl:param name="attr-value-style">color:black;</xsl:param>
		<xsl:param name="attr-name-style">color:red</xsl:param>
		<div>
			<xsl:choose>
				<xsl:when test="self::comment()">
					<xsl:attribute
name="style"><xsl:text>padding-left:</xsl:text><xsl:value-of
select="$indent"/></xsl:attribute>
					<font style="{$template-style}{$comment-style}">
						<xsl:text>&lt;!--</xsl:text>
						<xsl:value-of select="$selnode"/>
						<xsl:text>&gt;</xsl:text>
					</font>
				</xsl:when>
				<xsl:when test="self::processing-instruction()">
					<xsl:attribute
name="style"><xsl:text>padding-left:</xsl:text><xsl:value-of
select="$indent"/></xsl:attribute>
					<font style="{$template-style}{$pi-style}">
						<xsl:text>&lt;?</xsl:text>
						<xsl:value-of select="name()"/>
						<xsl:text/>
						<xsl:value-of select="."/>
						<xsl:text>?&gt;</xsl:text>
					</font>
				</xsl:when>
				<xsl:when test="self::text()">
					<xsl:attribute
name="style"><xsl:text>display:inline;</xsl:text></xsl:attribute>
					<font style="{$template-style}{text-node-style}">
						<xsl:call-template name="html-replace-entities">
							<xsl:with-param name="text" select="$selnode"/>
						</xsl:call-template>
					</font>
				</xsl:when>
				<xsl:otherwise>
					<xsl:attribute
name="style"><xsl:text>padding-left:</xsl:text><xsl:value-of
select="$indent"/></xsl:attribute>
					<font style="{$template-style}{$punctuation-style}">
						<xsl:text>&lt;</xsl:text>
					</font>
					<font style="{$template-style}{$node-name-style}">
						<xsl:value-of select="name($selnode)"/>
						<xsl:for-each select="$selnode/@*">
							<xsl:text>&#160;</xsl:text>
							<font style="{$template-style}{$attr-name-style}">
								<xsl:value-of select="name()"/>
							</font>
							<font style="{$template-style}{$punctuation-style}">
								<xsl:text>=&quot;</xsl:text>
							</font>
							<font style="{$template-style}{$attr-value-style}">
								<xsl:value-of select="."/>
							</font>
							<font style="{$template-style}{$punctuation-style}">
								<xsl:text>&quot;</xsl:text>
							</font>
						</xsl:for-each>
					</font>
					<xsl:choose>
						<xsl:when test="count(node()) = 0">
							<font style="{$template-style}{$punctuation-style}">
								<xsl:text>/&gt;</xsl:text>
							</font>
						</xsl:when>
						<xsl:otherwise>
							<font style="{$template-style}{$punctuation-style}">
								<xsl:text>&gt;</xsl:text>
							</font>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:for-each select="$selnode/node()">
				<xsl:call-template name="formatxml">
					<xsl:with-param name="selnode" select="."/>
					<xsl:with-param name="indent" select="concat(number('2'),'em;')"/>
				</xsl:call-template>
			</xsl:for-each>
			<xsl:choose>
				<xsl:when test="self::text()"/>
				<xsl:when test="count(node()) = 0"/>
				<xsl:otherwise>
					<font style="{$template-style}{$punctuation-style}">
						<xsl:text>&lt;/</xsl:text>
					</font>
					<font style="{$template-style}{$node-name-style}">
						<xsl:value-of select="name($selnode)"/>
					</font>
					<font style="{$template-style}{$punctuation-style}">
						<xsl:text>&gt;</xsl:text>
					</font>
				</xsl:otherwise>
			</xsl:choose>
		</div>
	</xsl:template>
	<xsl:template name="html-replace-entities">
		<xsl:param name="text"/>
		<xsl:variable name="tmp">
			<xsl:call-template name="replace-substring">
				<xsl:with-param name="from" select="'&gt;'"/>
				<xsl:with-param name="to" select="'&amp;gt;'"/>
				<xsl:with-param name="value">
					<xsl:call-template name="replace-substring">
						<xsl:with-param name="from" select="'&lt;'"/>
						<xsl:with-param name="to" select="'&amp;lt;'"/>
						<xsl:with-param name="value">
							<xsl:call-template name="replace-substring">
								<xsl:with-param name="from" select="'&quot;'"/>
								<xsl:with-param name="to" select="'&amp;quot;'"/>
								<xsl:with-param name="value">
									<xsl:call-template name="replace-substring">
										<xsl:with-param name="from" select="concat('&amp;','apos;')"/>
										<xsl:with-param name="to" select="'&amp;apos;'"/>
										<xsl:with-param name="value">
											<xsl:call-template name="replace-substring">
												<xsl:with-param name="from" select="'&amp;'"/>
												<xsl:with-param name="to" select="'&amp;amp;'"/>
												<xsl:with-param name="value" select="$text"/>
											</xsl:call-template>
										</xsl:with-param>
									</xsl:call-template>
								</xsl:with-param>
							</xsl:call-template>
						</xsl:with-param>
					</xsl:call-template>
				</xsl:with-param>
			</xsl:call-template>
		</xsl:variable>
		<xsl:value-of select="$tmp"/>
	</xsl:template>
	<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>
</xsl:stylesheet>

HTH,
Cheers,
Trace Wilson
http://www.vbxml.com
For live-instructor lead XSLT online training, go to http://www.trainit.com
Next course: May 29, 2001 GMT 8pm / US-ET 3pm
             May 30, 2001 GMT 10am /US-ET 5am


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread