This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: treeViewer
- To: Mike Brown <mike at skew dot org>
- Subject: Re: [xsl] treeViewer
- From: Jeni Tennison <mail at jenitennison dot com>
- Date: Tue, 13 Feb 2001 10:55:35 +0000
- CC: xsl-list at lists dot mulberrytech dot com
- Organization: Jeni Tennison Consulting Ltd
- References: <200102122055.f1CKtZd03987@skew.org>
- Reply-To: xsl-list at lists dot mulberrytech dot com
Hi Mike,
> I really want to do like what someone else was asking about on the list
> here recently.. ASCII art versions just like I normally do by hand in my
> posts:
Here you are. I thought you'd probably want any whitespace to be
escaped with \n and \t, and whitespace to be stripped (easy to take
that instruction out if you don't). I've also made it go from the
root node rather than an element, though that could be changed.
<xsl:output method="text" />
<xsl:strip-space elements="*" />
<xsl:template match="/">
<xsl:apply-templates select="." mode="ascii-art" />
</xsl:template>
<xsl:template match="/" mode="ascii-art">
<xsl:text>root
</xsl:text>
<xsl:apply-templates mode="ascii-art" />
</xsl:template>
<xsl:template match="*" mode="ascii-art">
<xsl:call-template name="ascii-art-hierarchy" />
<xsl:text />___element '<xsl:value-of select="name()" />'
<xsl:text />
<xsl:apply-templates select="@*" mode="ascii-art" />
<xsl:for-each select="namespace::*">
<xsl:call-template name="ascii-art-hierarchy" />
<xsl:text /> \___namespace '<xsl:value-of select="name()" />' = '<xsl:value-of select="." />'
<xsl:text />
</xsl:for-each>
<xsl:apply-templates mode="ascii-art" />
</xsl:template>
<xsl:template match="@*" mode="ascii-art">
<xsl:call-template name="ascii-art-hierarchy" />
<xsl:text /> \___attribute '<xsl:value-of select="name()" />' = '<xsl:value-of select="." />'
<xsl:text />
</xsl:template>
<xsl:template match="text()" mode="ascii-art">
<xsl:call-template name="ascii-art-hierarchy" />
<xsl:text>___text '</xsl:text>
<xsl:call-template name="escape-ws">
<xsl:with-param name="text" select="." />
</xsl:call-template>
<xsl:text>'
</xsl:text>
</xsl:template>
<xsl:template match="comment()" mode="ascii-art">
<xsl:call-template name="ascii-art-hierarchy" />
<xsl:text />___comment '<xsl:value-of select="." />'
<xsl:text />
</xsl:template>
<xsl:template match="processing-instruction()" mode="ascii-art">
<xsl:call-template name="ascii-art-hierarchy" />
<xsl:text />___processing-instruction '<xsl:value-of select="name()" />' = '<xsl:value-of select="." />'
<xsl:text />
</xsl:template>
<xsl:template name="ascii-art-hierarchy">
<xsl:for-each select="ancestor::*">
<xsl:choose>
<xsl:when test="following-sibling::node()"> | </xsl:when>
<xsl:otherwise><xsl:text> </xsl:text></xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:if test="parent::node()"> |</xsl:if>
</xsl:template>
<xsl:template name="escape-ws">
<xsl:param name="text" />
<xsl:choose>
<xsl:when test="contains($text, '
')">
<xsl:call-template name="escape-ws">
<xsl:with-param name="text" select="substring-before($text, '
')" />
</xsl:call-template>
<xsl:text>\n</xsl:text>
<xsl:call-template name="escape-ws">
<xsl:with-param name="text" select="substring-after($text, '
')" />
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($text, '	')">
<xsl:value-of select="substring-before($text, '	')" />
<xsl:text>\t</xsl:text>
<xsl:call-template name="escape-ws">
<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>
I hope that helps,
Jeni
---
Jeni Tennison
http://www.jenitennison.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list