RE: [xsl] hard core xlt ;-)

Subject: RE: [xsl] hard core xlt ;-)
From: "Dylan Walsh" <Dylan.Walsh@xxxxxxxxxx>
Date: Thu, 26 Jul 2001 17:53:18 +0100
From:	Elise D. [SMTP:tyris_azriel@xxxxxxxx]
Sent:	Thursday, July 26, 2001 4:14 PM
To:	xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:	RE: [xsl] hard core xlt ;-)

>well, i like the way you think about what is hard or
>not. i tryed for a first time to make "generic" xsl,
>meaning i never use the name of a tag, but only for a
>defined hierarchy/structure of the xml.
>
>so if i've well understood the way it should be done,
>i should detect the type of the node, detect what it
>contains .... is it right ?
>
>well, if you ever have a small example of doing this,
>it would be great :-)

The following probably has bugs, and it was written a long time ago. I'd
probably do a lot of things differently today, but I don't see any point
updating this when there is atleast one working implementation out
there.

Microsoft have something similar in IE5, but that is MSXSL rather than
true XSLT. Their one is a lot slicker than mine. There are things you
cannot do in XSLT, but you can in MSXSL, in particular you cannot "see"
the XML declaration with XSLT. Microsoft may have updated that
transformation to proper XSLT for MSXML3, others on the list would know
more about that than me.

<?xml version="1.0"?>
<!DOCTYPE xsl:stylesheet [<!ENTITY nbsp "&#160;">]> 
<xsl:stylesheet version="1.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform";> 
<xsl:output method="html" indent="yes" omit-xml-declaration="no"/>
<xsl:strip-space elements="*"/>

<!--
8/Dec/1999 DW

Comments are
now surrounded by PRE tags - preserves whitespace, but puts in a lot of
blank lines above
and below the comment

When script done, remove all whitespace (including carriage returns)
between the attributes 
part of the * template and the closing > of the opening tag a few lines
below (just after 
xsl:choose). Code will be one long line & harder to read, but it will no
longer put a space 
between the last attribute value and the right angle bracket.

Does weird stuff when DOCTYPE is present. Try it with a WML doc, seems
to go and get 
the external DTD and then displays the comments.

-->


<!-- Root node -->
  <xsl:template match="/">
    <HTML>
    <HEAD>
    <TITLE>XML file</TITLE>
    </HEAD>
    <BODY>
	<NOSCRIPT>
    		<B>
        		You browser does not appear to support
JavaScript. To view this page properly, you must have JavaScript
enabled.
    		</B>
  	</NOSCRIPT>

	<SCRIPT LANGUAGE="JavaScript">
  	&lt;!-- Hide this script from browsers which do not support it.
    		function moreInfo(arg) {
      		window.alert(arg);
    		}
  		//  --&gt;
  	</SCRIPT>

	<I><FONT COLOR="888888">(View the source for any XML, doctype,
or namespace declarations)</FONT></I><br/><br/>

    <FONT FACE="Arial,Helvetica" SIZE="2">
	<xsl:for-each select="attribute::*"> 
		&nbsp;
		<FONT COLOR="008800"><xsl:value-of
select="name()"/></FONT>=&quot;<FONT COLOR="0000FF"><B><xsl:value-of
select="."/></B></FONT>&quot;
	</xsl:for-each>

	<xsl:apply-templates />
    </FONT>
    </BODY>
    </HTML>
  </xsl:template>

  <!-- All other elements -->
  <xsl:template match="*">
 	<!-- Start the opening tag, insert its name-->

	<!-- Create whitespace for every ancestor of node (indentation)
-->
	<xsl:for-each select="ancestor::*">&nbsp; &nbsp; &nbsp;
</xsl:for-each>
	&lt;<FONT COLOR="880000"><xsl:value-of select="name()"/></FONT>

	<xsl:variable name="ANCESTORS"><xsl:for-each
select="ancestor-or-self::*">/<xsl:value-of
select="name()"/>[<xsl:value-of select="generate-id()"/>]
</xsl:for-each></xsl:variable>
	<xsl:variable name="PARENTNAME"><xsl:value-of
select="name()"/></xsl:variable>

	<!-- If the tag has attributes, insert whitespace between
element name and first attribute name-->
	<xsl:if test="self::*[attribute::*]"><FONT>&nbsp;
</FONT></xsl:if>

 	<!-- Insert the attributes and their values-->
	<xsl:for-each select="attribute::*"> 
		<FONT COLOR="008800"><xsl:value-of
select="name()"/></FONT>=&quot;<FONT COLOR="0000FF"><B><A NAME=""
onclick="moreInfo('You have selected the {name()} attribute (attribute
number {position()}, at node [{generate-id()}]), of the {$PARENTNAME}
element ({$ANCESTORS}).')"><u><xsl:value-of
select="."/></u></A></B></FONT>&quot;
	</xsl:for-each>



	<xsl:choose>

		<!-- Element contains children, text or processing
instructions or comments -->
		<xsl:when
test="self::*[child::*|text()|processing-instruction()|comment()]">&gt;<
BR></BR>

			<xsl:apply-templates />

			<!-- Created the closing tag, indenting it -->

			<!-- Create whitespace for every ancestor of
node (indentation)	-->
			<xsl:for-each select="ancestor::*">&nbsp; &nbsp;
&nbsp; </xsl:for-each>
			&lt;/<FONT COLOR="880000"><xsl:value-of
select="name()"/></FONT>&gt;	
		</xsl:when>

		<!-- An empty element  -->
		<xsl:otherwise>
			/&gt;
		</xsl:otherwise>

	</xsl:choose>
	<BR></BR>
  </xsl:template> 

  <!-- Comments  -->
  <xsl:template match="comment()">
	<FONT COLOR="888888" SIZE="3"><PRE>&lt;!--<xsl:value-of
select="."/>--&gt;</PRE></FONT>
	<BR></BR>
  </xsl:template>

  <!-- Text  -->
  <xsl:template match="text()">
	<!-- Create whitespace for every ancestor of node (indentation)
-->
	<xsl:for-each select="ancestor::*">&nbsp; &nbsp; &nbsp;
</xsl:for-each>
	<xsl:variable name="ANCESTORS"><xsl:for-each
select="ancestor::*">/<xsl:value-of select="name()"/>[<xsl:value-of
select="generate-id()"/>] </xsl:for-each></xsl:variable>
	<xsl:variable name="PARENTNAME"><xsl:value-of
select="name(parent::*)"/></xsl:variable>

	<FONT COLOR="0000FF"><B><A NAME="" onclick="moreInfo('You have
selected the text (node [{generate-id()}]) in the {$PARENTNAME} element
({$ANCESTORS}).')"><u><xsl:value-of
select="."/></u></A></B></FONT><BR></BR>


  </xsl:template>

  <!-- Match the processing instructions - how do you match <?XML?> ?
-->
  <xsl:template match="processing-instruction()">
	<!-- Create whitespace for every ancestor of node (indentation)
-->
	<xsl:for-each select="ancestor::*">&nbsp; &nbsp; &nbsp;
</xsl:for-each>
	&lt;?<xsl:value-of select="name()"/> 
	<FONT>&nbsp; </FONT>
	<xsl:value-of select="."/>?&gt;
	<BR></BR>
  </xsl:template>

</xsl:stylesheet>





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


Current Thread