3

Possible Duplicate:
e4x / as3: How to access a node with a dash in its name.

I've set the namespace for my XML to use SMIL and I'm able to pull the src attribute of an element this way:

my.node.@src which gets "this is some URL"

However, I have another attr called 'system-bitrate'. Because of the minus sign, I can't do @system-bitrate So I attempted what I normally do which is my.node.attribute('system-bitrate') which isn't working.

Oddly enough, not even my.node.attribute('src') works. I suspect this is due to the namespace? So how to I get attributes out using ny.node.attribute ?

The only thing that works is my.node.attributes()[1]. I know that's not the "right way" so I'm hoping someone can enlighten me!

FYI I'm working with SMIL files

** edit **

Here's the namespace required for the XML I'm using: default xml namespace = new Namespace('http://www.w3.org/2001/SMIL20/Language');

And an example of the XML I'm working with:

<smil xmlns="http://www.w3.org/2001/SMIL20/Language">
  <head>
    <meta name="title" content="Live"/>
  </head>
  <body>
    <switch>
      <video src="myStreamName" system-bitrate="200000"/>
    </switch>
  </body>
</smil>

** code sample for DennisJaaman **

default xml namespace = new Namespace('http://www.w3.org/2001/SMIL20/Language');

var xml:XML = XML(event.target.data);
for each(var o:XML in xml.body['switch'].video) {
    if(!hs) hs = o;
    else {
        trace(o.attributes()[1]); // works
                trace(o.@url); // doesn't work either (makes me wonder about NS issues
                trace(o['@system-bitrate']); // doesn't work
                trace(o.attribute('@system-bitrate') // doesn't work
                // etc etc, I just left a few in here
    }
}
Community
  • 1
  • 1
Jacksonkr
  • 31,583
  • 39
  • 180
  • 284

3 Answers3

4

Try to use square brackets like in the sample below:

default xml namespace = new Namespace("http://www.w3.org/2001/SMIL20/Language");
var xmlSmpl:XML=<smil xmlns="http://www.w3.org/2001/SMIL20/Language">
  <head>
    <meta name="title" content="Live"/>
  </head>
  <body>
    <switch>
      <video src="myStreamName" system-bitrate="200000"/>
    </switch>
  </body>
</smil>;

trace (xmlSmpl.body['switch']['video']['@system-bitrate']);
DigitalD
  • 586
  • 3
  • 12
  • Unfortunately, you have to use the following namespace: `default xml namespace = new Namespace('http://www.w3.org/2001/SMIL20/Language');` – Jacksonkr Jul 13 '11 at 13:56
  • I have no idea why that worked, but it did (maybe it was my bad all along?). +1 & answer – Jacksonkr Jul 14 '11 at 23:13
2

Behold! The power of QName!

my.node.attribute( 
    new QName( 'http://www.w3.org/2001/SMIL20/Language', 'system-bitrate' ) 
)

The thing about attribute (and descendant, and child...) is that its parameter is type * (anonymously). This is because it really isn't a String, it is coerced to a QName (without a URI) in the background. This means you were searching under the default URI for something under the URI above.

Let me know how that code above works out.

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
0

Check out this post:

e4x / as3: How to access a node with a dash in its name

******EDIT****:

And use the following notation to get XML Attributes that contain a - (dash)

trace("Video system-bitrate: " + my.node.video["@system-bitrate"]);

These do not work:

trace("Video system-bitrate: " + my.node.video.@["system-bitrate"]);
trace("Video system-bitrate: " + my.node.video.attribute("@system-bitrate"));

For more info check the LiveDocs

Cheers

Community
  • 1
  • 1
Dennis Jaamann
  • 3,547
  • 2
  • 23
  • 42
  • They're trying to grab a node whereas I can get the node, just not the attribute :( – Jacksonkr Jul 13 '11 at 14:58
  • Well, it is still the same principle, please check my edited post – Dennis Jaamann Jul 13 '11 at 16:13
  • I tried all those, and for some reason I'm only able to get anything out of `my.node.video.attributes()[1]` which seems so bizarre to me. I would think any of the ways you listed would work too. I'll post my code in an additional edit on my original question. – Jacksonkr Jul 13 '11 at 17:03