Search code examples
namespacesxqueryprefix

XQuery on doc collection with multiple prefixes that map to the same namespace


I am using XQuery (Berkeley dbXML 6.0) to query documents from a collection. All documents use the same namespace, but the prefixes differ. To abstract the problem:

doc1: <a xmlns:ns0="http://my.url"> here i am </a>

doc2: <a xmlns:ns1="http://my.url"> me too! </a>

Both ns0 and ns1 map to the same namespace. I would like to avoid returning different namespace prefixes in the XQuery result. A simple XQuery such as:

<Result xmlns:ns2="http://my.url"> {
  for $doc in collection("my_collection")/ns2:a
    <Return> {$doc} </Return>
} </Result>

shows the ns0 and ns1 prefixes for my documents 1 and 2. As they all map to the same namespace, I would have thought that the only namespace I should have seen was in the enclosing result document. The namespace prefixes are creating problems for downstream processing. I can remove these manually, but it would be nice if there was a way to construct this correctly in XQuery.


Solution

  • XQuery can't automatically change the namespace prefix because it can't be sure that it's unused. For example if there's an attribute xsi:type='my:part-number', then it doesn't know that my is a namespace prefix, because it's in an attribute value rather than in attribute content. You're going to have to do a much more thorough rebuilding of the document to achieve this (Personally, I would use XSLT for this).