Hi,

Yes you did get it right and I did try the query though sadly java runs out of memory. 


I rather not prune my xml because It's around 5GB. Recreating the document with only marked nodes requires about 20 MB.






Från: Christian Grün <christian.gruen@gmail.com>
Skickat: den 9 juli 2016 18:45
Till: Henning Phan
Kopia: BaseX
Ämne: Re: [basex-talk] Removing the xmlns attribute and/or adding the prefix
 
Hi Henning,

> Let me clarify and hopefully use the correct terminology. "move the @mark value over to the node". We have the element node <elem mark="y">x</elem>,  What I mean was to copy the value of @mark to the element's text node. Final result should be <elem>y</elem>

I see; so I think I got it right. Did you try the query I attached in
the last e-mail?

Christian



> You said Christian "As you are creating new elements, namespace declarations will be lost
> if they are not referenced somewhere in the document."  and I wonder  about the implication of that. But maybe another test case is more helpful and touches on the subject.
>
>
> If we have the xml
>
> === original ===
>
> <a xmlns="a.com" xmlns:b="b.com" xmlns:c="c.com">
>
>    <a>
>
>         <b:b/>
>
>         <c:c/>
>
> </a>
>
>
> How do we get:
>
> ====== Option 1
>
>
> <a xmlns="a.com" xmlns:b="b.com" xmlns:c="c.com">
>
>    <a>
>
>         <b:b/>
>
> </a>
>
> ===== end Option 1
>
>
> Or if that is not possible because xmlns:c is not referenced in the document I would settle with:
>
>
> ====== Option 2
>
>
> <a xmlns="a.com" xmlns:b="b.com">
>
>    <a>
>
>         <b:b/>
>
> </a>
>
>
> ====end option 2
>
>
> ________________________________
> Från: Christian Grün <christian.gruen@gmail.com>
> Skickat: den 9 juli 2016 17:07
> Till: Henning Phan
> Kopia: BaseX
> Ämne: Re: [basex-talk] Removing the xmlns attribute and/or adding the prefix
>
> Hi Henning,
>
> As you are creating new elements, namespace declarations will be lost
> if they are not referenced somewhere in the document.
>
> XQuery Update should help you, though. I’m not exactly sure what you
> meant by "move the @mark value over to the node", but the following
> function produces results similar to yours (it can surely be further
> optimized):
>
>   declare function local:new-doc-helper($node) {
>     $node update {
>       for $e in descendant-or-self::*
>       let $m := $e/@mark
>       return if($m) then (
>         delete node $m,
>         (: [.] is same as: where $d ne "" :)
>         for $d in data($m)[.]
>         return (
>           delete node $e/text(),
>           insert node $d as first into $e
>         )
>       ) else (
>         delete node $e
>       )
>     }
>   };
>
>   let $xml :=
>     <root xmlns="something" xmlns:xn="somethingElse"
>       xmlns:un="somethingMore" mark="">....</root>
>   return local:new-doc-helper($xml)
>
>
>
> ________________________________
> Från: basex-talk-bounces@mailman.uni-konstanz.de <basex-talk-bounces@mailman.uni-konstanz.de> för Kristian Kankainen <kristian@keeleleek.ee>
> Skickat: den 9 juli 2016 16:33
> Till: BaseX
> Ämne: Re: [basex-talk] Removing the xmlns attribute and/or adding the prefix
>
>
> Hi!
>
> I'm not really understanding your function, but is your problem about preserving the namescaces? If so, then maybe the copy-namespaces declaration can help you.
>
> See here http://www.ibm.com/support/knowledgecenter/SSEPEK_11.0.0/com.ibm.db2z11.doc.xml/src/tpc/db2z_copynamespacesdeclaration.html
www.ibm.com
The copy-namespaces declaration in the query prolog sets the policy for the query. The copy-namespaces policy controls how the namespace bindings are assigned when an ...


> DB2 11 - pureXML guide - Copy-namespaces declaration
> www.ibm.com
> The copy-namespaces declaration in the query prolog sets the policy for the query. The copy-namespaces policy controls how the namespace bindings are assigned when an ...
>
>
>
> Cheers
> Kristian K
>
> 09.07.2016 14:58 Henning Phan kirjutas:
>
>
> 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">
> my.org&nbsp;-&nbspSocial networking Resources and Information.
> 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
>
>