Thanks, Hans-Jürgen,

looks like a bug; we’ll track that down.

I assume you can simply omit the following line, as the necessary namespace declarations will automatically be added (unless you want to enforce the adoption of namespace declarations that are not required):

  in-scope-prefixes($n) ! namespace {.} {namespace-uri-for-prefix(., $n)},

Best,
Christian


On Wed, Sep 20, 2023 at 6:34 PM Hans-Juergen Rennau <hrennau@yahoo.de> wrote:
Dear BaseX folks,

constructing elements, I encounter a problem with namespaces which I do not understand. Is it a bug or do I overlook something?

To reproduce the problem you need to create an XML file "doc.xml" with this content:

    <foo bar="1"> 
        <peng/>
    </foo>
</doc>

And here's the query:

declare namespace f="http://basex.org/does/its/thing";

declare function f:pretty($n as node())
        as node()? {
    typeswitch($n)
    case document-node() return document {$n/node() ! f:pretty(.)}
    case element() return
        element {node-name($n)} {
            in-scope-prefixes($n) ! namespace {.} {namespace-uri-for-prefix(., $n)},
            $n/@* ! f:pretty(.),
            $n/node() ! f:pretty(.)
        }
    default return $n
};    

let $doc := doc('doc.xml')
return f:pretty($doc)

What I get is this error:

Stopped at C:/projects/tableman/prototype/thieme-showcase/xq/demo-namespace-problem.xq, 8/33:
[XQDY0102] Duplicate declaration of namespace 'http://basex.org/simply/the/best'.

Stack Trace:
- C:/projects/tableman/prototype/thieme-showcase/xq/demo-namespace-problem.xq, 11/33
- C:/projects/tableman/prototype/thieme-showcase/xq/demo-namespace-problem.xq, 11/33
- C:/projects/tableman/prototype/thieme-showcase/xq/demo-namespace-problem.xq, 6/63
- C:/projects/tableman/prototype/thieme-showcase/xq/demo-namespace-problem.xq, 17/16

Strangely enough, you do not get the error if you copy the contents of doc.xml into the query:

let $doc := <doc .../>
return f:pretty($doc)

You also do not get the error if you remove the @bar attribute in the document.

Kind regards,
Hans-Jürgen