I need to filter some XML data based on several conditions. I found the following article “”http://www.darronschall.com/weblog/archives/000214.cfm">E4X: Predicate Filtering with Regular Expressions" from Darron that provides a nice overview of filtering using e4x. I haven’t found any more detailed information on using predicates so I checked out the ECMAScript for XML
(E4X) Specification 2nd edition. See page 52 for details. But in short the filter can be an expression and use && and ||, the logical and and or.
filtered = list.item.( /^4567./.test( number ) || /^D./.test( name ) );
Here is the full test case:
package tests.actionscript
{
import flexunit.framework.*;
public class TestXml extends TestCase
{
public function testXMLFiltering():void {
var list:XML =
var filtered:XMLList = list.item.( /^D./.test( name ) ); Assert.assertEquals(2, new XMLListCollection(filtered).length);
filtered = list.item.( /^4567./.test( number ) );
Assert.assertEquals(3, new XMLListCollection(filtered).length);
filtered = list.item.( /^4567./.test( number ) && /^D./.test( name ) );
Assert.assertEquals(1, new XMLListCollection(filtered).length);
filtered = list.item.( /^4567./.test( number ) || /^D./.test( name ) );
Assert.assertEquals(4, new XMLListCollection(filtered).length);
}
} // class
} // package