When you expect XML messages with elements that use namespace prefixes, you can use a variable of the Namespace type to query the XML for elements contained in a specified namespace.
The syntax for declaring a Namespace variable is as follows:
var nsVariable = new Namespace("uriString");
The uriString value is the URI that uniquely identifies the namespace. By associating the variable with the namespace URI, you can then use the variable as you might use a namespace prefix. The syntax for using the namespace variable can be one of the following two variations. The first uses the . operator to separate elements in the hierarchy, which the second uses square brackets.
value = nsVariable::elementName1.nsVariable::elementName2;
value = ["nsVariable:elementName1"]["nsVariable:elementName2"];
The second variation is especially useful in cases where the element name conflicts with a script keyword (such as if or return). For more information about working around such conflicts, see A Few Things to Remember About XML Maps and Script.
In the following example, the "http://openuri.org/" namespace is associated with the myco variable.
/* Declare an XML variable with a literal XML value. */
var xmlEmps = <employeeData>
<inc:employees xmlns:inc="http://openuri.org/">
<inc:employee inc:id="111111111">
<inc:name>John</inc:name>
<inc:age>25</inc:age>
</inc:employee>
</inc:employees>
</employeeData>;
/*
* Declare a namespace variable to represent the namespace in the document
* above. When accessing the XML, the myco variable will correspond to the
* namespace URI in the way that the "inc" prefix above corresponds to the
* namespace in the XML instance.
*/
var myco = new Namespace("http://openuri.org/");
/*
* Create a new variable to hold the <employee> element containing John.
* Access the elements by using the namespace variable
* in double-colons in place of the prefix-colon combination used
* in the XML literal above.
*/
var xmlName = xmlEmps.myco::employees.myco::employee[0];
Note that you can also access namespaced attributes in the same way. In other words, you can use @inc::id to access the inc:id attribute in the example, as shown here:
/* Create a new variable to hold the <employee> node containing John. */ var xmlName = xmlEmps.myco::employee.attribute(@inc::id);Or like this:
var xmlName = xmlEmps.myco::employee.@inc::id;