Search code examples
actionscript-3apache-flexe4x

Can E4X Get Attribute of a Parent Node Based on Attribute of a Child At Any Level?


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?


Solution

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

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