|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: DataSchema.Text</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.Text</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>DataSource.Text normalizes delimited text data against a given schema into an object with known properties.</p> |
|
|
30 |
</div> |
|
|
31 |
|
|
|
32 |
<div class="example yui3-skin-sam"> |
|
|
33 |
<style scope> |
|
|
34 |
/* custom styles for this example */ |
|
|
35 |
#demo .output {margin-bottom:1em; padding:10px; border:1px solid #D9D9D9;} |
|
|
36 |
</style> |
|
|
37 |
|
|
|
38 |
<form id="demo"> |
|
|
39 |
<h6>Data</h6> |
|
|
40 |
<pre> |
|
|
41 |
notebooks, 100, spiral-bound |
|
|
42 |
pencils, 300, #2 with erasers |
|
|
43 |
pens, 500, blue ink |
|
|
44 |
</pre> |
|
|
45 |
|
|
|
46 |
<h6>Schema</h6> |
|
|
47 |
<pre> |
|
|
48 |
{ |
|
|
49 |
resultDelimiter: "\n", |
|
|
50 |
fieldDelimiter: ",", |
|
|
51 |
resultFields: [{key:"product"}, {key:"quantity"}, {key:"detail"}] |
|
|
52 |
} |
|
|
53 |
</pre> |
|
|
54 |
|
|
|
55 |
<h6>Normalized data</h6> |
|
|
56 |
<input type="button" id="demo_apply" value="Apply schema =>"> |
|
|
57 |
<div id="demo_output" class="output"></div> |
|
|
58 |
</form> |
|
|
59 |
|
|
|
60 |
<script> |
|
|
61 |
YUI().use("dump", "node", "dataschema-text", function (Y) { |
|
|
62 |
Y.on("click", function(e){ |
|
|
63 |
var data_in = "notebooks, 100, spiral-bound\npencils, 300, #2 with erasers\npens, 500, blue ink\n", |
|
|
64 |
schema = { |
|
|
65 |
resultDelimiter: "\n", |
|
|
66 |
fieldDelimiter: ",", |
|
|
67 |
resultFields: [{key:"product"}, {key:"quantity"}, {key:"detail"}] // Or simply: ["product", "quantity", "detail"] |
|
|
68 |
}; |
|
|
69 |
Y.one("#demo_output").setHTML(Y.dump(Y.DataSchema.Text.apply(schema, data_in))); |
|
|
70 |
}, "#demo_apply"); |
|
|
71 |
}); |
|
|
72 |
</script> |
|
|
73 |
|
|
|
74 |
</div> |
|
|
75 |
|
|
|
76 |
<p>In order to use DataSchema.Text, input data must be delimited text. Define as your schema's <code>resultDelimiter</code> property the string that separates each result, and define as your schema's <code>fieldDelimiter</code> property the string that separates each field of each result.</p> |
|
|
77 |
|
|
|
78 |
<pre class="code prettyprint">YUI().use("dataschema-text", function(Y) { |
|
|
79 |
var data_in = "notebooks, 100, spiral-bound\\npencils, 300, #2 with erasers\\npens, 500, blue ink", |
|
|
80 |
schema = { |
|
|
81 |
resultDelimiter: "\\n", |
|
|
82 |
fieldDelimiter: ",", |
|
|
83 |
// Or simply: ["product", "quantity", "detail"] |
|
|
84 |
resultFields: [{key:"product"}, {key:"quantity"}, {key:"detail"}] |
|
|
85 |
}, |
|
|
86 |
data_out = Y.DataSchema.Text.apply(schema, data_in)); |
|
|
87 |
|
|
|
88 |
alert(data_out); |
|
|
89 |
});</pre> |
|
|
90 |
|
|
|
91 |
</div> |
|
|
92 |
</div> |
|
|
93 |
</div> |
|
|
94 |
|
|
|
95 |
<div class="yui3-u-1-4"> |
|
|
96 |
<div class="sidebar"> |
|
|
97 |
|
|
|
98 |
|
|
|
99 |
|
|
|
100 |
<div class="sidebox"> |
|
|
101 |
<div class="hd"> |
|
|
102 |
<h2 class="no-toc">Examples</h2> |
|
|
103 |
</div> |
|
|
104 |
|
|
|
105 |
<div class="bd"> |
|
|
106 |
<ul class="examples"> |
|
|
107 |
|
|
|
108 |
|
|
|
109 |
<li data-description="Schema parsing a JavaScript array."> |
|
|
110 |
<a href="dataschema-array.html">DataSchema.Array</a> |
|
|
111 |
</li> |
|
|
112 |
|
|
|
113 |
|
|
|
114 |
|
|
|
115 |
<li data-description="Schema parsing JSON data."> |
|
|
116 |
<a href="dataschema-json.html">DataSchema.JSON</a> |
|
|
117 |
</li> |
|
|
118 |
|
|
|
119 |
|
|
|
120 |
|
|
|
121 |
<li data-description="Schema parsing XML data."> |
|
|
122 |
<a href="dataschema-xml.html">DataSchema.XML for XML Data</a> |
|
|
123 |
</li> |
|
|
124 |
|
|
|
125 |
|
|
|
126 |
|
|
|
127 |
<li data-description="Schema parsing data held in TABLE elements."> |
|
|
128 |
<a href="dataschema-table.html">DataSchema.XML for HTML Tables</a> |
|
|
129 |
</li> |
|
|
130 |
|
|
|
131 |
|
|
|
132 |
|
|
|
133 |
<li data-description="Schema parsing delimited plain-text data."> |
|
|
134 |
<a href="dataschema-text.html">DataSchema.Text</a> |
|
|
135 |
</li> |
|
|
136 |
|
|
|
137 |
|
|
|
138 |
|
|
|
139 |
<li data-description="Parsing data into specified types as the schema is being applied."> |
|
|
140 |
<a href="dataschema-parsing.html">Enforcing DataTypes</a> |
|
|
141 |
</li> |
|
|
142 |
|
|
|
143 |
|
|
|
144 |
|
|
|
145 |
|
|
|
146 |
|
|
|
147 |
|
|
|
148 |
|
|
|
149 |
|
|
|
150 |
|
|
|
151 |
|
|
|
152 |
|
|
|
153 |
|
|
|
154 |
|
|
|
155 |
|
|
|
156 |
</ul> |
|
|
157 |
</div> |
|
|
158 |
</div> |
|
|
159 |
|
|
|
160 |
|
|
|
161 |
|
|
|
162 |
<div class="sidebox"> |
|
|
163 |
<div class="hd"> |
|
|
164 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
165 |
</div> |
|
|
166 |
|
|
|
167 |
<div class="bd"> |
|
|
168 |
<ul class="examples"> |
|
|
169 |
|
|
|
170 |
|
|
|
171 |
|
|
|
172 |
|
|
|
173 |
|
|
|
174 |
|
|
|
175 |
|
|
|
176 |
|
|
|
177 |
|
|
|
178 |
|
|
|
179 |
|
|
|
180 |
|
|
|
181 |
|
|
|
182 |
|
|
|
183 |
<li data-description="The Local DataSource manages retrieval of in-page data, from JavaScript arrays and objects to DOM elements."> |
|
|
184 |
<a href="../datasource/datasource-local.html">DataSource.Local</a> |
|
|
185 |
</li> |
|
|
186 |
|
|
|
187 |
|
|
|
188 |
|
|
|
189 |
<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."> |
|
|
190 |
<a href="../datasource/datasource-get.html">DataSource.Get</a> |
|
|
191 |
</li> |
|
|
192 |
|
|
|
193 |
|
|
|
194 |
|
|
|
195 |
<li data-description="The IO DataSource manages retrieval of data from remote sources, via the IO Utility."> |
|
|
196 |
<a href="../datasource/datasource-io.html">DataSource.IO</a> |
|
|
197 |
</li> |
|
|
198 |
|
|
|
199 |
|
|
|
200 |
|
|
|
201 |
<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"> |
|
|
202 |
<a href="../datasource/datasource-function.html">DataSource.Function</a> |
|
|
203 |
</li> |
|
|
204 |
|
|
|
205 |
|
|
|
206 |
|
|
|
207 |
<li data-description="Use the DataSourceCache plugin to enable caching and reduce server calls to remote sources."> |
|
|
208 |
<a href="../datasource/datasource-caching.html">DataSource with Caching</a> |
|
|
209 |
</li> |
|
|
210 |
|
|
|
211 |
|
|
|
212 |
|
|
|
213 |
<li data-description="The DataSourceCache plugin supports offline caching so that cached data persists across browser sessions."> |
|
|
214 |
<a href="../datasource/datasource-offlinecache.html">DataSource with Offline Cache</a> |
|
|
215 |
</li> |
|
|
216 |
|
|
|
217 |
|
|
|
218 |
</ul> |
|
|
219 |
</div> |
|
|
220 |
</div> |
|
|
221 |
|
|
|
222 |
</div> |
|
|
223 |
</div> |
|
|
224 |
</div> |
|
|
225 |
</div> |
|
|
226 |
|
|
|
227 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
228 |
<script>prettyPrint();</script> |
|
|
229 |
|
|
|
230 |
<script> |
|
|
231 |
YUI.Env.Tests = { |
|
|
232 |
examples: [], |
|
|
233 |
project: '../assets', |
|
|
234 |
assets: '../assets/dataschema', |
|
|
235 |
name: 'dataschema-text', |
|
|
236 |
title: 'DataSchema.Text', |
|
|
237 |
newWindow: '', |
|
|
238 |
auto: false |
|
|
239 |
}; |
|
|
240 |
YUI.Env.Tests.examples.push('dataschema-array'); |
|
|
241 |
YUI.Env.Tests.examples.push('dataschema-json'); |
|
|
242 |
YUI.Env.Tests.examples.push('dataschema-xml'); |
|
|
243 |
YUI.Env.Tests.examples.push('dataschema-table'); |
|
|
244 |
YUI.Env.Tests.examples.push('dataschema-text'); |
|
|
245 |
YUI.Env.Tests.examples.push('dataschema-parsing'); |
|
|
246 |
YUI.Env.Tests.examples.push('datasource-local'); |
|
|
247 |
YUI.Env.Tests.examples.push('datasource-get'); |
|
|
248 |
YUI.Env.Tests.examples.push('datasource-io'); |
|
|
249 |
YUI.Env.Tests.examples.push('datasource-function'); |
|
|
250 |
YUI.Env.Tests.examples.push('datasource-caching'); |
|
|
251 |
YUI.Env.Tests.examples.push('datasource-offlinecache'); |
|
|
252 |
|
|
|
253 |
</script> |
|
|
254 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
255 |
|
|
|
256 |
|
|
|
257 |
|
|
|
258 |
</body> |
|
|
259 |
</html> |