Hi,
Allow me to piggy ride on this question, I got a problem of similar nature I could not myself figure out. I have an xml with some nodes marked with attribute @mark.
I want to do 3 things.
1: Recreate the document with only the marked nodes
2. If the @mark is empty I want to remove the mark attribute
3. If the @mark is non empty, move the @mark value over to the node and also remove the @mark attribute.

The problem is that I lose the namespace bindings of the document and dont know how to solve it.

======== example

The original file has other namespace bindings e.g.
<root xmlns="something" xmlns:xn="somethingElse" xmlns:un="somethingMore" mark="">....</root>

Calling my function returns:
<root xmlns="something" >....</root>

But what I wanted was:
<root xmlns="something" xmlns:xn="somethingElse" xmlns:un="somethingMore">....</root>


This is the function I created

(:
$node should be root node the first time the function is called
Function recreates the document but only takes nodes with the attribute @mark while simultaneously removing the mark and if mark is non-empty then the mark@ is the new node value.
:)

declare function local:new-doc-helper($node){
  element {$node/node-name() }{$node/@*[name() ne "mark"],if( data($node/@mark) ne "")then data($node/@mark) else $node/text(),for $x in $node/*[exists(@mark)] return local:new-doc-helper($x)}
};

Thank you for reading.


Från: basex-talk-bounces@mailman.uni-konstanz.de <basex-talk-bounces@mailman.uni-konstanz.de> för Christian Grün <christian.gruen@gmail.com>
Skickat: den 9 juli 2016 09:06
Till: Mike Engledew
Kopia: BaseX
Ämne: Re: [basex-talk] Removing the xmlns attribute and/or adding the prefix
 
Hi Mike,

> a) Is it possible to disable the addition of the xmlns attribute?

XQuery (Update) provides no single function or expression to rename or
drop namespaces, but you can easily do that by recursively rebuilding
your document without namespaces:

  declare function local:strip-ns($n as node()) as node() {
    if($n instance of element()) then (
      element { local-name($n) } {
        $n/@*,
        $n/node()/local:strip-ns(.)
      }
    ) else if($n instance of document-node()) then (
      document { local:strip-ns($n/node()) }
    ) else (
      $n
    )
  };

  let $xml := document {
    <items xmlns="http://server.my.org/xyz">
server.my.org
my.org is your first and best source for all of the information you’re looking for. From general topics to more of what you would expect to find here, my.org has it all. We hope you find what you are searching for!


      <item>Stuff1</item>
      <item>Stuff2</item>
    </items>
  }
  return local:strip-ns($xml)


As REST results cannot be post-processed in the very same step, you
will be more flexible with a RESTXQ facade.

> b) Is it possible to include the prefix in the xmlns attribute being
> appended to the root element?

I’m not sure what you mean by this alternative. Would you like to have
rebound the namespace to a prefix?

    <xyz:items xmlns:xyz="http://server.my.org/xyz">
      ...

The answer would be the same: A recursive XQuery function will do the job.

If this all sounds too cumbersome, and if you never work with
namespaces anyway, you can already strip all namespaces when adding
documents to a database via the STRIPNS option [1].

Hope this helps,
Christian

[1] http://docs.basex.org/wiki/Options#STRIPNS