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

    
40
<xsl:template name="additionalmetadata">
41
    <right>
42
      <h3>Additional Metadata</h3>
43
    </right>
44
    <xsl:apply-templates mode="ascii-art" />
45
</xsl:template>
46

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

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

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

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

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

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

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

    
147
</xsl:stylesheet>
(4-4/30)