forked from liyanage/webkit-xmlview-plugin
/
xml-pretty-print.xslt
138 lines (112 loc) · 5.39 KB
/
xml-pretty-print.xslt
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
137
138
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output
method="xml"
version="1.0"
encoding="utf-8"
indent="no"
doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
doctype-public="-//W3C//DTD XHTML 1.1//EN"
/>
<xsl:param name="web_resource_base"/>
<xsl:param name="user_css"/>
<xsl:param name="user_js"/>
<xsl:template match="/">
<html version="XHTML 1.1">
<head>
<style type='text/css'>
<xsl:copy-of select="$user_css"/>
</style>
<script type='text/javascript' src='{$web_resource_base}/prototype.js'/>
<script type='text/javascript'>
function xmlViewPluginSetup() {
document.fire("xmlviewplugin:loaded");
}
document.observe("dom:loaded", xmlViewPluginSetup);
</script>
<script type='text/javascript'>
<xsl:copy-of select="$user_js"/>
</script>
</head>
<body id='body'><xsl:apply-templates/></body>
</html>
</xsl:template>
<xsl:template match="comment()">
<div class='comment'><!--<xsl:value-of select="."/>--></div>
</xsl:template>
<xsl:template match="processing-instruction()">
<div class='xmlpi'><?<xsl:value-of select="name()"/><xsl:if test="string-length(.) > 0"><xsl:text> </xsl:text><xsl:value-of select="."/></xsl:if>?></div>
</xsl:template>
<xsl:template match="text()">
<span class='text'><xsl:copy/></span>
</xsl:template>
<!-- elements with mixed content -->
<xsl:template match="*[*|comment()|processing-instruction()]">
<xsl:variable name="lname" select="concat('name_', local-name())" />
<div class='element mixed {$lname}'>
<span class='tag open mixed {$lname}'><<xsl:value-of select="name()"/><span class='attributes_and_namespaces'><xsl:call-template name='namespaces'/><xsl:apply-templates select="@*"/></span>></span>
<div class='mixedcontent'>
<xsl:apply-templates/>
</div>
<span class='tag close mixed {$lname}'></<xsl:value-of select="name()"/>></span>
</div>
</xsl:template>
<!-- elements without mixed content -->
<xsl:template match="*">
<xsl:variable name="lname" select="concat('name_', local-name())" />
<div class='element nomixed {$lname}'>
<span class='tag open nomixed {$lname}'><<xsl:value-of select="name()"/><span class='attributes_and_namespaces'><xsl:call-template name='namespaces'/><xsl:apply-templates select="@*"/></span>></span><xsl:apply-templates/><span class='tag close nomixed {$lname}'></<xsl:value-of select="name()"/>></span>
</div>
</xsl:template>
<!-- empty elements -->
<xsl:template match="*[not(node())]">
<xsl:variable name="lname" select="concat('name_', local-name())" />
<div class='element selfclosed {$lname}'>
<span class='tag selfclosed {$lname}'><<xsl:value-of select="name()"/><span class='attributes_and_namespaces'><xsl:call-template name='namespaces'/><xsl:apply-templates select="@*"/></span>/></span>
</div>
</xsl:template>
<xsl:template match="@*">
<xsl:text> </xsl:text><span class='attribute name'><xsl:value-of select="name()"/></span>=<xsl:apply-templates select="." mode="attrvalue"/>
</xsl:template>
<!-- Try to emit well-formed markup for all single/double quote combinations in attribute values -->
<xsl:template match="@*[not(contains(., '"'))]" mode='attrvalue'>"<span class='attribute value'><xsl:value-of select="."/></span>"</xsl:template>
<xsl:template match='@*[not(contains(., "'"))]' mode='attrvalue'>'<span class='attribute value'><xsl:value-of select="."/></span>'</xsl:template>
<xsl:template match='@*[contains(., "'") and contains(., '"')]' mode='attrvalue'>"<span class='attribute value'>
<xsl:call-template name="replaceCharsInString">
<xsl:with-param name="stringIn" select="string(.)"/>
<xsl:with-param name="charsIn" select="'"'"/>
<xsl:with-param name="charsOut" select="'&quot;'"/>
</xsl:call-template>
</span>"</xsl:template>
<!-- Emit namespace declarations -->
<xsl:template name="namespaces">
<xsl:for-each select="@*|.">
<xsl:variable name="my_ns" select="namespace-uri()"/>
<!-- Emit a namespace declaration if this element or attribute has a namespace and no ancestor already defines it.
Currently this produces redundant declarations for namespaces used only on attributes. -->
<xsl:if test="$my_ns and not(ancestor::*[namespace-uri() = $my_ns])">
<xsl:variable name="prefix" select="substring-before(name(), local-name())"/>
<span class='namespace'> xmlns<xsl:if test="$prefix">:<xsl:value-of select="substring-before($prefix, ':')"/></xsl:if>='<xsl:value-of select="namespace-uri()"/>'</span>
</xsl:if>
</xsl:for-each>
</xsl:template>
<!-- string search/replace used in the attribute quote templates above. From http://www.dpawson.co.uk/xsl/sect2/replace.html -->
<xsl:template name="replaceCharsInString">
<xsl:param name="stringIn"/>
<xsl:param name="charsIn"/>
<xsl:param name="charsOut"/>
<xsl:choose>
<xsl:when test="contains($stringIn,$charsIn)">
<xsl:value-of select="concat(substring-before($stringIn,$charsIn),$charsOut)"/>
<xsl:call-template name="replaceCharsInString">
<xsl:with-param name="stringIn" select="substring-after($stringIn,$charsIn)"/>
<xsl:with-param name="charsIn" select="$charsIn"/>
<xsl:with-param name="charsOut" select="$charsOut"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$stringIn"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>