E4X能否根据任意级别子节点的属性获取父节点的属性?

发布于 2024-08-05 06:10:04 字数 872 浏览 7 评论 0原文

考虑这个带有“节点”的 XML 片段,它可以具有无限的子节点级别的“子节点”元素。

我想根据其 @id 属性查找任何给定 subnodenode@type 属性。例如,如果我的 id 为 9,那么我想从上面返回 type="foo"。

<xml>
    <node type="bar">
        <subnode id="4">
            <subnode id="5"/>
        </subnode>  
        <subnode id="6"/>
    </node>
    <node type="foo">
        <subnode id="7">
            <subnode id="8">
                <subnode id="9"/>
            </subnode>
        </subnode>
        <subnode id="10"/>
    </node>
</xml>

我提出了 E4X,但失败的是:

xml.node.(subnode.(@id == '8')).@type 

我可以看出为什么它不起作用。下面的内容更有意义,但语法失败(在 AS3 中):

xml.node.(..subnode.(@id == '8')).@type

如何做到这一点?

Consider this XML snippet with "nodes" which can have unlimited child levels of "subnode" elements.

I want to find @type attribute of the node for any given subnode, based on its @id attribute. For example, if I have an id of 9 then I want to return the type="foo" from above.

<xml>
    <node type="bar">
        <subnode id="4">
            <subnode id="5"/>
        </subnode>  
        <subnode id="6"/>
    </node>
    <node type="foo">
        <subnode id="7">
            <subnode id="8">
                <subnode id="9"/>
            </subnode>
        </subnode>
        <subnode id="10"/>
    </node>
</xml>

The E4X I have come up with, but which fails is:

xml.node.(subnode.(@id == '8')).@type 

I can kind of see why it doesn't work. What would make more sense is the following but the syntax fails (in AS3):

xml.node.(..subnode.(@id == '8')).@type

How can this be done?

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

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

发布评论

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

评论(3

清晨说晚安 2024-08-12 06:10:04

您应该能够使用此 E4X 获取类型值:

xml.node.(descendants("subnode")[email protected]("8")).@type;

You should be able to get the type value using this E4X:

xml.node.(descendants("subnode")[email protected]("8")).@type;
歌入人心 2024-08-12 06:10:04

尝试这个

for each(var node:XML in xml.node)
{
    var subnodes:XMLList = node..subnode;
    if(subnodes.(@id == '9').length() != 0)
        return node.@type;
}

编辑:即使这样也应该有效:

if(node..subnode.(@id == '9').length() != 0)

Try this

for each(var node:XML in xml.node)
{
    var subnodes:XMLList = node..subnode;
    if(subnodes.(@id == '9').length() != 0)
        return node.@type;
}

EDIT: Even this should work:

if(node..subnode.(@id == '9').length() != 0)
老旧海报 2024-08-12 06:10:04

放弃 E4X 后,我使用了“hack”并在 ActionScript 中完成了它。方法如下:

var p:XML = xml..subnode.(attribute('id').toLowerCase() === "8")[0];

//Traverse back up to the parent "node"           
while ( p.name().toString() === "subnode" ) {
    p = p.parent();
}

Alert.show(p.@type); //Should say "foo"

不过看起来很乱。仍然对任何简单的 E4X 解决方案感兴趣。

Having given up on E4X I used a "hack" and did it in ActionScript instead. Here's how:

var p:XML = xml..subnode.(attribute('id').toLowerCase() === "8")[0];

//Traverse back up to the parent "node"           
while ( p.name().toString() === "subnode" ) {
    p = p.parent();
}

Alert.show(p.@type); //Should say "foo"

Seems a mess though. Would still be interested in any plain E4X solution.

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