dataschema-table.mustache revision 11d2780adc5c17fefe1ea2d70c7e75ca6647c49c
e44e0ff3850931e7a79e4c86363f52990536f7bfDavid Lawrence<div class="intro">
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews <p>DataSchema.XML can be used to retrieve data held in HTML TABLE elements.</p>
499b34cea04a46823d003d4c0520c8b03e8513cbBrian Wellington</div>
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
e44e0ff3850931e7a79e4c86363f52990536f7bfDavid Lawrence<div class="example yui3-skin-sam">
e44e0ff3850931e7a79e4c86363f52990536f7bfDavid Lawrence {{>dataschema-table-source}}
e44e0ff3850931e7a79e4c86363f52990536f7bfDavid Lawrence</div>
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews<p>DataSource.XML's `apply()` 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>
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews```
dafcb997e390efa4423883dafd100c975c4095d6Mark AndrewsYUI().use("dataschema-xml", function(Y) {
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews var tableEl = Y.Node.getDOMNode(Y.one("#simple")),
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews schema = {
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews // Each record is held in a TR
e44e0ff3850931e7a79e4c86363f52990536f7bfDavid Lawrence resultListLocator: "tr",
e44e0ff3850931e7a79e4c86363f52990536f7bfDavid Lawrence // Note that the XPath indexes are 1-based!
50105afc551903541608b11851d73278b23579a3Mark Andrews resultFields: [
821644d49b73b49f2abc5463bc53a3132f612478Mark Andrews {key:"beverage", locator:"td[1]"},
821644d49b73b49f2abc5463bc53a3132f612478Mark Andrews {key:"price", locator:"td[2]"}
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence ]
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington },
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington data_out = Y.DataSchema.XML.apply(schema, tableEl);
f333ea9bdd3f85b74ae790e6c8ce2684295b3483Andreas Gustafsson
4f37905cc38162128a507e619e38ae535720686bAndreas Gustafsson alert(data_out);
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington});
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington```
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington
5a77e9620a0b2f7417469c98be374de49d0eccc6Andreas Gustafsson<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>
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington<ul>
50105afc551903541608b11851d73278b23579a3Mark Andrews <li>Access the contents of the THEAD to generate our schema dynamically;</li>
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington <li>and access only the TR elements contained in TBODY elements for data values, ignoring those TR elements in the THEAD.</li>
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington</ul>
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington```
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian WellingtonYUI().use("dataschema-xml", function(Y) {
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington // This function generates a schema based on contents of a TABLE element
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington var getSchemaFromTableNode = function(tableNode) {
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington var fields = [],
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington // Each record is held in a TR
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington schema = {resultListLocator:"tr"},
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington // Each field name is held in a TH
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington thList = tableNode.all("th");
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington // Generate field definitions based on TH
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington thList.each(function(thNode, i){
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington // Note that the XPath indexes are 1-based!
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington fields.push({key:thNode.get("text"), locator:"td["+(i+1)+"]"});
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington });
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington schema.resultFields = fields;
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington return schema;
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington };
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington var tableNode = Y.one("#complex"),
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington // Generate schema dynamically
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington schema = getSchemaFromTableNode(tableNode),
cc1eee4f5188a8174f10ab86da273fee50aa481aBrian Wellington // Create a temporary TBODY container to hold all data TRs
243ac4ecc1f45db7106c9166cd4187843507644cBrian Wellington tbody = document.createElement("tbody"),
243ac4ecc1f45db7106c9166cd4187843507644cBrian Wellington tbodyContainer = document.createDocumentFragment().appendChild(tbody);
243ac4ecc1f45db7106c9166cd4187843507644cBrian Wellington
243ac4ecc1f45db7106c9166cd4187843507644cBrian Wellington // Grab each TR in a TBODY but don't include TRs from the THEAD
243ac4ecc1f45db7106c9166cd4187843507644cBrian Wellington Y.all("#complex tbody tr").each(function(trNode, i){
// Cloning the TR keeps it in the document (optional)
// Append each TR to the container
tbodyContainer.appendChild(Y.Node.getDOMNode(trNode).cloneNode(true));
})
var data_out = Y.DataSchema.XML.apply(schema, tbodyContainer);
alert(data_out);
});
```