Project

General

Profile

1
<?xml version="1.0" encoding="utf-8"?>
2
<!--
3

    
4
ASCII XML Tree Viewer 1.0 (13 Feb 2001)
5
An XPath/XSLT visualisation tool for XML documents
6

    
7
Written by Jeni Tennison and Mike J. Brown
8
No license; use freely, but please credit the authors if republishing elsewhere.
9

    
10
Use this stylesheet to produce an ASCII art representation of an XML document's
11
node tree, as exposed by the XML parser and interpreted by the XSLT processor.
12
Note that the parser may not expose comments to the XSLT processor.
13

    
14
Usage notes
15
===========
16

    
17
By default, this stylesheet will not show namespace nodes. If the XSLT processor
18
supports the namespace axis and you want to see namespace nodes, just pass a
19
non-empty "show_ns" parameter to the stylesheet. Example using Instant Saxon:
20

    
21
    saxon somefile.xml ascii-treeview.xsl show_ns=yes
22

    
23
If you want to ignore whitespace-only text nodes, uncomment the xsl:strip-space
24
instruction below.
25

    
26
-->
27
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
28

    
29
<xsl:output method="html" />
30

    
31
<!-- uncomment the following to ignore whitespace-only text nodes -->
32
<!-- xsl:strip-space elements="*" -->
33

    
34
<!-- pass a non-empty show_ns parameter to the stylesheet to show namespace nodes -->
35
<xsl:param name="show_ns"/>
36

    
37
<xsl:variable name="apos">'</xsl:variable>
38

    
39
<xsl:template match="/">
40
    <xsl:apply-templates select="." mode="ascii-art" />
41
</xsl:template>
42

    
43
<xsl:template match="/" mode="ascii-art">
44
    <html><body><pre>
45
    <xsl:text>root&#xA;</xsl:text>
46
    <xsl:apply-templates mode="ascii-art" />
47
    </pre></body></html>
48
</xsl:template>
49

    
50
<xsl:template match="*" mode="ascii-art">
51
    <xsl:call-template name="ascii-art-hierarchy" />
52
    <xsl:text />___element '<xsl:value-of select="local-name()" />'<xsl:text />
53
    <xsl:if test="namespace-uri()"> in ns '<xsl:value-of select="namespace-uri()"/>' ('<xsl:value-of select="name()"/>')</xsl:if>
54
    <xsl:text>&#xA;</xsl:text>
55
    <xsl:apply-templates select="@*" mode="ascii-art" />
56
    <xsl:if test="$show_ns">
57
         <xsl:for-each select="namespace::*">
58
             <xsl:call-template name="ascii-art-hierarchy" />
59
             <xsl:text />  \___namespace '<xsl:value-of select="name()" />' = '<xsl:value-of select="." />'&#xA;<xsl:text />
60
         </xsl:for-each>
61
    </xsl:if>
62
    <xsl:apply-templates mode="ascii-art" />
63
</xsl:template>
64

    
65
<xsl:template match="@*" mode="ascii-art">
66
    <xsl:call-template name="ascii-art-hierarchy" />
67
    <xsl:text />  \___attribute '<xsl:value-of select="local-name()" />'<xsl:text />
68
    <xsl:if test="namespace-uri()"> in ns '<xsl:value-of select="namespace-uri()"/>' ('<xsl:value-of select="name()"/>')</xsl:if>
69
    <xsl:text /> = '<xsl:text />
70
    <xsl:call-template name="escape-ws">
71
        <xsl:with-param name="text" select="." />
72
    </xsl:call-template>
73
    <xsl:text />'&#xA;<xsl:text />
74
</xsl:template>
75

    
76
<xsl:template match="text()" mode="ascii-art">
77
    <xsl:call-template name="ascii-art-hierarchy" />
78
    <xsl:text>___text '</xsl:text>
79
    <xsl:call-template name="escape-ws">
80
        <xsl:with-param name="text" select="." />
81
    </xsl:call-template>
82
    <xsl:text>'&#xA;</xsl:text>
83
</xsl:template>
84

    
85
<xsl:template match="comment()" mode="ascii-art">
86
    <xsl:call-template name="ascii-art-hierarchy" />
87
    <xsl:text />___comment '<xsl:value-of select="." />'&#xA;<xsl:text />
88
</xsl:template>
89

    
90
<xsl:template match="processing-instruction()" mode="ascii-art">
91
    <xsl:call-template name="ascii-art-hierarchy" />
92
    <xsl:text />___processing instruction target='<xsl:value-of select="name()" />' instruction='<xsl:value-of select="." />'&#xA;<xsl:text />
93
</xsl:template>
94

    
95
<xsl:template name="ascii-art-hierarchy">
96
    <xsl:for-each select="ancestor::*">
97
        <xsl:choose>
98
            <xsl:when test="following-sibling::node()">  |   </xsl:when>
99
            <xsl:otherwise><xsl:text>      </xsl:text></xsl:otherwise>
100
        </xsl:choose>
101
    </xsl:for-each>
102
    <xsl:choose>
103
        <xsl:when test="parent::node() and ../child::node()">  |</xsl:when>
104
        <xsl:otherwise><xsl:text>   </xsl:text></xsl:otherwise>
105
    </xsl:choose>
106
</xsl:template>
107

    
108
<!-- recursive template to escape backslashes, apostrophes, newlines and tabs -->
109
<xsl:template name="escape-ws">
110
    <xsl:param name="text" />
111
    <xsl:choose>
112
        <xsl:when test="contains($text, '\')">
113
            <xsl:call-template name="escape-ws">
114
                <xsl:with-param name="text" select="substring-before($text, '\')" />
115
            </xsl:call-template>
116
            <xsl:text>\\</xsl:text>
117
            <xsl:call-template name="escape-ws">
118
                <xsl:with-param name="text" select="substring-after($text, '\')" />
119
            </xsl:call-template>
120
        </xsl:when>
121
        <xsl:when test="contains($text, $apos)">
122
            <xsl:call-template name="escape-ws">
123
                <xsl:with-param name="text" select="substring-before($text, $apos)" />
124
            </xsl:call-template>
125
            <xsl:text>\'</xsl:text>
126
            <xsl:call-template name="escape-ws">
127
                <xsl:with-param name="text" select="substring-after($text, $apos)" />
128
            </xsl:call-template>
129
        </xsl:when>
130
        <xsl:when test="contains($text, '&#xA;')">
131
            <xsl:call-template name="escape-ws">
132
                <xsl:with-param name="text" select="substring-before($text, '&#xA;')" />
133
            </xsl:call-template>
134
            <xsl:text>\n</xsl:text>
135
            <xsl:call-template name="escape-ws">
136
                <xsl:with-param name="text" select="substring-after($text, '&#xA;')" />
137
            </xsl:call-template>
138
        </xsl:when>
139
        <xsl:when test="contains($text, '&#x9;')">
140
            <xsl:value-of select="substring-before($text, '&#x9;')" />
141
            <xsl:text>\t</xsl:text>
142
            <xsl:call-template name="escape-ws">
143
                <xsl:with-param name="text" select="substring-after($text, '&#x9;')" />
144
            </xsl:call-template>
145
        </xsl:when>
146
        <xsl:otherwise><xsl:value-of select="$text" /></xsl:otherwise>
147
    </xsl:choose>
148
</xsl:template>
149

    
150
</xsl:stylesheet>
(3-3/26)