XMLNSC: Namespace declarations

The XMLNSC parser provides full support for namespaces.

The XMLNSC parser sets the correct namespace on every syntax element that it creates while parsing a message, and stores namespace declarations in the message tree. The parser uses the namespace declarations to select the appropriate prefixes when outputting the message tree.

The XMLNSC parser uses the following field types to represent namespace declarations. Use the field type constants that are listed in this table when you create namespace declarations in the message tree.

Table 1. Specific field type constants
XML construct XMLNSC field type constant Value
Namespace declaration
XMLNSC.SingleNamespaceDecl
XMLNSC.DoubleNamespaceDecl
0x03000102
0x03000103
When accessing elements and attributes in the message tree, do not use the constants that are listed in the previous table; instead, use the generic field type constant that matches both of the values in the table above.
Table 2. Generic field type constants
XML construct XMLNSC field type constant Purpose
Namespace declaration XMLNSC.NamespaceDecl Matches namespace declarations in both single and double quotation marks

ESQL code examples

Example 1: Declaring a non-empty prefix
DECLARE space1 NAMESPACE 'namespace1';
SET OutputRoot.XMLNSC.space1:root.(XMLNSC.NamespaceDecl)xmlns:ns1 = space1;
SET OutputRoot.XMLNSC.space1:root.space1:example = 'ABCDE';
This creates the following XML message:
<ns1:root xmlns:ns1="namespace1">
    <ns1:example>ABCDE</ns1:example>
</ns1:root>
Note that the NAMESPACE constant space1 is just a local variable in the ESQL; it does not affect the namespace prefix ns1 that is defined by the NameSpaceDecl element and appears in the output message.

However, as shown here, space1 can be used to initialize the NameSpaceDecl for ns1. This avoids the need to duplicate the namespace URI ('namespace1' in this example), which in practice is typically a much longer string.

Example 2: Declaring an empty prefix
DECLARE space1 NAMESPACE 'namespace1';
SET OutputRoot.XMLNSC.space1:root.(XMLNSC.NamespaceDecl)xmlns = space1;
SET OutputRoot.XMLNSC.space1:root.space1:example = 'ABCDE';
This creates the following XML message:
<root xmlns="namespace1">
    <example>ABCDE</example>
</root>
Note that the syntax elements root and example must have a non-empty namespace. The default namespace declaration means that any child element without a prefix is in the namespace namespace1.
Example 3: Example of incorrect usage
DECLARE space1 NAMESPACE 'namespace1';
SET OutputRoot.XMLNSC.root.(XMLNSC.NamespaceDecl)xmlns = space1;
SET OutputRoot.XMLNSC.root.example = 'ABCDE';
This example causes the XMLNSC parser to issue the message BIP5014 when it attempts to create the message tree. The elements root and example are both within the scope of the default namespace declaration; therefore, in ESQL, these elements must be qualified by a namespace prefix bound to that namespace.
Example 4: Adding a namespace declaration with a prefix
SET OutputRoot.(XMLNSC.DoubleNamespaceDecl)xmlns:ns2 = space1;
This example of a SET statement creates a namespace declaration with the name ns2 in the namespace xmlns.
CREATE LASTCHILD OF OutputRoot IDENTITY (XMLNSC.DoubleNamespaceDecl)xmlns:ns2 VALUE space1;
CREATE LASTCHILD OF OutputRoot TYPE XMLNSC.DoubleNamespaceDecl NAMESPACE 'xmlns' NAME 'ns2' VALUE space1;
These examples of a CREATE statement also create a namespace declaration with the name ns2 in the namespace xmlns.
However, be aware that the following example of a CREATE statement creates a namespace declaration with the name xmlns:ns2 in the default namespace:
CREATE LASTCHILD OF OutputRoot TYPE XMLNSC.DoubleNamespaceDecl NAME 'xmlns:ns2' VALUE space1;