.. 演算子で要素の子孫にアクセスする

..(二重ドット)演算子を使用すると、その子が階層のどのくらいの深さにあるかに関係なく、演算子の左側の要素に含まれている子を指定できます。この機能により、要素の子孫(子、孫など)に容易にアクセスできます。この演算子を使用した場合は、幾重にもネストされている要素の完全なパスを作成する必要がありません。

.. 演算子は、左オペランドのすべての子孫要素を調べ、右オペランドと名前が一致する要素を、それらがドキュメントに現れる順序で返します。左オペランドが要素のリストである場合は、それらの各要素がドキュメントの順序に従って調べられます。

次の例では、.. 演算子を使用して単一の値または要素を返します。

/* 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];

要素の名前が関数名と衝突する場合は、代わりに次の構文を使用できます。.. 演算子については、右オペランドと ECMAScript キーワードが衝突する場合の解決策は現時点ではありません。

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

関連トピック

要素の子に繰り返しアクセスする

インデックスから要素の子にアクセスする