YUI 3.x Home -

YUI Library Examples: DataSchema Utility: DataSchema.XML for XML Data

DataSchema Utility: DataSchema.XML for XML Data

DataSchema.XML normalizes arbitrary XML data against a given schema into an object with known properties.

Basic example

Data
<root>
    <session>34637542</session>
    <category name="music" id="5"><results>
        <song id="59672468">
            <title>I Kissed A Girl</title>
            <rank>1</rank>
            <artist id="30326214">Katy Perry</artist>
        </song>
        <song id="47973564">
            <title>Shake It</title>
            <rank>2</rank>
            <artist id="45575683">Metro Station</artist>
        </song>
        <song id="52207363">
            <title>Bleeding Love</title>
            <rank>3</rank>
            <artist id="37956508">Leona Lewis</artist>
        </song>
    </results></category>
</root>
    
Schema
{
    resultListLocator: "song",
    resultFields: [{key:"title"}, {key:"artist"}, {key:"rank"}]
}
    
Normalized data

Complex example

Data
<Response>
    <Session>542235629</Session>
    <Tracks  start="1" count="10" total="98" errorCount="0"
        defaultSort="popularity+" description="Top 100 Tracks"
        name="Top 100 Tracks">
        <Track id="59672468" rating="-1" title="I Kissed A Girl">
            <Artist id="30326214" rating="-1">Katy Perry</Artist>
            <ItemInfo><ChartPosition last="26" this="1"/></ItemInfo>
        </Track>
        <Track id="47973564" rating="-1" title="Shake It">
            <Artist id="45575683" rating="-1">Metro Station</Artist>
            <ItemInfo><ChartPosition last="27" this="2"/></ItemInfo>
        </Track>
        <Track id="52207363" rating="-1" title="Bleeding Love">
            <Artist id="37956508" rating="-1">Leona Lewis</Artist>
            <ItemInfo><ChartPosition last="28" this="3"/></ItemInfo>
        </Track>
    </Tracks>
</Response>
        
Schema
{
    metaFields: {session:"//Session", total:"//Tracks/@total"},
    resultListLocator: "Track",
    resultFields: [{key:"song", locator:"@title"},
        {key:"artist", locator:"Artist"},
        {key:"rank", locator:"ItemInfo/ChartPosition/@this"}]
}
    
Normalized data

In order to use DataSchema.XML, input data must be an XML document.

  1. YUI().use("datatype-xml", "dataschema-xml", function(Y) {
  2. var data_in = Y.DataType.XML.parse('<root><session>34637542</session><category name="music" id="5"><results><song id="59672468"><title>I Kissed A Girl</title><rank>1</rank><artist id="30326214">Katy Perry</artist></song><song id="47973564"><title>Shake It</title><rank>2</rank><artist id="45575683">Metro Station</artist></song><song id="52207363"><title>Bleeding Love</title><rank>3</rank><artist id="37956508">Leona Lewis</artist></song></results></category></root>'),
  3. schema = {
  4. resultListLocator: "song",
  5. // Or simply ["title", "artist", "rank"]
  6. resultFields: [{key:"title"}, {key:"artist"}, {key:"rank"}]
  7. },
  8. data_out = Y.DataSchema.XML.apply(schema, data_in));
  9.  
  10. alert(data_out);
  11. });
YUI().use("datatype-xml", "dataschema-xml", function(Y) {
    var data_in = Y.DataType.XML.parse('<root><session>34637542</session><category name="music" id="5"><results><song id="59672468"><title>I Kissed A Girl</title><rank>1</rank><artist id="30326214">Katy Perry</artist></song><song id="47973564"><title>Shake It</title><rank>2</rank><artist id="45575683">Metro Station</artist></song><song id="52207363"><title>Bleeding Love</title><rank>3</rank><artist id="37956508">Leona Lewis</artist></song></results></category></root>'),
        schema = {
            resultListLocator: "song",
            // Or simply ["title", "artist", "rank"]
            resultFields: [{key:"title"}, {key:"artist"}, {key:"rank"}]
        },
        data_out = Y.DataSchema.XML.apply(schema, data_in));
 
    alert(data_out);
});

The data itself can get fairly complex, with deeply nested nodes and values held in attributes. In your schema, you can use XPath notation to define these locations.

  1. YUI().use("datatype-xml", "dataschema-xml", function(Y) {
  2. var data_in = Y.DataType.XML.parse('<Response><Session>542235629</Session><Tracks start="1" count="10" total="98" errorCount="0" defaultSort="popularity+" description="Top 100 Tracks" name="Top 100 Tracks" ><Track id="59672468" rating="-1" title="I Kissed A Girl"><Artist id="30326214" rating="-1">Katy Perry</Artist><ItemInfo><ChartPosition last="26" this="1"/></ItemInfo></Track><Track id="47973564" rating="-1" title="Shake It"><Artist id="45575683" rating="-1">Metro Station</Artist><ItemInfo><ChartPosition last="27" this="2"/></ItemInfo></Track><Track id="52207363" rating="-1" title="Bleeding Love"><Artist id="37956508" rating="-1">Leona Lewis</Artist><ItemInfo><ChartPosition last="28" this="3"/></ItemInfo></Track></Tracks></Response>'),
  3. schema = {
  4. metaFields: {session:"//Session", total:"//Tracks/@total"},
  5. resultListLocator: "Track",
  6. resultFields: [
  7. {key:"song", locator:"@title"},
  8. {key:"artist", locator:"Artist"},
  9. {key:"rank", locator:"ItemInfo/ChartPosition/@this"}
  10. ]
  11. },
  12. data_out = Y.DataSchema.XML.apply(schema, data_in));
  13.  
  14. alert(data_out);
  15. });
YUI().use("datatype-xml", "dataschema-xml", function(Y) {
    var data_in = Y.DataType.XML.parse('<Response><Session>542235629</Session><Tracks  start="1" count="10" total="98" errorCount="0"  defaultSort="popularity+"  description="Top 100 Tracks"  name="Top 100 Tracks"  ><Track id="59672468" rating="-1" title="I Kissed A Girl"><Artist id="30326214" rating="-1">Katy Perry</Artist><ItemInfo><ChartPosition last="26" this="1"/></ItemInfo></Track><Track id="47973564" rating="-1" title="Shake It"><Artist id="45575683" rating="-1">Metro Station</Artist><ItemInfo><ChartPosition last="27" this="2"/></ItemInfo></Track><Track id="52207363" rating="-1" title="Bleeding Love"><Artist id="37956508" rating="-1">Leona Lewis</Artist><ItemInfo><ChartPosition last="28" this="3"/></ItemInfo></Track></Tracks></Response>'),
        schema = {
            metaFields: {session:"//Session", total:"//Tracks/@total"},
            resultListLocator: "Track",
            resultFields: [
                {key:"song", locator:"@title"},
                {key:"artist", locator:"Artist"},
                {key:"rank", locator:"ItemInfo/ChartPosition/@this"}
            ]
        },
        data_out = Y.DataSchema.XML.apply(schema, data_in));
 
    alert(data_out);
});

Copyright © 2009 Yahoo! Inc. All rights reserved.

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