RE: [xsl] Transforming XML to XML

Subject: RE: [xsl] Transforming XML to XML
From: Pieter Reint Siegers Kort <pieter.siegers@xxxxxxxxxxx>
Date: Wed, 11 Aug 2004 17:55:49 -0500
Hi James,

If you want to let XML show up in the browser (and additionally add syntax
coloring), below is a stylesheet written originally by Jason Patterson. I
still use it without any modifications. The original link to it was:
http://www.topxml.com/snippetcentral/main.asp?view=viewsnippet&lang=&id=v200
10305161458

Try it out and you'll see what I mean :-)

HTH,
<prs/>
http://www.x2x2x.org/x2x2x/home/
http://sourceforge.net/projects/saxondotnet/
http://www.pietsieg.com/sections/XQuery/ArtXQueryBeg.aspx

**********************************
<?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>&#160;</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>
						<xsl:if
test="not(parent::*/parent::*)">
							<xsl:for-each
select="namespace::*[position() &gt; 1]">
        							<xsl:if
test="not(parent::*/parent::*)">
 
<xsl:text>&#160;</xsl:text>
	
<font style="{$template-style}{$attr-name-style}">
 
<xsl:text>xmlns</xsl:text>
 
<xsl:if test="string-length(name())>0"><xsl:text>:</xsl:text></xsl:if>
 
<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:if>
							</xsl:for-each>
						</xsl:if>
					</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>

********************************** 

-----Original Message-----
From: Pilarski, James [mailto:James_Pilarski@xxxxxxxxxxxxxxxx] 
Sent: Wednesday, August 11, 2004 3:40 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] Transforming XML to XML

I have a very simple question about transforming XML to XML.  I have been
using XSLT to transform XML to XHTML and displaying the result in Internet
Explorer.  I would like to do the same but with XML displayed in the
browser.  A search of the archives has not provided a satisfactory solution.
Here is a simplified XML file, testdata.xml:

<?xml-stylesheet type="text/xsl" href="renewal.xsl"?> <Data>
	<car>Chevy</car>
	<car>Dodge</car>
	<car>Ford</car>
</Data>

Here is a copy of my XSL file:

<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" version="1.0" encoding="iso-8859-1" indent="yes"/>
<xsl:template match="/">
		<xsl:apply-templates select="Data"/>
</xsl:template>
</xsl:stylesheet>

Current Thread