<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>DataSchema</title>
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic">
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css">
<link rel="stylesheet" href="../assets/css/main.css">
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png">
<script src="../../build/yui/yui-min.js"></script>
</head>
<body>
<!--
<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>
-->
<div id="doc">
<div id="hd">
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1>
</div>
<a href="#toc" class="jump">Jump to Table of Contents</a>
<h1>DataSchema</h1>
<div class="yui3-g">
<div class="yui3-u-3-4">
<div id="main">
<div class="content"><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>
<h2 id="getting-started">Getting Started</h2>
<p>
To include the source files for DataSchema and its dependencies, first load
the YUI seed file if you haven't already loaded it.
</p>
<pre class="code prettyprint"><script src="http://yui.yahooapis.com/3.10.3/build/yui/yui-min.js"></script></pre>
<p>
Next, create a new YUI instance for your application and populate it with the
modules you need by specifying them as arguments to the <code>YUI().use()</code> method.
YUI will automatically load any dependencies required by the modules you
specify.
</p>
<pre class="code prettyprint"><script>
// Create a new YUI instance and populate it with the required modules.
YUI().use('dataschema', function (Y) {
// DataSchema is available and ready for use. Add implementation
// code here.
});
</script></pre>
<p>
For more information on creating YUI instances and on the
<a href="http://yuilibrary.com/yui/docs/api/classes/YUI.html#method_use"><code>use()</code> method</a>, see the
documentation for the <a href="../yui/index.html">YUI Global Object</a>.
</p>
<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><code>results</code></td>
<td>Array</td>
<td>An array of data.</td>
</tr>
<tr>
<td><code>meta</code></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>
<pre class="code prettyprint">// 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"
];</pre>
<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><code>resultFields</code></td>
<td>Array</td>
<td>Keys to assign to the values contained in the array.</td>
</tr>
</tbody>
</table>
<pre class="code prettyprint">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);</pre>
<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>
<pre class="code prettyprint">// 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"}
]
}
]
};</pre>
<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><code>metaFields</code></td>
<td>Object</td>
<td>Key/locator pairs that point to arbitrary data values.</td>
</tr>
<tr>
<td><code>resultListLocator</code></td>
<td>String</td>
<td>Locator to an internal array of tabular data.</td>
</tr>
<tr>
<td><code>resultFields</code></td>
<td>Array</td>
<td>Keys to assign to the values contained in the array.</td>
</tr>
</tbody>
</table>
<pre class="code prettyprint">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);</pre>
<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>
<pre class="code prettyprint">// 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></pre>
<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><code>metaFields</code></td>
<td>Object</td>
<td>Key/locator pairs that point to arbitrary data values.</td>
</tr>
<tr>
<td><code>resultListLocator</code></td>
<td>String</td>
<td>Locator to an internal node list of tabular data.</td>
</tr>
<tr>
<td><code>resultFields</code></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>
<pre class="code prettyprint">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);</pre>
<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>
<pre class="code prettyprint">// Sample text data
notebooks, 100, spiral-bound
pencils, 300, #2 erasers
pens, 500, blue ink</pre>
<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><code>resultDelimiter</code></td>
<td>String</td>
<td>Delimiter separating each row of tabular data</td>
</tr>
<tr>
<td><code>fieldDelimiter</code></td>
<td>String</td>
<td>Delimiter separating each column of tabular data</td>
</tr>
<tr>
<td><code>resultFields</code></td>
<td>Array</td>
<td>Keys to assign to the values contained in each field (column).</td>
</tr>
</tbody>
</table>
<pre class="code prettyprint">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);</pre>
<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>
<pre class="code prettyprint">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
});</pre>
<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>
</div>
</div>
</div>
<div class="yui3-u-1-4">
<div class="sidebar">
<div id="toc" class="sidebox">
<div class="hd">
<h2 class="no-toc">Table of Contents</h2>
</div>
<div class="bd">
<ul class="toc">
<li>
<a href="#getting-started">Getting Started</a>
</li>
<li>
<a href="#using">Using the DataSchema</a>
<ul class="toc">
<li>
<a href="#basics">DataSchema basics</a>
<ul class="toc">
<li>
<a href="#array">DataSchema.Array</a>
</li>
<li>
<a href="#json">DataSchema.JSON</a>
</li>
<li>
<a href="#xml">DataSchema.XML</a>
</li>
<li>
<a href="#text">DataSchema.Text</a>
</li>
</ul>
</li>
<li>
<a href="#plugin">DataSchema as a DataSource plugin</a>
</li>
</ul>
</li>
<li>
<a href="#knownissues">Known Issues</a>
</li>
</ul>
</div>
</div>
<div class="sidebox">
<div class="hd">
<h2 class="no-toc">Examples</h2>
</div>
<div class="bd">
<ul class="examples">
<li data-description="Schema parsing a JavaScript array.">
<a href="dataschema-array.html">DataSchema.Array</a>
</li>
<li data-description="Schema parsing JSON data.">
<a href="dataschema-json.html">DataSchema.JSON</a>
</li>
<li data-description="Schema parsing XML data.">
<a href="dataschema-xml.html">DataSchema.XML for XML Data</a>
</li>
<li data-description="Schema parsing data held in TABLE elements.">
<a href="dataschema-table.html">DataSchema.XML for HTML Tables</a>
</li>
<li data-description="Schema parsing delimited plain-text data.">
<a href="dataschema-text.html">DataSchema.Text</a>
</li>
<li data-description="Parsing data into specified types as the schema is being applied.">
<a href="dataschema-parsing.html">Enforcing DataTypes</a>
</li>
</ul>
</div>
</div>
<div class="sidebox">
<div class="hd">
<h2 class="no-toc">Examples That Use This Component</h2>
</div>
<div class="bd">
<ul class="examples">
<li data-description="The Local DataSource manages retrieval of in-page data, from JavaScript arrays and objects to DOM elements.">
<a href="../datasource/datasource-local.html">DataSource.Local</a>
</li>
<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.">
<a href="../datasource/datasource-get.html">DataSource.Get</a>
</li>
<li data-description="The IO DataSource manages retrieval of data from remote sources, via the IO Utility.">
<a href="../datasource/datasource-io.html">DataSource.IO</a>
</li>
<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">
<a href="../datasource/datasource-function.html">DataSource.Function</a>
</li>
<li data-description="Use the DataSourceCache plugin to enable caching and reduce server calls to remote sources.">
<a href="../datasource/datasource-caching.html">DataSource with Caching</a>
</li>
<li data-description="The DataSourceCache plugin supports offline caching so that cached data persists across browser sessions.">
<a href="../datasource/datasource-offlinecache.html">DataSource with Offline Cache</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>
<script>
YUI.Env.Tests = {
examples: [],
project: '../assets',
assets: '../assets/dataschema',
name: 'dataschema',
title: 'DataSchema',
newWindow: '',
auto: false
};
YUI.Env.Tests.examples.push('dataschema-array');
YUI.Env.Tests.examples.push('dataschema-json');
YUI.Env.Tests.examples.push('dataschema-xml');
YUI.Env.Tests.examples.push('dataschema-table');
YUI.Env.Tests.examples.push('dataschema-text');
YUI.Env.Tests.examples.push('dataschema-parsing');
YUI.Env.Tests.examples.push('datasource-local');
YUI.Env.Tests.examples.push('datasource-get');
YUI.Env.Tests.examples.push('datasource-io');
YUI.Env.Tests.examples.push('datasource-function');
YUI.Env.Tests.examples.push('datasource-caching');
YUI.Env.Tests.examples.push('datasource-offlinecache');
</script>
<script src="../assets/yui/test-runner.js"></script>
</body>
</html>