return undefined;


Conditional selections based on sibling values in E4X

Posted in AS3, Flash by Ben Clinkinbeard on the August 18th, 2006

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:

Actionscript:
  1. var x:XML =
  2. <Plans>
  3.     <Plan>
  4.         <Industry>MFG</Industry>
  5.         <Contact>Bob Cratchett</Contact>
  6.         <PlanNumber>11111</PlanNumber>
  7.     </Plan>
  8.     <Plan>
  9.         <Industry>AEROSPACE</Industry>
  10.         <Contact>Bo Peep</Contact>
  11.         <PlanNumber>22222</PlanNumber>
  12.     </Plan>
  13.     <Plan>
  14.         <Industry>ENTERTAINMENT</Industry>
  15.         <Contact>Scrooge McDuck</Contact>
  16.         <PlanNumber>33333</PlanNumber>
  17.     </Plan>
  18. </Plans>;
  19.  
  20. 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.

2 Responses to 'Conditional selections based on sibling values in E4X'

Subscribe to comments with RSS or TrackBack to 'Conditional selections based on sibling values in E4X'.


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

  2. Ben said,

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

Leave a Reply