|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: DataSchema.XML for HTML Tables</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<h1>Example: DataSchema.XML for HTML Tables</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><div class="intro"> |
|
|
29 |
<p>DataSchema.XML can be used to retrieve data held in HTML TABLE elements.</p> |
|
|
30 |
</div> |
|
|
31 |
|
|
|
32 |
<div class="example yui3-skin-sam"> |
|
|
33 |
<style scoped> |
|
|
34 |
/* custom styles for this example */ |
|
|
35 |
#demo .output {margin-bottom:1em; padding:10px; border:1px solid #D9D9D9;} |
|
|
36 |
#complex tbody, #complex thead { |
|
|
37 |
border:3px solid #CCC; |
|
|
38 |
} |
|
|
39 |
</style> |
|
|
40 |
|
|
|
41 |
<form id="demo"> |
|
|
42 |
<h4>Simple Table</h4> |
|
|
43 |
<h6>Data</h6> |
|
|
44 |
|
|
|
45 |
<table id="simple"> |
|
|
46 |
<tr> |
|
|
47 |
<td>coffee</td> |
|
|
48 |
<td>1.25</td> |
|
|
49 |
</tr> |
|
|
50 |
<tr> |
|
|
51 |
<td>juice</td> |
|
|
52 |
<td>2.00</td> |
|
|
53 |
</tr> |
|
|
54 |
<tr> |
|
|
55 |
<td>tea</td> |
|
|
56 |
<td>1.25</td> |
|
|
57 |
</tr> |
|
|
58 |
<tr> |
|
|
59 |
<td>soda</td> |
|
|
60 |
<td>1.00</td> |
|
|
61 |
</tr> |
|
|
62 |
</table> |
|
|
63 |
|
|
|
64 |
|
|
|
65 |
<h6>Schema</h6> |
|
|
66 |
<pre> |
|
|
67 |
{ |
|
|
68 |
// Each record is held in a TR |
|
|
69 |
resultListLocator: "tr", |
|
|
70 |
// Note that the XPath indexes are 1-based! |
|
|
71 |
resultFields: [{key:"beverage", locator:"td[1]"}, {key:"price", locator:"td[2]"}] |
|
|
72 |
} |
|
|
73 |
</pre> |
|
|
74 |
|
|
|
75 |
<h6>Normalized data</h6> |
|
|
76 |
<input type="button" id="demo_apply_basic" value="Apply schema =>"> |
|
|
77 |
<div id="demo_output_basic" class="output"></div> |
|
|
78 |
|
|
|
79 |
<h4>Complex table</h4> |
|
|
80 |
<h6>Data</h6> |
|
|
81 |
|
|
|
82 |
<table id="complex"> |
|
|
83 |
<thead> |
|
|
84 |
<tr> |
|
|
85 |
<th>item</th> |
|
|
86 |
<th>price</th> |
|
|
87 |
</tr> |
|
|
88 |
</thead> |
|
|
89 |
<tbody> |
|
|
90 |
<tr> |
|
|
91 |
<td>hamburger</td> |
|
|
92 |
<td>4.00</td> |
|
|
93 |
</tr> |
|
|
94 |
<tr> |
|
|
95 |
<td>cheeseburger</td> |
|
|
96 |
<td>4.50</td> |
|
|
97 |
</tr> |
|
|
98 |
<tr> |
|
|
99 |
<td>veggie burger</td> |
|
|
100 |
<td>4.00</td> |
|
|
101 |
</tr> |
|
|
102 |
<tr> |
|
|
103 |
<td>salmon burger</td> |
|
|
104 |
<td>5.00</td> |
|
|
105 |
</tr> |
|
|
106 |
</tbody> |
|
|
107 |
<tbody> |
|
|
108 |
<tr> |
|
|
109 |
<td>french fries</td> |
|
|
110 |
<td>1.50</td> |
|
|
111 |
</tr> |
|
|
112 |
<tr> |
|
|
113 |
<td>onion rings</td> |
|
|
114 |
<td>2.00</td> |
|
|
115 |
</tr> |
|
|
116 |
<tr> |
|
|
117 |
<td>fruit salad</td> |
|
|
118 |
<td>2.50</td> |
|
|
119 |
</tr> |
|
|
120 |
<tr> |
|
|
121 |
<td>side salad</td> |
|
|
122 |
<td>2.00</td> |
|
|
123 |
</tr> |
|
|
124 |
</tbody> |
|
|
125 |
<tbody> |
|
|
126 |
<tr> |
|
|
127 |
<td>coffee</td> |
|
|
128 |
<td>1.25</td> |
|
|
129 |
</tr> |
|
|
130 |
<tr> |
|
|
131 |
<td>juice</td> |
|
|
132 |
<td>2.00</td> |
|
|
133 |
</tr> |
|
|
134 |
<tr> |
|
|
135 |
<td>tea</td> |
|
|
136 |
<td>1.25</td> |
|
|
137 |
</tr> |
|
|
138 |
<tr> |
|
|
139 |
<td>soda</td> |
|
|
140 |
<td>1.00</td> |
|
|
141 |
</tr> |
|
|
142 |
</tbody> |
|
|
143 |
</table> |
|
|
144 |
|
|
|
145 |
<h6>Schema</h6> |
|
|
146 |
<pre> |
|
|
147 |
// This schema is dynamically generated |
|
|
148 |
{ |
|
|
149 |
// Each record is held in a TR |
|
|
150 |
resultListLocator: "tr", |
|
|
151 |
// Note that the XPath indexes are 1-based! |
|
|
152 |
resultFields: [{key:"beverage", locator:"td[1]"}, {key:"price", locator:"td[2]"}] |
|
|
153 |
} |
|
|
154 |
</pre> |
|
|
155 |
|
|
|
156 |
<h6>Normalized data</h6> |
|
|
157 |
<input type="button" id="demo_apply_complex" value="Apply schema =>"> |
|
|
158 |
<div id="demo_output_complex" class="output"></div> |
|
|
159 |
</form> |
|
|
160 |
|
|
|
161 |
|
|
|
162 |
<script> |
|
|
163 |
YUI().use("dump", "node", "datatype-xml", "dataschema-xml", function (Y) { |
|
|
164 |
Y.on("click", function(e){ |
|
|
165 |
var tableEl = Y.Node.getDOMNode(Y.one("#simple")), |
|
|
166 |
schema = { |
|
|
167 |
// Each record is held in a TR |
|
|
168 |
resultListLocator: "tr", |
|
|
169 |
// Note that the XPath indexes are 1-based! |
|
|
170 |
resultFields: [{key:"beverage", locator:"td[1]"}, {key:"price", locator:"td[2]"}] |
|
|
171 |
}; |
|
|
172 |
Y.one("#demo_output_basic").setHTML(Y.dump(Y.DataSchema.XML.apply(schema, tableEl))); |
|
|
173 |
}, "#demo_apply_basic"); |
|
|
174 |
|
|
|
175 |
|
|
|
176 |
Y.on("click", function(e){ |
|
|
177 |
// This function generates a schema based on contents of a TABLE element |
|
|
178 |
var getSchemaFromTableNode = function(tableNode) { |
|
|
179 |
var fields = [], |
|
|
180 |
// Each record is held in a TR |
|
|
181 |
schema = {resultListLocator:"tr"}, |
|
|
182 |
// Each field name is held in a TH |
|
|
183 |
thList = tableNode.all("th"); |
|
|
184 |
|
|
|
185 |
// Generate field definitions based on TH |
|
|
186 |
thList.each(function(thNode, i){ |
|
|
187 |
// Note that the XPath indexes are 1-based! |
|
|
188 |
fields.push({key:thNode.get("text"), locator:"td["+(i+1)+"]"}); |
|
|
189 |
}); |
|
|
190 |
schema.resultFields = fields; |
|
|
191 |
return schema; |
|
|
192 |
}; |
|
|
193 |
|
|
|
194 |
var tableNode = Y.one("#complex"), |
|
|
195 |
// Generate schema dynamically |
|
|
196 |
schema = getSchemaFromTableNode(tableNode), |
|
|
197 |
// Create a temporary TBODY container to hold all data TRs |
|
|
198 |
tbody = document.createElement("tbody"), |
|
|
199 |
tbodyContainer = document.createDocumentFragment().appendChild(tbody); |
|
|
200 |
|
|
|
201 |
// Grab each TR in a TBODY but don't include TRs from the THEAD |
|
|
202 |
Y.all("#complex tbody tr").each(function(trNode, i){ |
|
|
203 |
// Cloning the TR keeps it in the document (optional) |
|
|
204 |
// Append each TR to the container |
|
|
205 |
tbodyContainer.appendChild(Y.Node.getDOMNode(trNode).cloneNode(true)); |
|
|
206 |
}) |
|
|
207 |
|
|
|
208 |
Y.one("#demo_output_complex").setHTML(Y.dump(Y.DataSchema.XML.apply(schema, tbodyContainer))); |
|
|
209 |
}, "#demo_apply_complex"); |
|
|
210 |
}); |
|
|
211 |
</script> |
|
|
212 |
|
|
|
213 |
</div> |
|
|
214 |
|
|
|
215 |
<p>DataSource.XML's <code>apply()</code> method supports passing in DOM nodes or document fragments. Use XPath strings to define data locations. In this example, we are retrieving data held in the rows of a TABLE element.</p> |
|
|
216 |
|
|
|
217 |
<pre class="code prettyprint">YUI().use("dataschema-xml", function(Y) { |
|
|
218 |
var tableEl = Y.Node.getDOMNode(Y.one("#simple")), |
|
|
219 |
schema = { |
|
|
220 |
// Each record is held in a TR |
|
|
221 |
resultListLocator: "tr", |
|
|
222 |
// Note that the XPath indexes are 1-based! |
|
|
223 |
resultFields: [ |
|
|
224 |
{key:"beverage", locator:"td[1]"}, |
|
|
225 |
{key:"price", locator:"td[2]"} |
|
|
226 |
] |
|
|
227 |
}, |
|
|
228 |
data_out = Y.DataSchema.XML.apply(schema, tableEl); |
|
|
229 |
|
|
|
230 |
alert(data_out); |
|
|
231 |
});</pre> |
|
|
232 |
|
|
|
233 |
|
|
|
234 |
<p>If the table has a THEAD element and/or multiple TBODY elements, special considerations must be taken to apply the schema to the appropriate collection of TR elements. In the following complex example we leverage the power of the Node API to</p> |
|
|
235 |
|
|
|
236 |
<ul> |
|
|
237 |
<li>Access the contents of the THEAD to generate our schema dynamically;</li> |
|
|
238 |
<li>and access only the TR elements contained in TBODY elements for data values, ignoring those TR elements in the THEAD.</li> |
|
|
239 |
</ul> |
|
|
240 |
|
|
|
241 |
<pre class="code prettyprint">YUI().use("dataschema-xml", function(Y) { |
|
|
242 |
// This function generates a schema based on contents of a TABLE element |
|
|
243 |
var getSchemaFromTableNode = function(tableNode) { |
|
|
244 |
var fields = [], |
|
|
245 |
// Each record is held in a TR |
|
|
246 |
schema = {resultListLocator:"tr"}, |
|
|
247 |
// Each field name is held in a TH |
|
|
248 |
thList = tableNode.all("th"); |
|
|
249 |
|
|
|
250 |
// Generate field definitions based on TH |
|
|
251 |
thList.each(function(thNode, i){ |
|
|
252 |
// Note that the XPath indexes are 1-based! |
|
|
253 |
fields.push({key:thNode.get("text"), locator:"td["+(i+1)+"]"}); |
|
|
254 |
}); |
|
|
255 |
schema.resultFields = fields; |
|
|
256 |
return schema; |
|
|
257 |
}; |
|
|
258 |
|
|
|
259 |
var tableNode = Y.one("#complex"), |
|
|
260 |
// Generate schema dynamically |
|
|
261 |
schema = getSchemaFromTableNode(tableNode), |
|
|
262 |
// Create a temporary TBODY container to hold all data TRs |
|
|
263 |
tbody = document.createElement("tbody"), |
|
|
264 |
tbodyContainer = document.createDocumentFragment().appendChild(tbody); |
|
|
265 |
|
|
|
266 |
// Grab each TR in a TBODY but don't include TRs from the THEAD |
|
|
267 |
Y.all("#complex tbody tr").each(function(trNode, i){ |
|
|
268 |
// Cloning the TR keeps it in the document (optional) |
|
|
269 |
// Append each TR to the container |
|
|
270 |
tbodyContainer.appendChild(Y.Node.getDOMNode(trNode).cloneNode(true)); |
|
|
271 |
}) |
|
|
272 |
|
|
|
273 |
var data_out = Y.DataSchema.XML.apply(schema, tbodyContainer); |
|
|
274 |
|
|
|
275 |
alert(data_out); |
|
|
276 |
});</pre> |
|
|
277 |
|
|
|
278 |
</div> |
|
|
279 |
</div> |
|
|
280 |
</div> |
|
|
281 |
|
|
|
282 |
<div class="yui3-u-1-4"> |
|
|
283 |
<div class="sidebar"> |
|
|
284 |
|
|
|
285 |
|
|
|
286 |
|
|
|
287 |
<div class="sidebox"> |
|
|
288 |
<div class="hd"> |
|
|
289 |
<h2 class="no-toc">Examples</h2> |
|
|
290 |
</div> |
|
|
291 |
|
|
|
292 |
<div class="bd"> |
|
|
293 |
<ul class="examples"> |
|
|
294 |
|
|
|
295 |
|
|
|
296 |
<li data-description="Schema parsing a JavaScript array."> |
|
|
297 |
<a href="dataschema-array.html">DataSchema.Array</a> |
|
|
298 |
</li> |
|
|
299 |
|
|
|
300 |
|
|
|
301 |
|
|
|
302 |
<li data-description="Schema parsing JSON data."> |
|
|
303 |
<a href="dataschema-json.html">DataSchema.JSON</a> |
|
|
304 |
</li> |
|
|
305 |
|
|
|
306 |
|
|
|
307 |
|
|
|
308 |
<li data-description="Schema parsing XML data."> |
|
|
309 |
<a href="dataschema-xml.html">DataSchema.XML for XML Data</a> |
|
|
310 |
</li> |
|
|
311 |
|
|
|
312 |
|
|
|
313 |
|
|
|
314 |
<li data-description="Schema parsing data held in TABLE elements."> |
|
|
315 |
<a href="dataschema-table.html">DataSchema.XML for HTML Tables</a> |
|
|
316 |
</li> |
|
|
317 |
|
|
|
318 |
|
|
|
319 |
|
|
|
320 |
<li data-description="Schema parsing delimited plain-text data."> |
|
|
321 |
<a href="dataschema-text.html">DataSchema.Text</a> |
|
|
322 |
</li> |
|
|
323 |
|
|
|
324 |
|
|
|
325 |
|
|
|
326 |
<li data-description="Parsing data into specified types as the schema is being applied."> |
|
|
327 |
<a href="dataschema-parsing.html">Enforcing DataTypes</a> |
|
|
328 |
</li> |
|
|
329 |
|
|
|
330 |
|
|
|
331 |
|
|
|
332 |
|
|
|
333 |
|
|
|
334 |
|
|
|
335 |
|
|
|
336 |
|
|
|
337 |
|
|
|
338 |
|
|
|
339 |
|
|
|
340 |
|
|
|
341 |
|
|
|
342 |
|
|
|
343 |
</ul> |
|
|
344 |
</div> |
|
|
345 |
</div> |
|
|
346 |
|
|
|
347 |
|
|
|
348 |
|
|
|
349 |
<div class="sidebox"> |
|
|
350 |
<div class="hd"> |
|
|
351 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
352 |
</div> |
|
|
353 |
|
|
|
354 |
<div class="bd"> |
|
|
355 |
<ul class="examples"> |
|
|
356 |
|
|
|
357 |
|
|
|
358 |
|
|
|
359 |
|
|
|
360 |
|
|
|
361 |
|
|
|
362 |
|
|
|
363 |
|
|
|
364 |
|
|
|
365 |
|
|
|
366 |
|
|
|
367 |
|
|
|
368 |
|
|
|
369 |
|
|
|
370 |
<li data-description="The Local DataSource manages retrieval of in-page data, from JavaScript arrays and objects to DOM elements."> |
|
|
371 |
<a href="../datasource/datasource-local.html">DataSource.Local</a> |
|
|
372 |
</li> |
|
|
373 |
|
|
|
374 |
|
|
|
375 |
|
|
|
376 |
<li data-description="The Get DataSource, which manages retrieval of data from remote sources via the Get Utility, can be useful for accessing data from cross-domain servers without the need for a proxy."> |
|
|
377 |
<a href="../datasource/datasource-get.html">DataSource.Get</a> |
|
|
378 |
</li> |
|
|
379 |
|
|
|
380 |
|
|
|
381 |
|
|
|
382 |
<li data-description="The IO DataSource manages retrieval of data from remote sources, via the IO Utility."> |
|
|
383 |
<a href="../datasource/datasource-io.html">DataSource.IO</a> |
|
|
384 |
</li> |
|
|
385 |
|
|
|
386 |
|
|
|
387 |
|
|
|
388 |
<li data-description="The Function DataSource, which manages retrieval of data from a JavaScript function, provides a highly customizeable mechanism for implementer-defined data retrieval algorithms"> |
|
|
389 |
<a href="../datasource/datasource-function.html">DataSource.Function</a> |
|
|
390 |
</li> |
|
|
391 |
|
|
|
392 |
|
|
|
393 |
|
|
|
394 |
<li data-description="Use the DataSourceCache plugin to enable caching and reduce server calls to remote sources."> |
|
|
395 |
<a href="../datasource/datasource-caching.html">DataSource with Caching</a> |
|
|
396 |
</li> |
|
|
397 |
|
|
|
398 |
|
|
|
399 |
|
|
|
400 |
<li data-description="The DataSourceCache plugin supports offline caching so that cached data persists across browser sessions."> |
|
|
401 |
<a href="../datasource/datasource-offlinecache.html">DataSource with Offline Cache</a> |
|
|
402 |
</li> |
|
|
403 |
|
|
|
404 |
|
|
|
405 |
</ul> |
|
|
406 |
</div> |
|
|
407 |
</div> |
|
|
408 |
|
|
|
409 |
</div> |
|
|
410 |
</div> |
|
|
411 |
</div> |
|
|
412 |
</div> |
|
|
413 |
|
|
|
414 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
415 |
<script>prettyPrint();</script> |
|
|
416 |
|
|
|
417 |
<script> |
|
|
418 |
YUI.Env.Tests = { |
|
|
419 |
examples: [], |
|
|
420 |
project: '../assets', |
|
|
421 |
assets: '../assets/dataschema', |
|
|
422 |
name: 'dataschema-table', |
|
|
423 |
title: 'DataSchema.XML for HTML Tables', |
|
|
424 |
newWindow: '', |
|
|
425 |
auto: false |
|
|
426 |
}; |
|
|
427 |
YUI.Env.Tests.examples.push('dataschema-array'); |
|
|
428 |
YUI.Env.Tests.examples.push('dataschema-json'); |
|
|
429 |
YUI.Env.Tests.examples.push('dataschema-xml'); |
|
|
430 |
YUI.Env.Tests.examples.push('dataschema-table'); |
|
|
431 |
YUI.Env.Tests.examples.push('dataschema-text'); |
|
|
432 |
YUI.Env.Tests.examples.push('dataschema-parsing'); |
|
|
433 |
YUI.Env.Tests.examples.push('datasource-local'); |
|
|
434 |
YUI.Env.Tests.examples.push('datasource-get'); |
|
|
435 |
YUI.Env.Tests.examples.push('datasource-io'); |
|
|
436 |
YUI.Env.Tests.examples.push('datasource-function'); |
|
|
437 |
YUI.Env.Tests.examples.push('datasource-caching'); |
|
|
438 |
YUI.Env.Tests.examples.push('datasource-offlinecache'); |
|
|
439 |
|
|
|
440 |
</script> |
|
|
441 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
442 |
|
|
|
443 |
|
|
|
444 |
|
|
|
445 |
</body> |
|
|
446 |
</html> |