0a2e141e353f8a735c317f9defc22ff22fd4c8fa
[iot2.git] / tree-view.xsl
1 <?xml version="1.0" encoding="UTF-8"?>
2 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
3   <xsl:output method="html" 
4               encoding="UTF-8"
5               indent="no"/>
6
7   <xsl:variable name="apos">'</xsl:variable>
8
9   <xsl:template match="/">
10     <html>
11       <head>
12         <meta charset="UTF-8" />
13         <title>Parse-Tree</title>
14         <link type="text/css" rel="stylesheet" href="tree-view.css"/>
15       </head>
16       <body>
17         <h3>Parse-Tree</h3>
18         <xsl:apply-templates select="." mode="render"/>
19       </body>
20     </html>
21   </xsl:template>
22
23   <xsl:template match="/" mode="render">
24     <xsl:apply-templates mode="render"/>
25   </xsl:template>
26
27   <xsl:template match="*" mode="render">
28     <xsl:call-template name="ascii-art-hierarchy"/>
29     <br/>
30     <xsl:call-template name="ascii-art-hierarchy"/>
31     <span class='connector'>___</span>
32     <span class="element"><xsl:value-of select="local-name()"/></span>
33     <xsl:text>&#160;</xsl:text>
34     <br/>
35     <xsl:apply-templates select="@*" mode="render"/>
36     <xsl:apply-templates mode="render"/>
37   </xsl:template>
38
39   <xsl:template match="@*" mode="render">
40     <xsl:call-template name="ascii-art-hierarchy"/>
41     <span class='connector'>&#160;&#160;</span>
42     <span class='connector'>\___</span>
43
44     <xsl:text>&#160;</xsl:text>
45     <span class="name">
46       <xsl:value-of select="local-name()"/>
47     </span>
48     <xsl:text> = </xsl:text>
49     <span class="value">
50       <xsl:call-template name="escape-ws">
51         <xsl:with-param name="text" select="translate(.,' ','&#160;')"/>
52       </xsl:call-template>
53     </span>
54     <br/>
55   </xsl:template>
56
57   <xsl:template match="text()" mode="render">
58     <xsl:call-template name="ascii-art-hierarchy"/>
59     <br/>
60     <xsl:call-template name="ascii-art-hierarchy"/>
61     <span class='connector'>___</span>
62     <xsl:text>&#160;</xsl:text>
63     <span class="value">
64       <xsl:call-template name="escape-ws">
65         <xsl:with-param name="text" select="translate(.,' ','&#160;')"/>
66       </xsl:call-template>
67     </span>
68     <br/>
69   </xsl:template>
70
71   <xsl:template match="comment()" mode="render" />
72   <xsl:template match="processing-instruction()" mode="render" />
73
74
75   <xsl:template name="ascii-art-hierarchy">
76     <xsl:for-each select="ancestor::*">
77       <xsl:choose>
78         <xsl:when test="following-sibling::node()">
79           <span class='connector'>&#160;&#160;</span>|<span class='connector'>&#160;&#160;</span>
80           <xsl:text>&#160;</xsl:text>
81         </xsl:when>
82         <xsl:otherwise>
83           <span class='connector'>&#160;&#160;&#160;&#160;</span>
84           <span class='connector'>&#160;&#160;</span>
85         </xsl:otherwise>
86       </xsl:choose>
87     </xsl:for-each>
88     <xsl:choose>
89       <xsl:when test="parent::node() and ../child::node()">
90         <span class='connector'>&#160;&#160;</span>
91         <xsl:text>|</xsl:text>
92       </xsl:when>
93       <xsl:otherwise>
94         <span class='connector'>&#160;&#160;&#160;</span>
95       </xsl:otherwise>
96     </xsl:choose>
97   </xsl:template>
98
99   <!-- recursive template to escape linefeeds, tabs -->
100   <xsl:template name="escape-ws">
101     <xsl:param name="text"/>
102     <xsl:choose>
103       <xsl:when test="contains($text, '&#xA;')">
104         <xsl:call-template name="escape-ws">
105           <xsl:with-param name="text" select="substring-before($text, '&#xA;')"/>
106         </xsl:call-template>
107         <span class="escape">\n</span>
108         <xsl:call-template name="escape-ws">
109           <xsl:with-param name="text" select="substring-after($text, '&#xA;')"/>
110         </xsl:call-template>
111       </xsl:when>
112       <xsl:when test="contains($text, '&#x9;')">
113         <xsl:value-of select="substring-before($text, '&#x9;')"/>
114         <span class="escape">\t</span>
115         <xsl:call-template name="escape-ws">
116           <xsl:with-param name="text" select="substring-after($text, '&#x9;')"/>
117         </xsl:call-template>
118       </xsl:when>
119       <xsl:otherwise><xsl:value-of select="$text"/></xsl:otherwise>
120     </xsl:choose>
121   </xsl:template>
122
123 </xsl:stylesheet>