Joe Wicentowski
2018-04-02 19:00:45 UTC
Hi all,
A common question for beginners is how to access empty namespaced elements
in non-empty contexts. The easiest answer is to use a wildcard. For
example, we use one here inside the curly braces:
let $x := <x><y/></x>
return
<z xmlns="foo">{ $x/*:y }</z>
As expected, this query returns:
<z xmlns="foo"><y xmlns=""/></z>
The other method I know of is to use the URI-qualified name:
let $x := <x><y/></x>
return
<z xmlns="foo">{ $x/Q{}y }</z>
A third method occurred to me, but it does not work, and I haven't been
able to pin down the reason despite reading the spec. The idea is to bind
the empty namespace URI to a namespace prefix, e.g., "my":
declare namespace my="";
let $x := <x><y/></x>
return
<z xmlns="foo">{ $x/my:y }</z>
In BaseX, eXist, and Saxon, this returns err:XPST0081 (
https://www.w3.org/TR/xquery-31/#ERRXPST0081). The location of the error
points to the step, "my:y" on the final line.
Can anyone enlighten me on the reason this 3rd method is invalid? Bonus
points for an explanation that includes why I cannot bind a namespace
prefix to the empty namespace, but I can access it via *: and Q{}?
Thanks,
Joe
A common question for beginners is how to access empty namespaced elements
in non-empty contexts. The easiest answer is to use a wildcard. For
example, we use one here inside the curly braces:
let $x := <x><y/></x>
return
<z xmlns="foo">{ $x/*:y }</z>
As expected, this query returns:
<z xmlns="foo"><y xmlns=""/></z>
The other method I know of is to use the URI-qualified name:
let $x := <x><y/></x>
return
<z xmlns="foo">{ $x/Q{}y }</z>
A third method occurred to me, but it does not work, and I haven't been
able to pin down the reason despite reading the spec. The idea is to bind
the empty namespace URI to a namespace prefix, e.g., "my":
declare namespace my="";
let $x := <x><y/></x>
return
<z xmlns="foo">{ $x/my:y }</z>
In BaseX, eXist, and Saxon, this returns err:XPST0081 (
https://www.w3.org/TR/xquery-31/#ERRXPST0081). The location of the error
points to the step, "my:y" on the final line.
Can anyone enlighten me on the reason this 3rd method is invalid? Bonus
points for an explanation that includes why I cannot bind a namespace
prefix to the empty namespace, but I can access it via *: and Q{}?
Thanks,
Joe