YUI 3.x Home -

YUI Library Examples: DataSchema Utility: DataSchema.XML for HTML Tables

DataSchema Utility: DataSchema.XML for HTML Tables

DataSchema.XML can be used to retrieve data held in HTML TABLE elements.

Simple Table

Data
coffee 1.25
juice 2.00
tea 1.25
soda 1.00
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]"}]
}
    
Normalized data

Complex table

Data
item price
hamburger 4.00
cheeseburger 4.50
veggie burger 4.00
salmon burger 5.00
french fries 1.50
onion rings 2.00
fruit salad 2.50
side salad 2.00
coffee 1.25
juice 2.00
tea 1.25
soda 1.00
Schema
// This schema is dynamically generated
{
    // 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]"}]
}
    
Normalized data

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.

  1. YUI().use("dataschema-xml", function(Y) {
  2. var tableEl = Y.Node.getDOMNode(Y.get("#simple")),
  3. schema = {
  4. // Each record is held in a TR
  5. resultListLocator: "tr",
  6. // Note that the XPath indexes are 1-based!
  7. resultFields: [
  8. {key:"beverage", locator:"td[1]"},
  9. {key:"price", locator:"td[2]"}
  10. ]
  11. },
  12. data_out = Y.DataSchema.XML.apply(schema, tableEl));
  13.  
  14. alert(data_out);
  15. });
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.
  1. YUI().use("dataschema-xml", function(Y) {
  2. // This function generates a schema based on contents of a TABLE element
  3. var getSchemaFromTableNode = function(tableNode) {
  4. var fields = [],
  5. // Each record is held in a TR
  6. schema = {resultListLocator:"tr"},
  7. // Each field name is held in a TH
  8. thList = tableNode.queryAll("th");
  9.  
  10. // Generate field definitions based on TH
  11. thList.each(function(thNode, i){
  12. // Note that the XPath indexes are 1-based!
  13. fields.push({key:thNode.get("text"), locator:"td["+(i+1)+"]"});
  14. });
  15. schema.resultFields = fields;
  16. return schema;
  17. };
  18.  
  19. var tableNode = Y.get("#complex"),
  20. // Generate schema dynamically
  21. schema = getSchemaFromTableNode(tableNode),
  22. // Create a temporary TBODY container to hold all data TRs
  23. tbody = document.createElement("tbody"),
  24. tbodyContainer = document.createDocumentFragment().appendChild(tbody);
  25.  
  26. // Grab each TR in a TBODY but don't include TRs from the THEAD
  27. Y.all("#complex tbody tr").each(function(trNode, i){
  28. // Cloning the TR keeps it in the document (optional)
  29. // Append each TR to the container
  30. tbodyContainer.appendChild(Y.Node.getDOMNode(trNode).cloneNode(true));
  31. })
  32.  
  33. var data_out = Y.DataSchema.XML.apply(schema, tbodyContainer));
  34.  
  35. alert(data_out);
  36. });
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);
});

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings