1

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.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
Marie R
  • 270
  • 3
  • 9
  • If the different namespace prefixes are causing problems for downstream processing, that is an indication that they are not using proper tooling and are just doing string operations instead of using XML APIs. I would spend more time working with them to improve their side than bending over backwards to make the XML serialize differently. – Mads Hansen Jul 17 '22 at 14:24

1 Answers1

2

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).

Michael Kay
  • 156,231
  • 11
  • 92
  • 164