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
      <h3>Additional Metadata</h3>
42
    <pre>
43
    <xsl:text>additionalMetadata&#xA;</xsl:text>
44
    <xsl:apply-templates mode="ascii-art" />
45
    </pre>
46
</xsl:template>
47

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

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

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

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

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

    
93
<xsl:template name="ascii-art-hierarchy">
94
    <xsl:for-each select="ancestor::*">
95
      <xsl:choose>
96
        <xsl:when test="local-name()!='additionalMetadata'">
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:when>
102
      </xsl:choose>
103
    </xsl:for-each>
104
    <xsl:choose>
105
        <xsl:when test="parent::node() and ../child::node()">  |</xsl:when>
106
        <xsl:otherwise><xsl:text>   </xsl:text></xsl:otherwise>
107
    </xsl:choose>
108
</xsl:template>
109

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

    
152
</xsl:stylesheet>
(2-2/27)