[xquery-talk] Brainteaser
Sander Bos
sander at x-hive.com
Wed May 14 10:18:29 PDT 2003
> Here's a brainteaser XQuery problem, borrowed from a co-worker's real
> problem from this afternoon. I thought I'd share it as a
> little XQuery
> braincell stretch. I'm not asking for help; I know the
> answer and will
> post it later in the off chance no one else writes in.
> (Don't cheat and
> peek ahead unless you need to!)
> define function get-root() as element {
> document("teaser.xml")/root
> }
>
> <html xmlns="http://www.w3.org/1999/xhtml">
> <body>
> <br> This correctly prints the original document
> {
> get-root()
> }
> </br>
>
> <br> This should print the original document, but doesn't
> {
> document("teaser.xml")/root
> }
> </br>
>
> <br> This should print the names, but doesn't
> {
> get-root()/record/name
> }
> </br>
> </body>
> </html>
[SPOILER....]
That the test-case is small obviously makes it easier to find what is
the problem:
<html xmlns="http://www.w3.org/1999/xhtml">
The default namespace declaration means that 'root' in
document("teaser.xml")/root
is treated as '{http://www.w3.org/1999/xhtml}root' which cannot be found
in the namespace free document. The function does not have this problem
because it is outside the scope of the element constructor.
Kind regards,
--Sander.
--
X-Hive Corporation (www.x-hive.com)
email: sander at x-hive.com
phone: +31 10 2818080
More information about the Talk
mailing list