YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: DataTable Control: XML Data with XPath

# DataTable Control: XML Data with XPath

This DataTable is populated with XML data where the schema is defined using XPath-syntax locators. Please note that namespace support is not implemented at this time.

View example in new window.

When working with XML data, use the DataSource's `useXPath` property to enable XPath syntax in your locator strings.

## Sample Code for this Example

Data:

<myroot rootatt='5'> <top>topvalue</top> <second nested='nestedsecond' /> <allitems> <livehere> <item type='foo'> <name type='nametype0'>Abc</name> <rank>0</rank> <subitem> <name type='subnametype0'>subABC</name> <age>10</age> </subitem> </item> <item type='bar'> <name type='nametype1'>Def</name> <rank>1</rank> <subitem> <name type='subnametype1'>subDEF</name> <age>11</age> </subitem> </item> <item type='bat'> <name type='nametype2'>Ghi</name> <rank>2</rank> <subitem> <name type='subnametype2'>subGHI</name> <age>12</age> </subitem> </item> </livehere> </allitems> </myroot>

CSS:

/* No custom CSS. */

Markup:

<div id="xpath"></div>

JavaScript:

YAHOO.util.Event.addListener(window, "load", function() { YAHOO.example.XPath = function() { var myColumnDefs = [ {key:"type"}, {key:"rank"}, {key:"subnameatt"}, {key:"age"}, {key:"name"} ]; var myDataSource = new YAHOO.util.DataSource("assets/php/xml_proxy.php"); myDataSource.responseType = YAHOO.util.DataSource.TYPE_XML; myDataSource.useXPath = true; myDataSource.responseSchema = { metaFields: {rootatt:"/myroot/@rootatt", topnode:"//top", nestedatt:"//second/@nested"}, resultNode: "item", fields: [{key:"type", locator:"@type"}, {key:"rank", parser:"number"}, "name", {key:"subnameatt", locator:"subitem/name/@type"}, {key:"age", locator:"subitem/age", parser:"number"}] }; var myDataTable = new YAHOO.widget.DataTable("xpath", myColumnDefs, myDataSource); return { oDS: myDataSource, oDT: myDataTable }; }(); });

## Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

### YUI Logger Output:

Note: Logging and debugging is currently turned off for this example.

Reload with logging
and debugging enabled.

### DataTable Control Examples:

### More DataTable Control Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2010 Yahoo! Inc. All rights reserved.

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