HOWTO: Namespace use with XMLQuery

Just a small post here as a reminder how it can be applied: the use of namespaces in conjunction with the XMLQuery operator. A question came up on the OTN XMLDB Forum based on XMLQuery. After building a correct example XML file I needed the namespace reference within XMLQuery and it wasn’t easy to find so here, as a reminder, the comprised version.

See also post “Xquery Error – ORA19276” as a reference.

with stuff AS
 (select xmltype('xml data string')
 xmlcol from dual
select XMLQuery
('xquery version "1.0"; (: :)
  DECLARE namespace xqs="WiMax_AP"; (: :)
  FOR $i in /xqs:ConfigDataFile
  WHERE $i/xqs:fileHeader[@NeID="WiMAX_AP_1"]
  RETURN $i/xqs:configData'
  PASSING xmlcol
) FROM stuff

As Mark stated on the linked URL on my OTN thread: “The empty comments ‘(: :)’ (bracket, colon, space, colon, bracket) at the end of each XQuery statement are introduced to stop the SQL*PLUS interpreter from treating the ‘;’ as the end of the SQL Statement.”

You will find on the OTN XMLDB forum thread “XMLQuery (XQuery) with namespace” (XML document issues) also better examples of the use of the “xquery” operator and the use of xquery statements combined with xmltable. Also the default namespace declaration is demonstrated in this OTN XMLDB forum thread.

Based on the W3 Namespace Declaration for XQuery Syntax a default namespace declaration associated with unprefixed names of elements and types, would be declared in a XQuery via:

DefaultNamespaceDecl ::= 
   "declare" "default" ("element"|"function") "namespace" URILiteral 

So in a statement this could be something like the following:

select xmlquery('xquery version "1.0"; (: :)
                 declare default element namespace 
                            ""; (: :)
                 for $s in /XMLMessage
                 return $s' 
                 PASSING   cmf.object_value 
                 RETURNING content)
from xmltype_table cmf;



Marco Gralike Written by:

One Comment

Comments are closed.