Cross Reference: /yui3/src/dataschema/docs/index.mustache
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<div class="intro">
<p>
The DataSchema Utility applies a given schema against data of arbitrary
formats, normalizing input such as JSON, XML, or delimited text into a
JavaScript object with known properties. The value of the DataSchema
Utility is in its ability to translate data from a variety of sources
into a consistent format for consumption by components in a predictable
manner.
</p>
</div>
{{>getting-started}}
<h2 id="using">Using the DataSchema</h2>
<p>This section describes how to use the DataSchema in further detail.</p>
<h3 id="basics">DataSchema basics</h3>
<p>
DataSchema classes are standalone static utilities that accept data input
plus a schema definition and return a JavaScript object with the following
properties:
</p>
<table>
<thead>
<tr>
<th>Property</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>`results`</td>
<td>Array</td>
<td>An array of data.</td>
</tr>
<tr>
<td>`meta`</td>
<td>Object</td>
<td>Arbitrary data values filtered from the input data.</td>
</tr>
</tbody>
</table>
<p>
Note that the schema you define will depend on which subclass of DataSchema
is being used.
</p>
<h4 id="array">DataSchema.Array</h4>
<p>
Use DataSchema.Array when working with JavaScript arrays. These arrays may
contain JavaScript objects, other arrays, or primitive values.
</p>
```
// A sample array of objects
[
{make:"Chevrolet",model:"Bel Air",year:1957},
{make:"Dodge",model:"Dart",year:1964},
{make:"Ford",model:"Mustang",year:1968}
];
// A sample array of arrays
[
["Chevrolet", "Bel Air", 1957],
["Dodge", "Dart", 1964],
["Ford", "Mustang", 1968]
];
// A sample array of primitives
[
"1957 Chevrolet Bel Air", "1964 Dodge Dart", "1968 Ford Mustang"
];
```
<p>Define a schema with the following properties for your array data:</p>
<table>
<thead>
<tr>
<th>Property</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>`resultFields`</td>
<td>Array</td>
<td>Keys to assign to the values contained in the array.</td>
</tr>
</tbody>
</table>
```
var mySchema = {
resultFields: [{key:"make"}, {key:"model"}, {key:"year"}]
};
// Returns an object with the properties "results" and "meta"
var myOutput = Y.DataSchema.Array.apply(mySchema, myData);
```
<h4 id="json">DataSchema.JSON</h4>
<p>
Use DataSchema.JSON when working with JavaScript objects or JSON data.
Typically, your data will hold meta values as well as an internal array of
tabular data.
</p>
```
// Sample JSON data
{
"profile":{
"current":160,
"target":150
},
"program": [
{
"category":"exercise",
"weekly schedule":[
{"day":"sunday", "activity":"swimming"},
{"day":"monday", "activity":"running"},
{"day":"tuesday", "activity":"biking"},
{"day":"wednesday", "activity":"running"},
{"day":"thursday", "activity":"swimming"},
{"day":"friday", "activity":"running"},
{"day":"saturday", "activity":"golf"}
]
}
]
};
```
<p>
Locators are string values in your schema that use dot notation or bracket
syntax to point to data values within the object. Define a schema with the
following properties for your object data:
</p>
<table>
<thead>
<tr>
<th>Property</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>`metaFields`</td>
<td>Object</td>
<td>Key/locator pairs that point to arbitrary data values.</td>
</tr>
<tr>
<td>`resultListLocator`</td>
<td>String</td>
<td>Locator to an internal array of tabular data.</td>
</tr>
<tr>
<td>`resultFields`</td>
<td>Array</td>
<td>Keys to assign to the values contained in the array.</td>
</tr>
</tbody>
</table>
```
var mySchema = {
metaFields: {current:"profile.current", target:"profile.target"},
resultListLocator: "program[0]['weekly schedule']",
resultFields: [{key:"day"}, {key:"activity"}]
};
// Returns an object with the properties "results" and "meta"
var myOutput = Y.DataSchema.JSON.apply(mySchema, myData);
```
<h4 id="xml">DataSchema.XML</h4>
<p>
<strong>Note:</strong> XML parsing currently has known issues on the
Android WebKit browser.
</p>
<p>
Use DataSchema.XML when working with XML data. As with JSON data, your XML
data may hold meta values as well as an internal node list of tabular
data.
</p>
```
// Sample XML data
<Response>
<Session>542235629</Session>
<Tracks start="1" count="10" total="98" errorCount="0"
defaultSort="popularity+" description="Top 100 Tracks"
name="Top 100 Tracks">
<Track id="59672468" rating="-1" title="I Kissed A Girl">
<Artist id="30326214" rating="-1">Katy Perry</Artist>
<ItemInfo><ChartPosition last="26" this="1"/></ItemInfo>
</Track>
<Track id="47973564" rating="-1" title="Shake It">
<Artist id="45575683" rating="-1">Metro Station</Artist>
<ItemInfo><ChartPosition last="27" this="2"/></ItemInfo>
</Track>
<Track id="52207363" rating="-1" title="Bleeding Love">
<Artist id="37956508" rating="-1">Leona Lewis</Artist>
<ItemInfo><ChartPosition last="28" this="3"/></ItemInfo>
</Track>
</Tracks>
</Response>
```
<p>
Locators are XPath string values in your schema that point to data values
within the XML. Define a schema with the following properties for your XML
data:
</p>
<table>
<thead>
<tr>
<th>Property</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>`metaFields`</td>
<td>Object</td>
<td>Key/locator pairs that point to arbitrary data values.</td>
</tr>
<tr>
<td>`resultListLocator`</td>
<td>String</td>
<td>Locator to an internal node list of tabular data.</td>
</tr>
<tr>
<td>`resultFields`</td>
<td>Array</td>
<td>Keys to assign to the values contained in the array. Locators may be defined to point to complex nested values or values held in attributes.</td>
</tr>
</tbody>
</table>
```
var mySchema = {
metaFields: {session:"//Session", total:"//Tracks/@total"},
resultListLocator: "Track", // node name or XPath
resultFields: [{key:"song", locator:"@title"}, {key:"artist", locator:"Artist"}, {key:"rank", locator:"ItemInfo/ChartPosition/@this"}]
};
// Returns an object with the properties "results" and "meta"
var myOutput = Y.DataSchema.XML.apply(mySchema, myData);
```
<h4 id="text">DataSchema.Text</h4>
<p>
Use DataSchema.Text when working with delimited textual data. Typically,
your data will not contain meta values.
</p>
```
// Sample text data
notebooks, 100, spiral-bound
pencils, 300, #2 erasers
pens, 500, blue ink
```
<p>Define a schema with the following properties for your text data:</p>
<table>
<thead>
<tr>
<th>Property</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>`resultDelimiter`</td>
<td>String</td>
<td>Delimiter separating each row of tabular data</td>
</tr>
<tr>
<td>`fieldDelimiter`</td>
<td>String</td>
<td>Delimiter separating each column of tabular data</td>
</tr>
<tr>
<td>`resultFields`</td>
<td>Array</td>
<td>Keys to assign to the values contained in each field (column).</td>
</tr>
</tbody>
</table>
```
var mySchema = {
resultDelimiter: "\\n",
fieldDelimiter: ",",
resultFields: [{key:"product"}, {key:"quantity"}, {key:"detail"}];
// Returns an object with the properties "results" and "meta"
var myOutput = Y.DataSchema.Text.apply(mySchema, myData);
```
<h3 id="plugin">DataSchema as a DataSource plugin</h3>
<p>
DataSchema plugins integrate DataSource's retrieval functionality with
schema-based normalization of the retrieved data for further consumption by
another component. There are currently four available DataSource plugins:
DataSourceArraySchema, DataSourceJSONSchema, DataSourceXMLSchema, and
DataSourceTextSchema.
</p>
```
myDataSource.plug({fn: Y.Plugin.DataSourceJSONSchema, cfg: {
schema: {
resultListLocator: "ResultSet.Result",
resultFields: ["Title"]
}
}});
// myCallback functions will receive the schema-normalized response object
myDataSource.sendRequest({
request: myRequest,
callback: myCallback
});
```
<h2 id="knownissues">Known Issues</h2>
<p>
<strong>Known Android issues (bugs 2529621, 2529758, 2529775):</strong> XML
parsing is currently buggy on the Android WebKit browser.
</p>