When working with XML, you can use embedded expressions as a shorthand means to resolve or change element and attribute values. You can use the {} (curly braces) operator to enclose an expression that should be evaluated before the entire string is converted to XML for assignment to a variable. The ECMAScript in the following example restructures the XML in the xmlEmployees variable so that the id value is no longer stored in a child element of <employee>, but in an id attribute of that element.
/* Declare an XML variable with a literal XML value. */ var xmlEmployees = <employees> <employee> <id>111111111</id> <firstname>John</firstname> <lastname>Walton</lastname> <age>25</age> </employee> <employee> <id>222222222</id> <firstname>Sue</firstname> <lastname>Day</lastname> <age>32</age> </employee> </employees>; /* * Loop through the list of employees, restructuring the <employee> element * to make the id element an attribute of the <employee> element. */ for(e in xmlEmployees..employee){ var newStructure = <employee ssn={e.id}> <first_name>{e.firstname}</first_name> <last_name>{e.lastname}</last_name> <age>{e.age}</age> </employee>; }
Note that while you can also use {} to dynamically resolve element names, the value between {} may not be an empty string. For example, this is illegal: <{}>someValue</{}>.