-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example-textarea.html
87 lines (68 loc) · 2.13 KB
/
Example-textarea.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta encoding="utf-8">
<title>XSLT</title>
</head>
<body>
<form>
<label for="xslt-textarea">XSLT</label>
<textarea id="xslt-textarea" name="xslt-textarea" style="width:600px; height:400px">
Paste XSLT
</textarea>
<label for="xml-textarea">XML</label>
<textarea id="xml-textarea" name="xml-textarea" style="width:600px; height:400px">
Paste XML
</textarea>
<button id="transform" type="button">Transform</button>
</form>
<div id="example"></div>
<script type="text/javascript">
//Run when document has loaded
(function(){
function documentLoaded(){
if(!('XSLTProcessor' in window && 'DOMParser' in window && 'XMLSerializer' in window)){
console.log('XSLTProcessor, DOMParser and XMLSerializer do not appear to be available in this browser. Please try another.');
return;
}
const xsltProcessor = new XSLTProcessor();
const parser = new DOMParser();
const serializer = new XMLSerializer();
const button = document.getElementById("transform");
button.addEventListener("click", function(){
const xslttextarea = document.getElementById("xslt-textarea");
xsltstr = xslttextarea.value;
//TODO test > 0 length;
xsltDoc = parser.parseFromString(xsltstr, "application/xml");
console.log(xsltDoc);
//TODO test contentype, charset
let errorNode = xsltDoc.querySelector('parsererror');
if (errorNode) {
console.log(errorNode);
} else {
console.log("parsing ok");
}
const xmltextarea = document.getElementById("xml-textarea");
xmlstr = xmltextarea.value;
//TODO test > 0 length;
xmlDoc = parser.parseFromString(xmlstr, "application/xml");
console.log(xmlDoc);
//TODO test contentype, charset
errorNode = xmlDoc.querySelector('parsererror');
if (errorNode) {
console.log(errorNode);
} else {
console.log("parsing ok");
}
//XSLT Processor and import XSLT
xsltProcessor.importStylesheet(xsltDoc);
//now we can run the transformation and save to fragment
const fragment = xsltProcessor.transformToFragment(xmlDoc, document);
document.getElementById("example").appendChild(fragment);
}, false);
}
window.addEventListener("load", documentLoaded, false);
})();
</script>
</body>
</html>