DataSchema Utility: DataSchema.XML for HTML Tables
DataSchema.XML can be used to retrieve data held in HTML TABLE elements.
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.
YUI().use("dataschema-xml", function(Y) { var tableEl = Y.Node.getDOMNode(Y.get("#simple")), schema = { // Each record is held in a TR resultListLocator: "tr", // Note that the XPath indexes are 1-based! resultFields: [ {key:"beverage", locator:"td[1]"}, {key:"price", locator:"td[2]"} ] }, data_out = Y.DataSchema.XML.apply(schema, tableEl)); alert(data_out); });
YUI().use("dataschema-xml", function(Y) { var tableEl = Y.Node.getDOMNode(Y.get("#simple")), schema = { // Each record is held in a TR resultListLocator: "tr", // Note that the XPath indexes are 1-based! resultFields: [ {key:"beverage", locator:"td[1]"}, {key:"price", locator:"td[2]"} ] }, data_out = Y.DataSchema.XML.apply(schema, tableEl)); alert(data_out); });
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
- Access the contents of the THEAD to generate our schema dynamically;
- and access only the TR elements contained in TBODY elements for data values, ignoring those TR elements in the THEAD.
YUI().use("dataschema-xml", function(Y) { // This function generates a schema based on contents of a TABLE element var getSchemaFromTableNode = function(tableNode) { var fields = [], // Each record is held in a TR schema = {resultListLocator:"tr"}, // Each field name is held in a TH thList = tableNode.queryAll("th"); // Generate field definitions based on TH thList.each(function(thNode, i){ // Note that the XPath indexes are 1-based! fields.push({key:thNode.get("text"), locator:"td["+(i+1)+"]"}); }); schema.resultFields = fields; return schema; }; var tableNode = Y.get("#complex"), // Generate schema dynamically schema = getSchemaFromTableNode(tableNode), // Create a temporary TBODY container to hold all data TRs tbody = document.createElement("tbody"), tbodyContainer = document.createDocumentFragment().appendChild(tbody); // Grab each TR in a TBODY but don't include TRs from the THEAD 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); });
YUI().use("dataschema-xml", function(Y) { // This function generates a schema based on contents of a TABLE element var getSchemaFromTableNode = function(tableNode) { var fields = [], // Each record is held in a TR schema = {resultListLocator:"tr"}, // Each field name is held in a TH thList = tableNode.queryAll("th"); // Generate field definitions based on TH thList.each(function(thNode, i){ // Note that the XPath indexes are 1-based! fields.push({key:thNode.get("text"), locator:"td["+(i+1)+"]"}); }); schema.resultFields = fields; return schema; }; var tableNode = Y.get("#complex"), // Generate schema dynamically schema = getSchemaFromTableNode(tableNode), // Create a temporary TBODY container to hold all data TRs tbody = document.createElement("tbody"), tbodyContainer = document.createDocumentFragment().appendChild(tbody); // Grab each TR in a TBODY but don't include TRs from the THEAD 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); });