views:

1230

answers:

3

Hi,

I'm struggling with some E4X and hope you can help.

Here's my XML structure:

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

As you can see there are multiple "nodes" which can have unlimited child levels of "subnode" elements.

What I want to do is 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.

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

Anybody?

Jake

A: 

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.

Jake

Jake Howlett
A: 

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)
Amarghosh
Thanks Amargosh. Really looking for a pure one-liner E4X solution though
Jake Howlett
+3  A: 

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

xml.node.(descendants("subnode")[email protected]("8")).@type;
Niko Nyman
Looks promising Nico, but it tells me that "value is not a function" when I add that line of code. Any idea?
Jake Howlett
Further to that it seems to be the descendants() part which is causing the error.
Jake Howlett
I tested this line of code by copy pasting your XML in my test document, and it worked. Can you post the exact error message you're getting?
Niko Nyman
You're right. I tested it with the XML I posted and it *did* work. Must be something weird about my actual XML. Will look in to it. Thanks for the answer!! I knew there was a way to do it...
Jake Howlett
Got it to work with my real XML now. No idea what I did wrong first time.
Jake Howlett
Great! I've noticed it helps to think that when doing such conditional access stuff using E4X, the code inside the parentheses must return a Boolean. My usual mistake is trying to put something inside the parentheses that would return an XMLList.
Niko Nyman
Aha, that explains it Niko. Thanks - you're a star.
Jake Howlett
@Niko Why doesn't `xml.node.(descendants("subnode").@id == "8").@type;` work in this case? It seems to be doing the same thing, isn't it?
Amarghosh
That prints nothing
Amarghosh
It's a good question. I tried that first and also noticed that it needs `contains()` for some reason.
Niko Nyman