E4X可以根据任何级别的孩子的属性获取父节点的属性吗?

时间:2009-09-15 15:14:13

标签: actionscript-3 flex e4x

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

我想根据@type属性查找任何给定node subnode的{​​{1}}属性。例如,如果我的id为9,那么我想从上面返回type =“foo”。

@id

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

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

我可以理解为什么它不起作用。更有意义的是以下但语法失败(在AS3中):

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

如何做到这一点?

3 个答案:

答案 0 :(得分:5)

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

xml.node.(descendants("subnode").@id.contains("8")).@type;

答案 1 :(得分: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解决方案感兴趣。

答案 2 :(得分: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)
相关问题