XMLNAMESPACES

The XMLNAMESPACES declaration constructs namespace declarations from the arguments. This declaration can only be used as an argument for the XMLELEMENT and XMLFOREST functions. The result is one or more XML namespace declarations containing in-scope namespaces for each non-null input value.

XMLNAMESPACES(,namespace-uriASnamespace-prefix1DEFAULTnamespace-uriNO DEFAULT)
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.
AS namespace-prefix
Specifies a namespace prefix. The prefix is an SQL identifier that must be in the form of an XML NCName. See the W3C XML namespace specifications for more details on valid names. 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.

Examples

Note: XML processing does not insert blank spaces or new line characters in the output. All example output has been formatted to enhance readability.
  • 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(200))
      FROM EMPLOYEE E WHERE E.EDLEVEL = 12

    This query produces the following result:

    00029 <bo:employee xmlns:bo="urn:bo" LASTNAME="PARKER" FIRSTNME="JOHN">
          <bo:hiredate>1988-05-30</bo:hiredate>
          </bo:employee>
    00031 <bo:employee xmlns:bo="urn:bo" LASTNAME="SETRIGHT" FIRSTNME="MAUDE">
          <bo:hiredate>1964-09-12</bo:hiredate>
          </bo:employee>
  • 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(200))
      FROM EMPLOYEE WHERE EDLEVEL = 12

    This query produces 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>