-
Notifications
You must be signed in to change notification settings - Fork 0
/
xslt.html
194 lines (188 loc) · 8.49 KB
/
xslt.html
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<html>
<head>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-38277515-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<meta charset="utf-8" />
<title>XSLT Online Realtime Transformer</title>
<!-- jquery - http://jqueryui.com/ -->
<link rel="stylesheet" href="jquery-ui-1.9.2.custom/css/ui-lightness/jquery-ui-1.9.2.custom.min.css" />
<script src="jquery-ui-1.9.2.custom/js/jquery-1.8.3.min.js"></script>
<script src="jquery-ui-1.9.2.custom/js/jquery-ui-1.9.2.custom.min.js"></script>
<!-- codemirror - http://codemirror.net/ -->
<link rel="stylesheet" href="codemirror-3.0/lib/codemirror.css">
<script src="codemirror-3.0/lib/codemirror.js"></script>
<script src="codemirror-3.0/mode/xml/xml.js"></script>
<script src="codemirror-3.0/lib/util/closetag.js"></script>
<script src="codemirror-3.0/lib/util/simple-hint.js"></script>
<link rel="stylesheet" href="codemirror-3.0/lib/util/simple-hint.css">
<script src="codemirror-3.0/lib/util/xml-hint.js"></script>
<style>
html,body{width:100%;height:100%;font-family:verdana;}
html,body,textarea,h1{margin:0;padding:0;}
h1{color:lightgrey;text-align:right;position:absolute;z-index:-1;font-size:56px;right:24px;}
.ui-resizable-se{bottom:15px;right:1px;}
.ui-wrapper{float:left;}
textarea{width:32%;height:60%;}
#prompt{padding:10px 0px 0px 10px;}
#container > div {padding:10px 0px 0px 10px;}
#errorbar{float:left;clear:left;padding:10px;}
#footer{position:fixed;bottom:4px;right:4px;text-align:center;font-size:10px;color:grey;}
.error{color:red;}
.CodeMirror{border:1px solid black;height:auto;}
.CodeMirror-scroll{overflow-y:hidden;overflow-x:auto;}
</style>
<script>
//setup before functions
var typingTimer; //timer identifier
var doneTypingInterval = 200; //time in ms, 5 second for example
/* Object, containing references to the code mirror text areas, by id */
var cm_config = {};
$(document).ready(function() {
CodeMirror.xmlHints['<'] = [
'xsl:apply-imports', 'xsl:apply-templates', 'xsl:attribute', 'xsl:attribute-set', 'xsl:call-template', 'xsl:choose', 'xsl:comment', 'xsl:copy', 'xsl:copy-of',
'xsl:decimal-format', 'xsl:element', 'xsl:fallback', 'xsl:for-each', 'xsl:if', 'xsl:import', 'xsl:include', 'xsl:key', 'xsl:message', 'xsl:namespace-alias',
'xsl:number', 'xsl:otherwise', 'xsl:output', 'xsl:param', 'xsl:preserve-space', 'xsl:processing-instruction', 'xsl:sort', 'xsl:strip-space', 'xsl:stylesheet',
'xsl:template', 'xsl:text', 'xsl:transform', 'xsl:value-of', 'xsl:variable', 'xsl:when', 'xsl:with-param'
];
CodeMirror.xmlHints['<xsl:apply-templates '] = ['select=""','mode=""',];
CodeMirror.xmlHints['<xsl:attribute '] = ['name=""','namespace=""',];
CodeMirror.xmlHints['<xsl:attribute-set '] = ['name=""','use-attribute-sets=""',];
CodeMirror.xmlHints['<xsl:call-template '] = ['name=""',];
CodeMirror.xmlHints['<xsl:copy '] = ['use-attribute-sets=""',];
CodeMirror.xmlHints['<xsl:copy-of '] = ['select=""',];
CodeMirror.xmlHints['<xsl:decimal-format '] = ['name=""','decimal-separator=""','grouping-separator=""','infinity=""','minus-sign=""','NaN=""','percent=""','per-mille=""','zero-digit=""','digit=""','pattern-separator=""',];
CodeMirror.xmlHints['<xsl:element '] = ['name=""','namespace=""','use-attribute-sets=""',];
CodeMirror.xmlHints['<xsl:for-each '] = ['select=""',];
CodeMirror.xmlHints['<xsl:if '] = ['test=""',];
CodeMirror.xmlHints['<xsl:import '] = ['href=""',];
CodeMirror.xmlHints['<xsl:include '] = ['href=""',];
CodeMirror.xmlHints['<xsl:key '] = ['name=""','match=""','use=""',];
CodeMirror.xmlHints['<xsl:message '] = ['terminate=""',];
CodeMirror.xmlHints['<xsl:namespace-alias '] = ['stylesheet-prefix=""','result-prefix=""',];
CodeMirror.xmlHints['<xsl:number '] = ['count=""','level=""','from=""','value=""','format=""','lang=""','letter-value=""','grouping-separator=""','grouping-size=""',];
CodeMirror.xmlHints['<xsl:output '] = ['method=""','version=""','encoding=""','omit-xml-declaration=""','standalone=""','doctype-public=""','doctype-system=""','cdata-section-elements=""','indent=""','media-type=""',];
CodeMirror.xmlHints['<xsl:param '] = ['name=""','select=""',];
CodeMirror.xmlHints['<xsl:preserve-space '] = ['elements=""',];
CodeMirror.xmlHints['<xsl:processing-instruction '] = ['name=""',];
CodeMirror.xmlHints['<xsl:sort '] = ['select=""','lang=""','data-type=""','order=""','case-order=""',];
CodeMirror.xmlHints['<xsl:strip-space '] = ['elements=""',];
CodeMirror.xmlHints['<xsl:stylesheet '] = ['version=""','extension-element-prefixes=""','exclude-result-prefixes=""','id=""',];
CodeMirror.xmlHints['<xsl:template '] = ['name=""','match=""','mode=""','priority=""',];
CodeMirror.xmlHints['<xsl:text '] = ['disable-output-escaping=""',];
CodeMirror.xmlHints['<xsl:transform '] = ['version=""','extension-element-prefixes=""','exclude-result-prefixes=""','id=""',];
CodeMirror.xmlHints['<xsl:value-of '] = ['select=""','disable-output-escaping=""',];
CodeMirror.xmlHints['<xsl:variable '] = ['name=""','select=""',];
CodeMirror.xmlHints['<xsl:when '] = ['test=""',];
CodeMirror.xmlHints['<xsl:with-param '] = ['name=""','select=""',];
var xslKeys = {
"'<'": function(cm) { CodeMirror.xmlHint(cm, '<'); },
"Ctrl-Space": function(cm) { CodeMirror.xmlHint(cm, ''); }
};
$("textarea").resizable({handles:"se"});
$("textarea").each(function(index,value){
var id = $(value).attr("id");
var extraKeyz = (id == "xslt" ? xslKeys : null);
cm_config[id] = CodeMirror.fromTextArea($(value).get(0),{
mode:'text/xml',
lineNumbers:true,
extraKeys: extraKeyz,
autoCloseTags:true,
onKeyEvent : function (editor, event) {
keyPressHandler(editor, $.event.fix(event));
}
});
$(value).attr("spellcheck","false");
$(value).parent().prepend($("<h1>").html(id));
});
doneTyping();
});
function keyPressHandler(editor, event){
if (event.type == "keyup"){
clearTimeout(typingTimer);
typingTimer = setTimeout(doneTyping, doneTypingInterval);
}
}
//user is "finished typing," do something
var count = 0;
function doneTyping(){
transform(cm_config.xml, cm_config.xslt);
}
function transform(xmlDom, xslDom){
if (document.implementation && document.implementation.createDocument) {
$("#errorbar").html("");
var xml = parseXmlDocument(xmlDom);
var xsl = parseXmlDocument(xslDom);
xsltProcessor=new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
var resultDocument = xsltProcessor.transformToFragment(xml,document);
if (resultDocument != null){
var tmp = document.createElement("div");
tmp.appendChild(resultDocument);
cm_config.output.setValue(tmp.innerHTML);
} else {
cm_config.output.setValue("Error: resultDocument is null.");
$(cm_config.output.getWrapperElement()).addClass("error");
}
} else {
cm_config.output.setValue("Cannot perform xslt transformation in this browser!");
}
}
function parseXmlDocument(xmlDom){
$(cm_config.output.getWrapperElement()).removeClass("error");
var doc = new DOMParser();
var xml = doc.parseFromString(xmlDom.getValue(),"text/xml");
var elems = xml.getElementsByTagName("parsererror");
if (elems.length>0) {
//$(xmlDom).addClass("error");
$("#errorbar").append(elems[0])
}
return xml;
}
</script>
</head>
<body>
<div id="prompt">Start typing in any of the boxes...</div>
<div id="container">
<textarea id="xml" autofocus="autofocus">
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
</book>
</bookstore>
</textarea>
<textarea id="xslt">
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
version="2.0"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<ol>
<li>
<xsl:value-of select="//title" />
</li>
<li>
<xsl:value-of select="fn:tokenize(//author,'\s')[3]" />
</li>
</ol>
</xsl:template>
</xsl:stylesheet>
</textarea>
<textarea id="output"></textarea>
</div>
<div id="errorbar"></div>
<div id="footer">Built for Firefox and Chrome.</div>
</body>
</html>