4

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

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

<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

如何才能做到这一点?

4

3 回答 3

5

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

xml.node.(descendants("subnode").@id.contains("8")).@type;
于 2009-09-16T21:05:52.643 回答
0

在放弃了 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 解决方案感兴趣。

于 2009-09-16T08:48:57.350 回答
0

试试这个

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)
于 2009-09-16T09:04:21.783 回答