XMLNAMESPACES

The XMLNAMESPACES function constructs namespace declarations from the arguments. This function can be used as an argument only for specific functions, such as the XMLELEMENT function and the XMLFOREST function.

Read syntax diagram
                  .-,---------------------------------------.     
                  V                                         |     
>>-XMLNAMESPACES(---+-namespace-uri--AS--namespace-prefix-+-+-)-><
                    |                            (1)      |       
                    '-+-DEFAULT--namespace-uri-+----------'       
                      '-NO DEFAULT-------------'                  

Notes:
  1. The DEFAULT or NO DEFAULT clause can only be specified one time.

The schema is SYSIBM.

The result is one or more XML namespace declarations containing in-scope namespaces for each non-null input value.

namespace-uri
Specifies an SQL character string constant that contains the namespace name or a universal resource identifier (URI). The character string constant must not be an empty string if it is used with namespace-prefix. namespace-uri cannot be http://www.w3.org/XML/1998/namespace or http://www.w3.org/2000/xmlns/.
AS namespace-prefix
Specifies a namespace prefix. The prefix is an SQL identifier that must be in the form of an XML NCName. The prefix must not be "xml" or "xmlns". The prefix must be unique within the list of namespace declarations.

The following namespace prefixes are pre-defined in SQL/XML: "xml", "xs", "xsd", "xsi", and "sqlxml". Their bindings are:

  • xmlns:xml = "http://www.w3.org/XML/1998/namespace"
  • xmlns:xs = "http://www.w3.org/2001/XMLSchema"
  • xmlns:xsd = "http://www.w3.org/2001/XMLSchema"
  • xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
  • xmlns:sqlxml= "http://standards.iso.org/iso/9075/2003/sqlxml"
DEFAULT namespace-uri or NO DEFAULT
Specifies whether a default namespace is to be used within the scope of this namespace declaration.

The scope of this namespace declaration is the specified XML element and all XML expressions that are contained in the specified XML element.

DEFAULT namespace-uri
Specifies the default namespace to use within the scope of this namespace declaration. The namespace-uri applies for unqualified names in the scope unless it is overridden in a nested scope by another DEFAULT declaration or by a NO DEFAULT declaration.

namespace-uri specifies an SQL character string constant that contains a namespace name or universal resource identifier (URI). The character string constant can be an empty string in the context of the DEFAULT clause.

NO DEFAULT
Specifies that no default namespace is to be used within the scope of this namespace declaration. There is no default namespace in the scope unless the NO DEFAULT clause is overridden in a nested scope by a DEFAULT declaration.

The result of the function is an XML value that is an XML sequence that contains an XML namespace declaration for each specified namespace. The result cannot be null.

Example 1: Generate an "employee" element for each employee. The employee element is associated with XML namespace "urn:bo', which is bound to prefix "bo". The element contains attributes for names and a hiredate subelement.
SELECT e.empno, XMLSERIALIZE(XMLELEMENT(NAME "bo:employee",
                   XMLNAMESPACES('urn:bo' as "bo"),
                   XMLATTRIBUTES(e.lastname, e.firstnme),
                   XMLELEMENT(NAME "bo:hiredate", e.hiredate)) AS CLOB(50))
   FROM employee e where e.edlevel = 12;
The result of the query would be similar to the following result:
00029 <bo:employee xmlns:bo="urn:bo" LASTNAME="PARKER" FIRSTNME="JOHN">
      <bo:hiredate>198-5-3</bo:hiredate>
      </bo:employee>
00031 <bo:employee xmlns:bo="urn:bo" LASTNAME="SETRIGHT"
      FIRSTNME="MAUDE">
      <bo:hiredate>1964-9-12</bo:hiredate>
      </bo:employee>
Example 2: Generate two elements for each employee using XMLFOREST. The first "lastname" element is associated with the default namespace "http://hr.org", and the second "job" element is associated with XML namespace "http://fed.gov", which is bound to prefix "d".
SELECT empno, XMLSERIALIZE(XMLFOREST(
          XMLNAMESPACES(DEFAULT 'http://hr.org', 'http://fed.gov' AS "d"),
         lastname, job AS "d:job") AS CLOB(50))
FROM employee where edlevel = 12;
The result of the query would be similar to the following result:
00029 <LASTNAME xmlns="http://hr.org" xmlns:d="http://fed.gov">PARKER
      </LASTNAME>
      <d:job xmlns="http://hr.org" xmlns:d="http://fed.gov">
      OPERATOR</d:job>
00031 <LASTNAME xmlns="http://hr.org" xmlns:d="http://fed.gov">
      SETRIGHT</LASTNAME>
      <d:job xmlns="http://hr.org" xmlns:d="http://fed.gov">
      OPERATOR</d:job>