Conditional selections based on sibling values in E4X
This may be obvious to others but I found it a bit counter intuitive and thought I would post a note as much for my own reference as for others. Consider the following code:
-
var x:XML =
-
<Plans>
-
<Plan>
-
<Industry>MFG</Industry>
-
<Contact>Bob Cratchett</Contact>
-
<PlanNumber>11111</PlanNumber>
-
</Plan>
-
<Plan>
-
<Industry>AEROSPACE</Industry>
-
<Contact>Bo Peep</Contact>
-
<PlanNumber>22222</PlanNumber>
-
</Plan>
-
<Plan>
-
<Industry>ENTERTAINMENT</Industry>
-
<Contact>Scrooge McDuck</Contact>
-
<PlanNumber>33333</PlanNumber>
-
</Plan>
-
</Plans>;
-
-
trace(x.Plan.(Industry == "AEROSPACE").PlanNumber);
This will return the correct value (22222), but I thought it was a bit odd that the dot that precedes the condition does not actually take you down into the node, allowing you to still directly access the property you're after. I think this is a very smart implementation because otherwise it would be very difficult and convoluted to select a value based on a sibling value. It would likely require something similar to, albeit more complicated than, the methodology recently discussed by Mike Chambers.
E4X definitely rocks the house.



on August 18th, 2006 at 7:55 pm
That may be because x.Plan is an XMLList, and (Industry == "AEROSPACE") is selecting one Plan in the list.
on August 18th, 2006 at 8:52 pm
Good point Robert. I guess it is a little more straightforward than I thought. Like I said, E4X rocks :)