O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Filtering XML Data

The E4X filtering predicate operator is a simple but powerful search tool. It can take any XMLList and return a subset of items from that list based on a specified condition. (The term predicate is borrowed from the W3C's XPath Language. See http://www.w3.org/TR/xpath20/#id-predicates.)

The filtering predicate operator takes the general form:

theXMLList.(conditionExpression)

For each item in theXMLList, the conditionExpression is executed once. If the conditionExpression yields true for an item, that item is added to an XMLList that is returned after all items have been processed. Note that during each execution of the conditionExpression, the current item is temporarily added to the front of the scope chain, allowing the item's child elements and attributes to be referenced directly by name within the expression.

The filtering predicate operator is extremely intuitive to use. Let's take a look at a new XML fragment and do some filtering! Example 18-12, the new fragment, represents a company's staff list.

Example 18-12. An employee list

var staff:XML = <STAFF> <EMPLOYEE ID="501" HIRED="1090728000000"> <NAME>Marco Crawley</NAME> <MANAGER>James Porter</MANAGER> <SALARY>25000</SALARY> <POSITION>Designer</POSITION> </EMPLOYEE> <EMPLOYEE ID="500" HIRED="1078462800000"> <NAME>Graham Barton</NAME> <MANAGER>James Porter</MANAGER> <SALARY>35000</SALARY> <POSITION>Designer</POSITION> </EMPLOYEE> <EMPLOYEE ID="238" HIRED="1014699600000"> <NAME>James Porter</NAME> <MANAGER>Dorian ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required