-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example-FileReaderInputs.html
98 lines (74 loc) · 2.05 KB
/
Example-FileReaderInputs.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta encoding="utf-8">
<title>XSLT</title>
</head>
<body>
<!--XSLT file-->
<label>XSLT file</label>
<input type="file" id="xsltfileload">
<!--XML file-->
<label>XML file</label>
<input type="file" id="xmlfileload">
<div id="output"></div>
<script type="text/javascript">
(function(){
function documentLoaded(){
if(!('XSLTProcessor' in window && 'DOMParser' in window)){
console.log('XSLTProcessor does not appear to be available in this browser. Please try another.');
return;
}
const xsltProcessor = new XSLTProcessor();
const parser = new DOMParser();
//output
const output = document.getElementById("output");
//handle xsltfile
//application/xslt+xml
const xsltfile = document.getElementById("xsltfileload");
xsltfile.addEventListener("change", function(){
const file = this.files[0];
if(file.type !== "application/xslt+xml"){
console.log("file is not an XSLT file");
return;
}
const reader = new FileReader();
reader.readAsText(file);
//load
reader.addEventListener("load", function(){
let xsl = parser.parseFromString(reader.result, "application/xml");
xsltProcessor.importStylesheet(xsl);
}, false);
//error
reader.addEventListener("error", function(){
console.log("error");
}, false);
}, false);
//handle xmlfile
//text/xml
const xmlfile = document.getElementById("xmlfileload");
xmlfile.addEventListener("change", function(){
const file = this.files[0];
if(file.type !== "text/xml"){
console.log("file is not an XML file");
return;
}
const reader = new FileReader();
reader.readAsText(file);
//load
reader.addEventListener("load", function(){
const xml = parser.parseFromString(reader.result, "application/xml");
const fragment = xsltProcessor.transformToFragment(xml, document);
output.appendChild(fragment);
}, false);
//error
reader.addEventListener("error", function(){
console.log("error");
}, false);
}, false);
}
window.addEventListener("load", documentLoaded, false);
}) ();
</script>
</body>
</html>