0

There is a well known Identity Transform sample code in the XQuery wikibook

But it works well only with no namespace documents, or explicitly declaring the namespaces with the same prefixes used in the document about to be processed.

If you don't declare the namespaces (with the same prefixes), you get an error:

Cannot compile xquery: err:XPST0081 No namespace defined for prefix xsd [at line 15, column 12]

is there a way to write an Identity Transform in XQuery, that can automatically handle the namespaces and prefixes, avoiding the explicit declaration of namespaces?

EDIT:

This is the code from the Wikibook:

(: return a deep copy of  the element and all sub elements :)
declare function local:copy($element as element()) as element() {
   element {node-name($element)}
      {$element/@*,
          for $child in $element/node()
              return
               if ($child instance of element())
                 then local:copy($child)
                 else $child
      }
};

In my case, I don't know the namespaces or prefixes in the document to be processed, so the element { } { } construct fails if the namespace and prefix are not declared in the XQuery.

To reproduce it, just copy/paste and run it with a document that uses prefixed namespaces.

Bill Velasquez
  • 875
  • 4
  • 9
  • 1
    this hasn't be the case in my experience. can you post your code and content? – joemfb Aug 29 '14 at 16:52
  • I think you will have to paste an example of your XML. I just checked your XQuery and it works fine for me with namespaces with and without prefixes. – adamretter Sep 02 '14 at 14:46
  • Try it this way: let $x := doc('http://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/maindoc/UBL-Order-2.1.xsd') return local:copy($x) Remember I'm using eXist-db – Bill Velasquez Sep 02 '14 at 20:19

2 Answers2

0

I think the answer is no. If you want to process namespaced elements, then those namespaces need to be declared.

wst
  • 11,681
  • 1
  • 24
  • 39
  • In this case, what to do if you know the namespaces of the document, but not the prefixes used? – Bill Velasquez Aug 29 '14 at 16:16
  • 2
    @BillVelasquez Actually, I'm not able to reproduce this problem. If you want to select elements (like `//some-prefix:some-local-name`) then `some-prefix` needs to be declared in association with the namespace of the elements you intend to select (regardless of its prefix). However, if you are simply copying the elements using an identity transformation, then each element will "know" its namespace and a prefix declaration should be added to the element automatically when it's constructed. Can you post a simple example that reproduces your problem? – wst Aug 29 '14 at 16:31
0

Based on the error message, all you need to do is declare the xsd namespace at the top of your XQuery document.

Declare namespace xsd="http://www.w3.org/2001/XMLSchema";

If this doesn't do it, then posting your XQuery would greatly help us understand what the issue is.

westbaystars
  • 151
  • 1
  • 4