Hi,
Thank you Kristian for your tips, though the namespace was still not included, maybe I used it incorrectly?
To answer Christian:
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 rather not prune my xml because It's around 5GB but the marked nodes are around 20 MB.
If it's relevant how the nodes are marked. There are some nodes we are interested in and those nodes and all their ancestors are marked.
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>
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 ...
|
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 nodes2. If the @mark is empty I want to remove the mark attribute3. 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 calledFunction 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 prefixHi 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.orgmy.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