Re: [xsl] treeViewer

Subject: Re: [xsl] treeViewer
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Tue, 13 Feb 2001 10:55:35 +0000
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&#xA;</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()" />'&#xA;<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="." />'&#xA;<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="." />'&#xA;<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>'&#xA;</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="." />'&#xA;<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="." />'&#xA;<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, '&#xA;')">
         <xsl:call-template name="escape-ws">
            <xsl:with-param name="text" select="substring-before($text, '&#xA;')" />
         </xsl:call-template>
         <xsl:text>\n</xsl:text>
         <xsl:call-template name="escape-ws">
            <xsl:with-param name="text" select="substring-after($text, '&#xA;')" />
         </xsl:call-template>
      </xsl:when>
      <xsl:when test="contains($text, '&#x9;')">
         <xsl:value-of select="substring-before($text, '&#x9;')" />
         <xsl:text>\t</xsl:text>
         <xsl:call-template name="escape-ws">
            <xsl:with-param name="text" select="substring-after($text, '&#x9;')" />
         </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


Current Thread