User Guide

Traversing XML structures 325
Filtering by attribute or element value
You can use the parentheses operators— ( and ) —to filter elements with a specific element
name or attribute value. Consider the following XML object:
var x:XML =
<employeeList>
<employee id="347">
<lastName>Zmed</lastName>
<firstName>Sue</firstName>
<position>Data analyst</position>
</employee>
<employee id="348">
<lastName>McGee</lastName>
<firstName>Chuck</firstName>
<position>Jr. data analyst</position>
</employee>
</employeeList>
The following expressions are all valid:
x.employee.(lastName == "McGee")—This is the second employee node.
x.employee.(lastName == "McGee").firstName—This is the firstName property of
the second
employee node.
x.employee.(lastName == "McGee").@id—This is the value of the id attribute of the
second
employee node.
x.employee.(@id == 347)—The first employee node.
x.employee.(@id == 347).lastName—This is the lastName property of the first
employee node.
x.employee.(@id > 300)—This is an XMLList with both employee properties.
x.employee.(position.toString().search("analyst") > -1)—This is an
XMLList with both
position properties.
If you try to filter on attributes or elements that may not exist, Flash Player will throw an
exception. For example, the final line of following code generates an errors because there is no
id attribute in the second p element:
var doc:XML =
<body>
<p id='123'>Hello, <b>Bob</b>.</p>
<p>Hello.</p>
</body>;
trace(doc.p.(@id == '123'));