as3 命名空间 - 获取一个带有减号的属性

发布于 2024-11-19 18:47:39 字数 1816 浏览 4 评论 0原文

可能的重复:
e4x / as3:如何访问名称中带有破折号的节点。

我已将 XML 的命名空间设置为使用 SMIL,并且可以通过以下方式提取元素的 src 属性:

my.node.@src 得到“这是某个 URL”

但是,我有另一个名为“系统比特率”的属性。由于减号,我无法执行 @system-bitrate 所以我尝试了我通常做的事情,即 my.node.attribute('system-bitrate')这是行不通的。

奇怪的是,甚至 my.node.attribute('src') 也不起作用。我怀疑这是由于命名空间造成的?那么如何使用 ny.node.attribute 获取属性呢?

唯一有效的是 my.node.attributes()[1]我知道这不是“正确的方法”,所以我希望有人能启发我!

仅供参考,我正在与SMIL 文件

** 编辑 **

这是我正在使用的 XML 所需的命名空间: default xml namespace = new Namespace('http://www.w3.org/2001/SMIL20/Language');

我正在使用的 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>

** 代码示例丹尼斯贾曼 **

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
    }
}

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
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没企图 2024-11-26 18:47:39

尝试使用方括号,如下例所示:

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']);

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']);
淡淡離愁欲言轉身 2024-11-26 18:47:39

看哪! QName 的力量!

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

关于属性(以及后代,子级...)的事情是它的参数是类型 * (匿名)。这是因为它实际上不是一个字符串,它在后台被强制转换为 QName(没有 URI)。这意味着您正在默认 URI 下搜索上述 URI 下的内容。

让我知道上面的代码是如何工作的。

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.

吹梦到西洲 2024-11-26 18:47:39

查看这篇文章:

e4x / as3:如何访问名称中带有破折号的节点

******编辑****:

并使用以下表示法获取包含 - (破折号)的 XML 属性

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

这些不起作用:

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

有关更多信息,请检查LiveDocs

干杯

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文