thisXML キーワードで現在の XML を指定する

thisXML プロパティを使用すると、式で返された XML などの現在の XML を指定できます。thisXML プロパティは、コード内からオブジェクトまたは関数を指定するために使用する ECMAScript の this キーワードと同じように機能します。

thisXML プロパティは、XMLList の内容をフィルタ処理するときに特に便利です。次の例では、thisXML を使用して、データの欠落した <employee> 要素が存在しないようにしています。thisXML を使用することで、フィルタ式( children().length < 4 が、thisXML の前の式の結果である XMLList に対して評価されます。つまり、この例で、thisXMLxmlEmployees..employee で返された XML と同じです。

/* 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>
            <homeoffice>Alburquerque</homeoffice>
        </employee>
</employees>;
/* 
 * Find the <employee> elements that have less than 4 child elements.
 * Return the id attributes for those elements. 
 * This code returns "111111111".
 */
var ageXML = xmlEmployees..employee.(thisXML.children().length < 4).@id;

注意: thisXML は、属性でフィルタ処理する場合にも便利です。属性および .@ 演算子の性質上、次のような場合には thisXML を使用する必要があります。

/* 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>
            <homeoffice>Alburquerque</homeoffice>
        </employee>
</employees>;
/* 
 * Find the <employee> element where the id attribute is 222222222.
 * Return the corresponding last name.
 * This code returns "Day".
 */
var ageXML = xmlEmployees..employee.(thisXML.@id == "222222222").lastname;

関連トピック

.@ 演算子で属性にアクセスする

XML 変数を作成および使用する