# Accessing Element Descendants With the .. Operator

You can use the .. (double dot) operator to specify any child contained by the element left of the operator, regardless of how far down the hierarchy the child is. This gives you easy access to descendents (children, grandchildren, and so on) of an element. Through this operator, you can avoid having to build a complete path to an element that may be deeply nested.

The .. operator examines all of the descendent elements of its left operand and returns those with names that match its right operand, and returns them in the order in which they appear in the document. When the left operand is a list of elements, each of these elements is examined according to their order in the document.

The following example illustrates using the .. operator to return a single value or an element.

    /* Declare an XML variable with a literal XML value. */
    var xmlEmployees = <employees>
        <employee id="111111111">
            <firstname>John</firstname>
            <lastname>Walton</lastname>
            <age>25</age>
        </employee>
        <employee id="222222222">
            <firstname>Sue</firstname>
            <lastname>Day</lastname>
            <age>32</age>
        </employee>
    </employees>;
    /*
     * Return the first name of the first employee in the list.
     * This code returns an XMLList type containing "John".
     */
    var name = xmlEmployees..employee[0].firstname;
    /*
     * Return the first <employee> element.
     * This code returns an XML type with all of the first <employee> element:
     *    <employee id="111111111">
     *        <firstname>John</firstname>
     *        <lastname>Walton</lastname>
     *        <age>25</age>
     *    </employee>
     */
    var firstEmployee = xmlEmployees..employee[0];

When elements have names that conflict with function names, you can use the following syntax instead. Note that there is currently no workaround for using the .. operator when its right operand conflicts an ECMAScript keyword.

    /*
     * Return the first <parent> element.
     * This avoids a conflict with the parent function.
     */
    var firstParent = xmlFamilies..::parent[0];

When elements have names that conflict with ECMAScript keywords, you can use the following syntax instead..

    /*
     * Return all of the <if> elements.
     * This avoids a conflict with the "if" ECMAScript keyword.
     */
    var ifs = xmlData.product_description["..if"];
    /*
     * Return the first <parent> element.
     * This avoids a conflict with the parent function.
     */
    var firstParent = xmlFamilies.kid.::parent[0];

Related Topics

Accessing Element Children Iteratively

Accessing Element Children Through an Index