Creating a new XML object using namespaced elements

A

andyashton

How can I create a new XML object for processing using only elements
from a certain namespace? If I have a file like this:

<?xml version="1.0" encoding="UTF-8"?>
<sports xmlns:player="http://www.mlb.com/players">
<sport>Baseball</sport>
<league>MLB</league>
<player:player>
<player:name>Derek Jeter</player:name>
<player:posititon>Shortstop</player:position>
</player:player>
<player:player>
<player:name>Andy Pettite</player:name>
<player:posititon>Pitcher</player:position>
</player:player>
</sports>

I think I should be able to access those elements in the 'player'
namespace like this:

// parse with E4X
var p = new Namespace("http://www.mlb.com/players");
// why does this default namespace declaration not work!?
var xml = new XML(text);

if(xml.p::player.length()) {
var playersElements = xml.p::player;
} else {
var playersElements = [xml];
}

for each(var players in playersElements) {
....process those elements...
}

But it doesn't seem to work. Can anyone point out where I'm going
wrong?

Thanks!
 
M

Martin Honnen

andyashton said:
How can I create a new XML object for processing using only elements
from a certain namespace? If I have a file like this:

<?xml version="1.0" encoding="UTF-8"?>
<sports xmlns:player="http://www.mlb.com/players">
<sport>Baseball</sport>
<league>MLB</league>
<player:player>
<player:name>Derek Jeter</player:name>
<player:posititon>Shortstop</player:position>
^^^^^^^^^ ^^^^^^^^
</player:player>
<player:player>
<player:name>Andy Pettite</player:name>
<player:posititon>Pitcher</player:position>
^^^^^^^^^ ^^^^^^^^^

Your sample is not well-formed.
</player:player>
</sports>

If I correct that then the following example works well for me with
Firefox 2.0:

var xml = <sports xmlns:player="http://www.mlb.com/players">
<sport>Baseball</sport>
<league>MLB</league>
<player:player>
<player:name>Derek Jeter</player:name>
<player:position>Shortstop</player:position>
</player:player>
<player:player>
<player:name>Andy Pettite</player:name>
<player:position>Pitcher</player:position>
</player:player>
</sports>;

var p = new Namespace("http://www.mlb.com/players");

var playerElements = xml.p::player;

for each (var player in playerElements) {
alert(player.p::name);
}
 
A

andyashton

Thank you very much for taking the time to respond to my question.
Unfortunately I probably made a rookie mistake by trying to simplify
my real-life question with a cooked up example :). Along, the same
lines, here is the code I'm *actually* working with and I wonder if
anyone can give me some tips.

I have this snippet of an XML file:

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:mods="http://www.loc.gov/mods/v3">
<channel>
<link>http://www2.skidmore.edu/library/newbooks/newbooks.cfm?
select=Economics</link>
<title>New acquisitions for Economics</title>
<description>A New books feed for Economics</description>
<item>
<mods:mods version="3.0">
<mods:titleInfo>
<mods:nonSort>The </mods:nonSort>
<mods:title>Mormons</mods:title>
</mods:titleInfo>
...(and so forth)...
</mods:mods>
<title>The Mormons</title>
<link>http://foo</link>
</item>
</channel>
</rss>

and I would like to retrieve the <mods:title> using E4X:

// parse with E4X
var m = new Namespace("http://www.loc.gov/mods/v3");
var xml = new XML(text);

if(xml.m::mods.length()) {
var modsElements = xml.m::mods;
} else {
var modsElements = [xml];
}

for each(var mods in modsElements) {

// get each title
for each(var titleInfo in mods.m::titleInfo) {
if(titleInfo.@type != "abbreviated") {
alert(titleInfo.m::title);
}
}
}

....

Now I can verify that this code works with a regular MODS XML file,
like this:

<?xml version="1.0" encoding="UTF-8"?>
<modsCollection xmlns="http://www.loc.gov/mods/v3" xmlns:xsi="http://
www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://
www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/v3/mods-3-0.xsd">
<mods version="3.0">
<titleInfo>
<nonSort>The </nonSort>
<title>Mormons</title>
</titleInfo>
...(and so forth)...
</mods>
</modsCollection>

I thought, given how I understood namespace handling in E4X, that the
script snippet above should be able to retrieve the titleInfo.title
elements from either example, but that isn't the case. The <rss>
(first) example returns a blank value.

Can anyone tell me what I'm doing wrong? Thanks!
 
M

Martin Honnen

andyashton said:
I thought, given how I understood namespace handling in E4X, that the
script snippet above should be able to retrieve the titleInfo.title
elements from either example, but that isn't the case. The <rss>
(first) example returns a blank value.

Here is a working sample (tested with Firefox 2.0), accessing the
descendant mods element using '..' as follows:

var xml = <rss version="2.0" xmlns:mods="http://www.loc.gov/mods/v3">
<channel>
<link>http://www2.skidmore.edu/library/newbooks/newbooks.cfm?
select=Economics</link>
<title>New acquisitions for Economics</title>
<description>A New books feed for Economics</description>
<item>
<mods:mods version="3.0">
<mods:titleInfo>
<mods:nonSort>The </mods:nonSort>
<mods:title>Mormons</mods:title>
</mods:titleInfo>
...(and so forth)...
</mods:mods>
<title>The Mormons</title>
<link>http://foo</link>
</item>
</channel>
</rss>;

var m = new Namespace("http://www.loc.gov/mods/v3");

for each (var mods in xml..m::mods) {
for each (var titleInfo in mods.m::titleInfo) {
alert(titleInfo.m::title);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top