forked from dnewcome/jath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
samples.html
193 lines (165 loc) · 5.99 KB
/
samples.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
<!doctype html>
<html>
<head>
<script src="jath.js" type="text/javascript" language="javascript"></script>
<script language="javascript">
/**
* Run samples as rudimentary functionality check. Later these
* will form the basis of a test suite.
*/
function runTests() {
/**
tests using XML without namespaces, or more correctly, with
every element in the null XML namespace
**/
var xmlDoc = getXmlDoc( "labels.xml" );
// test case with an array of simple objects
var template = [ "//label", { id: "@id", added: "@added" } ];
var parsed1 = Jath.parse( template, xmlDoc );
// test case with a array of nested objects
var template2 = [
"//label", {
id: "@id",
added: "@added",
address: {
street: "address/street",
city: "address/city"
}
}
];
var parsed2 = Jath.parse( template2, xmlDoc );
// test case with an array of arrays, the outermost of which
// simply appears at the output as a container and is not
// filled by iteration over the source XML data
var template3 = [
null,
[ "//label", "@id" ],
[ "//address", "province" ]
];
var parsed3 = Jath.parse( template3, xmlDoc );
/**
XML namespace tests
**/
var xmlDocNs = getXmlDoc( "labels-ns.xml" );
var xmlDocDefns = getXmlDoc( "labels-defns.xml" );
// what we were doing before won't work with namespaces
var template4 = [ "//label", { id: "@id", added: "@added" } ];
var parsed4 = Jath.parse( template4, xmlDocNs );
// prove that things don't work with default namespace
// however, in IE this works
var template5 = [ "//label", { id: "@id", added: "@added" } ];
var parsed5 = Jath.parse( template5, xmlDocDefns );
// using different type of xpath query - works with default ns
var template6 = [ "//*[namespace-uri()='http://example.com' and name()='label']", { id: "@id", added: "@added" } ];
var parsed6 = Jath.parse( template6, xmlDocDefns );
// work w/ ns prefixes
// note name() will be lbl:label, we need local-name() to refer to unprefixed element
var template7 = [ "//*[namespace-uri()='http://example.com/labelns' and local-name()='label']", { id: "@id", added: "@added" } ];
var parsed7 = Jath.parse( template7, xmlDocNs );
// work with default ns using resolver - you have to use prefix for
// resolver to get called (using 'foo'), but resolver doesn't use it.
setDefaultNSResolver( 'http://example.com' );
var template8 = [ "//foo:label", { id: "@id", added: "@added" } ];
var parsed8 = Jath.parse( template8, xmlDocDefns );
// using prefixes and our own resolver
setCustomResolver( { def: 'http://example.com', lbl: 'http://example.com/labelns' } );
var template9 = [ "//lbl:label", { id: "@id", added: "@added" } ];
var parsed9 = Jath.parse( template9, xmlDocNs );
// test 10 fails on Opera, not sure why yet
setDefaultResolver( xmlDocNs );
var template10 = [ "//lbl:label", { id: "@id", added: "@added" } ];
var parsed10 = Jath.parse( template10, xmlDocNs );
}
/**
* Set Jath up with custom resolver using given json object as the
* prefix lookup map - eg { prefix: "namespace", ... }
*/
function setCustomResolver( mappings ) {
Jath.resolver = function( prefix ) {
return mappings[ prefix ];
}
}
/**
* this resolver resolves a given default namespace. Not particularly
* useful since it always returns the same namespace for any given
* prefix. TODO: delete this function in lieu of extending
* setCustomResolver to return a default if the prefix is not found,
* which is effectively what this function is doing.
*/
function setDefaultNSResolver( namespace ) {
Jath.resolver = function() {
return namespace;
};
}
/**
* creates the 'default' Moz resolver - note does not
* resolve default namespaces though. All other prefix mappings will
* be automatically created however.
*/
function setDefaultResolver( xmldoc ) {
Jath.resolver = xmldoc.createNSResolver( xmldoc );
}
/**
* Load xml test data from disk
* NOTE: start chrome with --allow-file-access-from-files flag in order to
* allow reading the test data from disk
*/
function getXmlDoc( filename ) {
var xmlDoc;
if( browser == 'chrome' || browser == 'opera' || browser == 'safari' ) {
xmlDoc = getXmlDocChrome( filename );
}
else if( browser == 'msie' ) {
xmlDoc = getXmlDocIE( filename );
}
else if( browser == 'mozilla' ) {
xmlDoc = getXmlDocMozilla( filename );
}
// using mozilla as the default for now
else {
xmlDOc = getXmlDocMozilla( filename );
}
return xmlDoc;
}
function getXmlDocMozilla( filename ) {
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.async = false;
xmlDoc.load( filename );
return xmlDoc;
}
function getXmlDocChrome( filename ) {
var xhr = new window.XMLHttpRequest();
xhr.open( "GET", filename, false );
xhr.send( null );
return xhr.responseXML;
}
function getXmlDocIE( filename ) {
var xmlDoc = new ActiveXObject("MSXML2.DOMDocument.3.0");
xmlDoc.async = false;
xmlDoc.setProperty( "SelectionLanguage", "XPath" );
xmlDoc.resolveExternals = false;
xmlDoc.load( filename );
return xmlDoc;
}
/**
* Temp hack for doing browser check: TODO: implement proper
* capability checks.
*/
var browser = 'mozilla';
if( navigator.userAgent.toLowerCase().indexOf( 'chrome' ) > -1 ) {
browser = 'chrome';
}
else if( navigator.userAgent.toLowerCase().indexOf( 'opera' ) > -1 ) {
browser = 'opera';
}
else if( navigator.userAgent.toLowerCase().indexOf( 'safari' ) > -1 ) {
browser = 'safari';
}
else if( navigator.userAgent.toLowerCase().indexOf( 'msie' ) > -1 ) {
browser = 'msie';
}
</script>
</head>
<body onload="runTests();">
</body>
</html>