forked from eXist-db/xsltforms-xar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
beautify.txs
136 lines (136 loc) · 5.35 KB
/
beautify.txs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<txs:scriptsheet xmlns:txs="http://www.agencexml.com/txs" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:stylesheet xmlns="http://www.w3.org/1999/xhtml" version="1.0" txs:name="main">
<xsl:variable name="start">                                                             </xsl:variable>
<xsl:template match="/">
<txs:return contenttype="application/xhtml+xml" format="xhtml">
<html xml:lang="en" lang="en">
<head>
<title>tXs Beautify Script</title>
<style type="text/css">
.tag {color:blue;}
.attrname {color:red;}
.equals {color:black;}
.attrvalue {color:green;}
.text {color:black;}
</style>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</txs:return>
</xsl:template>
<xsl:template match="*">
<xsl:param name="offset" select="0"/>
<xsl:if test="$offset != 0">
<br/>
</xsl:if>
<xsl:value-of select="substring($start,1,$offset * 3)"/>
<span class="tag">
<xsl:text><</xsl:text>
<xsl:value-of select="name()"/>
</span>
<xsl:variable name="parent" select=".."/>
<xsl:variable name="element" select="."/>
<xsl:choose>
<xsl:when test="namespace::*">
<xsl:for-each select="(namespace::* | @*/namespace::*)[name()!='xml']">
<xsl:variable name="prefix" select="name()"/>
<xsl:variable name="uri" select="."/>
<xsl:if test="(not($parent/namespace::*[name()=$prefix and . = $uri]) or $offset=0) and (($element|$element//*|$element//@*)[namespace-uri()=$uri])"> xmlns<xsl:if test="$prefix">:<xsl:value-of select="$prefix"/></xsl:if>="<xsl:value-of select="$uri"/>"</xsl:if>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="(. | @*)[name() != local-name()]"> xmlns:<xsl:value-of select="substring-before(name(),':')"/>="<xsl:value-of select="namespace-uri()"/>"</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates select="@*"/>
<xsl:choose>
<xsl:when test="count(node()[not(self::text())])=0 and text()">
<span class="tag">
<xsl:text>></xsl:text>
</span>
<xsl:apply-templates select="node()"/>
<span class="tag">
<xsl:text></</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>></xsl:text>
</span>
</xsl:when>
<xsl:when test="node()">
<span class="tag">
<xsl:text>></xsl:text>
</span>
<xsl:apply-templates select="node()"><xsl:with-param name="offset" select="$offset +1"/></xsl:apply-templates>
<br/>
<xsl:value-of select="substring($start,1,$offset * 3)"/>
<span class="tag">
<xsl:text></</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>></xsl:text>
</span>
</xsl:when>
<xsl:otherwise>
<span class="tag">
<xsl:text>/></xsl:text>
</span>
</xsl:otherwise>
</xsl:choose>
<xsl:text></xsl:text>
</xsl:template>
<xsl:template match="@*">
<xsl:text> </xsl:text>
<span class="attrname">
<xsl:value-of select="name()"/>
</span>
<span class="equals">
<xsl:text>=</xsl:text>
</span>
<span class="attrvalue">
<xsl:text>"</xsl:text>
<xsl:call-template name="escapeEntities"><xsl:with-param name="text" select="."/></xsl:call-template>
<xsl:text>"</xsl:text>
</span>
</xsl:template>
<xsl:template match="text()">
<xsl:param name="offset" select="0"/>
<xsl:if test="$offset != 0">
<br/>
</xsl:if>
<xsl:if test="normalize-space(.)!=''">
<xsl:value-of select="substring($start,1,$offset * 3)"/>
<span class="text">
<xsl:call-template name="escapeEntities">
<xsl:with-param name="text" select="normalize-space(.)"/>
</xsl:call-template>
</span>
</xsl:if>
</xsl:template>
<xsl:template name="escapeEntities" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="text"/>
<xsl:param name="done"/>
<xsl:param name="entities">&.&amp;.'.&apos;.<.&lt;.>.&gt;.".&quot;.</xsl:param>
<xsl:param name="entity" select="substring-before($entities,'.')"/>
<xsl:choose>
<xsl:when test="contains($text, $entity)">
<xsl:call-template name="escapeEntities">
<xsl:with-param name="text" select="substring-after($text,$entity)"/>
<xsl:with-param name="done" select="concat($done, substring-before($text,$entity), substring-before(substring-after($entities,'.'), '.'))"/>
<xsl:with-param name="entities" select="$entities"/>
<xsl:with-param name="entity" select="$entity"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="substring-after(substring-after($entities,'.'),'.') != ''">
<xsl:call-template name="escapeEntities">
<xsl:with-param name="text" select="concat($done, $text)"/>
<xsl:with-param name="entities" select="substring-after(substring-after($entities,'.'),'.')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat($done, $text)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
</txs:scriptsheet>