[xquery-talk] namespace questions

Michael Kay mhk at mhk.me.uk
Mon May 8 10:23:17 PDT 2006

> I use the follwing xquery:
> declare namespace sam="http://www.aaa.aaa/a"; declare 
> namespace huhu="http://www.aaa.aaa/b"; let $extern := <a 
> namespace="huhu"/> return
>    <foo>
> 	<sam:bar/>
> 	{element {concat($extern/@namespace,":","ele")} {"text"}}
> 	</foo>
> which generates:
> <foo>
>     <sam:bar/>
>     <huhu:ele>text</huhu:ele>
> </foo>
> Questions:
> 1.	What is necessary to create the missing namespace 
> declarations xmlns:sam=”http://www.aaa.aaa/a”  in the foo element ?

Write <foo xmlns:sam="http://www.aaa.aaa/a"/>.

There are two kinds of namespace declarations in XQuery. "declare namespace"
defines a namespace binding that can be used anywhere in the query, but
which is not copied into constructed elements. An xmlns="xyz" declaration in
a direct element constructor defines a binding that can be used anywhere
lexically within that element constructor, and which is also copied to the
result element.

> 2. If the second line  (declare namespace 
> huhu=http://www.aaa.aaa/b”; is missing ) there is a runtime 
> error. (Undeclared prefix in element name:huhu). If the 
> prefix “huhu” of the namespace is not known in advance, but 
> only at run time, is it still possible to construct a node 
> “<huhu:ele>” ?

Yes, supply a value of type xs:QName(), which you can construct dynamically
if you know the namespace URI and local name.

element {QName('uri', 'local')} { content }

Michael Kay

More information about the talk mailing list