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:
<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.
Both comments and pings are currently closed.
