述語で複数の子をフィルタ処理する

. または .. 演算子を使用して複数の子が返される場合は、.()(述語)演算子を使用し、その結果を特定の基準に基づいてフィルタ処理することができます。.() 演算子は、XPath [ ] 演算子(パスの「述語」と呼ばれるものを構成する)と似た方法で機能します。

この xmlEmployees 変数に基づく以下のサンプル関数では、従業員に関する情報がフィルタ値に基づいて返されます。

/* 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 <employee> element for the employee whose last name 


 * is "Day". * This code returns an XMLList type containing: *    <employee id="222222222"> *        <firstname>Sue</firstname> *        <lastname>Day</lastname> *        <age>32</age> *    </employee> /*    var empDay = xmlEmployees..employee.(lastname == "Day");
/* * Return the age of the employee whose id is "111111111". * This code returns an XMLList type containing "25". */ var empAge = xmlEmployees..employee.(thisXML.@id == "111111111").age;

要素の名前が ECMAScript キーワードまたは関数名と衝突する場合は、代わりに次の構文を使用できます。ECMAScript キーワード用と関数名用の 2 通りの解決策があります。両方の場合で、thisXML プロパティを使用する必要があります。

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

関連トピック

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