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

.@(属性)演算子では、. 演算子で要素にアクセスするように属性にアクセスできます。

/* 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>;
/*
 * Assign a variable with John's id number. 
 * This code returns a string containing "111111111".
 */
var xmlEmployeeID = xmlEmployees.employees.employee[0].@id;
/*
 * Set Sue's id number to "555555555".
 */
xmlEmployees.employees.employee[1].@id = "555555555";

注意: 属性と .@ 演算子では、その性質上、.@ 演算子を使用してフィルタ処理するときには thisXML プロパティを使用する必要があります。

/* 
 * Find the <employee> element where the id attribute is 222222222.
 * Return the corresponding last name.
 * This code returns <lastname>Day</lastname>.
 */
var ageXML = xmlEmployees..employee.(thisXML.@id == "222222222").lastname;

関連トピック

attribute 関数

attributes 関数