Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Descendants and Filters

Accessing descendants

In many cases, the node that you're interested in is not the direct child of the top XML element. Instead, it may be nested several levels down. You can access nodes at any depth using the .. operator, or by using the descendants property. For instance

var element = <pets>
                <dogs>
                  <fido color="red"/>
                  <spike color="blue"/>
                </dogs>
              </pets>;
element..fido.@color = "green";
element..spike.@color = "purple";

changes the colors of both of our pet dogs.

The same rules that apply to the . operator apply to the .. operator. That is, if there are multiple descendants of the requested type, an XML list is returned, instead of the normal element. The * selector returns all descendants in an XML list.

Filters

In many cases, especially with the * selector, you don't want to work with all of the nodes that would be returned by the . or .. operator. To work with only a specific subset, place a conditional in parentheses after the operator. Consider

var element = <dogs>
                <fido color="brown"/>
                <spike color="black"/>
                <killer color="brown"/>
              </dogs>;
var list = element.*.(@color == "brown");

Originally, elements.* returns a list with all 3 dogs in it. However, only fido and killer have their color attributes as brown. The filtering conditional therefore creates a list with just fido and killer as members.

Document Tags and Contributors

 Contributors to this page: Sheppy, trevorh, Brettz9, Waldo, Jminta
 Last updated by: Sheppy,