Specifying the Current XML with the thisXML Keyword

You can use the thisXML property to specify the current XML, such as XML returned from an expression. The thisXML property works in a manner similar to the this keyword in Java, which is used to specify objects or functions from within their own code.

The thisXML property is especially useful when filtering the contents of an XMLList. In the following example, thisXML is used to ensure that there aren't any <employee> elements that may be lacking data. Using thisXML ensures that the filter expression—children().length < 4—is evaluated against the XMLList resulting from expression that precedes thisXML. In other words, in this example thisXML is equivalent to the XML returned by xmlEmployees..employee.

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

Note: You may also find yourself using thisXML when filtering by attribute. The nature of attributes and the @ operator makes it necessary to use thisXML in cases like the following:

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

Related Topics

Accessing Attributes and Their Values With the @ Operator

Creating and Using XML Variables