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
|
<pre>
|
45
|
<xsl:text>additionalMetadata
</xsl:text>
|
46
|
<xsl:apply-templates mode="ascii-art" />
|
47
|
</pre>
|
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>
</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="." />'
<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 />'
<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>'
</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="." />'
<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="." />'
<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="local-name()!='additionalMetadata'">
|
99
|
<xsl:choose>
|
100
|
<xsl:when test="following-sibling::node()"> | </xsl:when>
|
101
|
<xsl:otherwise><xsl:text> </xsl:text></xsl:otherwise>
|
102
|
</xsl:choose>
|
103
|
</xsl:when>
|
104
|
</xsl:choose>
|
105
|
</xsl:for-each>
|
106
|
<xsl:choose>
|
107
|
<xsl:when test="parent::node() and ../child::node()"> |</xsl:when>
|
108
|
<xsl:otherwise><xsl:text> </xsl:text></xsl:otherwise>
|
109
|
</xsl:choose>
|
110
|
</xsl:template>
|
111
|
|
112
|
<!-- recursive template to escape backslashes, apostrophes, newlines and tabs -->
|
113
|
<xsl:template name="escape-ws">
|
114
|
<xsl:param name="text" />
|
115
|
<xsl:choose>
|
116
|
<xsl:when test="contains($text, '\')">
|
117
|
<xsl:call-template name="escape-ws">
|
118
|
<xsl:with-param name="text" select="substring-before($text, '\')" />
|
119
|
</xsl:call-template>
|
120
|
<xsl:text>\\</xsl:text>
|
121
|
<xsl:call-template name="escape-ws">
|
122
|
<xsl:with-param name="text" select="substring-after($text, '\')" />
|
123
|
</xsl:call-template>
|
124
|
</xsl:when>
|
125
|
<xsl:when test="contains($text, $apos)">
|
126
|
<xsl:call-template name="escape-ws">
|
127
|
<xsl:with-param name="text" select="substring-before($text, $apos)" />
|
128
|
</xsl:call-template>
|
129
|
<xsl:text>\'</xsl:text>
|
130
|
<xsl:call-template name="escape-ws">
|
131
|
<xsl:with-param name="text" select="substring-after($text, $apos)" />
|
132
|
</xsl:call-template>
|
133
|
</xsl:when>
|
134
|
<xsl:when test="contains($text, '
')">
|
135
|
<xsl:call-template name="escape-ws">
|
136
|
<xsl:with-param name="text" select="substring-before($text, '
')" />
|
137
|
</xsl:call-template>
|
138
|
<xsl:text>\n</xsl:text>
|
139
|
<xsl:call-template name="escape-ws">
|
140
|
<xsl:with-param name="text" select="substring-after($text, '
')" />
|
141
|
</xsl:call-template>
|
142
|
</xsl:when>
|
143
|
<xsl:when test="contains($text, '	')">
|
144
|
<xsl:value-of select="substring-before($text, '	')" />
|
145
|
<xsl:text>\t</xsl:text>
|
146
|
<xsl:call-template name="escape-ws">
|
147
|
<xsl:with-param name="text" select="substring-after($text, '	')" />
|
148
|
</xsl:call-template>
|
149
|
</xsl:when>
|
150
|
<xsl:otherwise><xsl:value-of select="$text" /></xsl:otherwise>
|
151
|
</xsl:choose>
|
152
|
</xsl:template>
|
153
|
|
154
|
</xsl:stylesheet>
|