Working with namespaces

The following example shows how to use ESQL to work with namespaces.

About this task

Namespace constants are declared at the start of the main module so that you can use prefixes in the ESQL statements instead of the full URIs of the namespaces. The namespace constants affect only the ESQL; they do not control the prefixes that are generated in the output message. The prefixes in the generated output message are controlled by namespace declarations in the message tree. You can include namespace declarations in the tree using the XML.NamespaceDecl field type. These elements are then used to generate namespace declarations in the output message.

When the output message is generated, if the parser encounters a namespace for which it has no corresponding namespace declaration, a prefix is automatically generated using prefixes of the form NSn where n is a positive integer.

CREATE COMPUTE MODULE xmlns_doc_flow_Compute
CREATE FUNCTION Main() RETURNS BOOLEAN 
BEGIN 
CALL CopyMessageHeaders();
-- Declaration of namespace constants -- 
These are only used by ESQL

DECLARE sp1 NAMESPACE 'http://www.ibm.com/space1'; 
DECLARE sp2 NAMESPACE 'http://www.ibm.com/space2'; 
DECLARE sp3 NAMESPACE 'http://www.ibm.com/space3';  

-- Namespace declaration for prefix 'space1'  
SET OutputRoot.XMLNS.message.(XML.NamespaceDecl)xmlns:space1 = 'http://www.ibm.com/space1';   
SET OutputRoot.XMLNS.message.sp1:data1 = 'Hello!';         

-- Default namespace declaration ( empty prefix )  
SET OutputRoot.XMLNS.message.sp2:data2.(XML.NamespaceDecl)xmlns = 'http://www.ibm.com/space2'; 
SET OutputRoot.XMLNS.message.sp2:data2.sp2:subData1 = 'Hola!'; 
SET OutputRoot.XMLNS.message.sp2:data2.sp2:subData2 = 'Guten Tag!';  
SET OutputRoot.XMLNS.message.sp3:data3 = 'Bonjour!';  
RETURN TRUE; 
END;  

CREATE PROCEDURE CopyMessageHeaders() 
BEGIN 
DECLARE I INTEGER 1; 
DECLARE J INTEGER CARDINALITY(InputRoot.*[]); 
WHILE I < J DO SET OutputRoot.*[I] = InputRoot.*[I]; 
  SET I = I + 1; 
END WHILE; 
END;  
END MODULE;
When this ESQL is processed, the following output message is generated:
<message xmlns:space1="http://www.ibm.com/space1">
 <space1:data1>Hello!</space1:data1>
 <data2 xmlns="http://www.ibm.com/space2">
  <subData1>Hola!</subData1>
  <subData2>Guten Tag!</subData2>
 </data2>
 <NS1:data3 xmlns="http://www.ibm.com/space3>Bonjour!</NS1:data3>
</message>