Removing Elements and Attributes With the delete Operator

You can use the delete operator to remove specified elements and attributes, as shown in the following example.

/* 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>;
/*
 * Remove the information about John.
 */

delete xmlEmployees.employees.employee[0];

/* 
 * Remove the entire <employees> node, leaving an empty XML variable.
 */

delete xmlEmployees.employees;

Note that when using delete with a path that ends with a filter predicate, you must append the thisXML property, as in the following example:

/*
 * A delete operation with a predicate expression must end with the thisXML property.

 */
delete xmlEmployees.employees.employee.(firstname == "John").thisXML;

Related Topics

Filtering Multiple Children With Predicates