Hi,
We are using BaseX as our backend xml database using the rest interface. Responses to our queries includ an xmlns attribute e.g.
<items xmlns="http://server.my.org/xyz"> <item>Stuff1</item> <item>Stuff2</item> </items>
We are attempting to unmarshall the response using the JAXB client e.g.
ItemList itemList = response.readEntity(new GenericType<List<Item>>(){});
the inclusion of the xmlns attribute prevents JAXB from doing the unmarshall successfully, firstly because it lacks the actual prefix defined for our application eg. xmlns:xyz="http://server.my.org/xyz" and secondly because it forces us to define our own unmarshaller and set up a package-info.java definition with overrides for xml interactions outside our application.
a) Is it possible to disable the addition of the xmlns attribute? b) Is it possible to include the prefix in the xmlns attribute being appended to the root element?
I think we would favor option a) if possible because it causes us the least amount of rework :-)
Thanks in advance for any help and/or suggestions,
MikeE.
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"> <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%22%3E ...
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
I want to point out that the FunctX XQuery Function Library has many functions for manipulating namespaces and/or prefixes.
http://www.xqueryfunctions.com/xq/c0021.html
Best regards Kristian K
09.07.2016 10:06 Christian Grün kirjutas:
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"> <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
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 - Social networking Resources and Information.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%22%3E ...
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
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....
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 - Social networking Resources and Information. 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
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>
====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.... DB2 11 - pureXML guide - Copy-namespaces declarationhttp://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 ...
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.demailto:basex-talk-bounces@mailman.uni-konstanz.de basex-talk-bounces@mailman.uni-konstanz.demailto:basex-talk-bounces@mailman.uni-konstanz.de för Christian Grün christian.gruen@gmail.commailto: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 - Social networking Resources and Information.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%22%3E ...
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
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.... 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 - Social 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
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....
DB2 11 - pureXML guide - Copy-namespaces declarationhttp://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.comhttp://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 - Social 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
Yes you did get it right and I did try the query though sadly java runs out of memory.
In that case, you could try to create a database of your document and run the update operations directly on that database..
1. open your database 2. run the query:
for $e in //* 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 )
If that doesn’t help…
1. delete all nodes without @mark attribute 2. optimize your database and 3. rewrite nodes with the @mark attribute
I rather not prune my xml because It's around 5GB.
What do you mean by pruning?
Hi,
Running it directly lead to the same result, out of memory.
Pruning is deleting branches, or nodes in our case, I learned that terminology from an AI course.
I really wish to see a solution that creates a new xml as it is likely much faster and more memory friendly and traverses fewer nodes rather than deleting nodes from the original which is not only slower but more data needs to be deleted.
( I'm making a lot of assumptions on how the delete works now )
________________________________ Från: Christian Grün christian.gruen@gmail.com Skickat: den 9 juli 2016 21:19 Till: Henning Phan Kopia: BaseX Ämne: Re: [basex-talk] Removing the xmlns attribute and/or adding the prefix
Yes you did get it right and I did try the query though sadly java runs out of memory.
In that case, you could try to create a database of your document and run the update operations directly on that database..
1. open your database 2. run the query:
for $e in //* 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 )
If that doesn't help...
1. delete all nodes without @mark attribute 2. optimize your database and 3. rewrite nodes with the @mark attribute
I rather not prune my xml because It's around 5GB.
What do you mean by pruning?
Running it directly lead to the same result, out of memory.
Is it really the delete operation that causes OOM? That’d be rather unusual, so feel free to give me more details.
If it’s the subsequent insert that causes the troubles, could you possibly give us some details on the size of the database after the optimize call? Did you try to fully optimize the database (…using the ALL flag or db:optimize($db, true())…)?
Hi,
db:optimize($db, true())
Then used the query directly on the database.
Result: out of memory.
Size of database 2176 MB
________________________________ Från: Christian Grün christian.gruen@gmail.com Skickat: den 9 juli 2016 21:53 Till: Henning Phan Kopia: BaseX Ämne: Re: [basex-talk] Removing the xmlns attribute and/or adding the prefix
Running it directly lead to the same result, out of memory.
Is it really the delete operation that causes OOM? That'd be rather unusual, so feel free to give me more details.
If it's the subsequent insert that causes the troubles, could you possibly give us some details on the size of the database after the optimize call? Did you try to fully optimize the database (...using the ALL flag or db:optimize($db, true())...)?
Did you delete and insert nodes on separate steps, as suggested before?
Am 09.07.2016 22:12 schrieb "Henning Phan" henningphan@hotmail.com:
Hi,
db:optimize($db, true())
Then used the query directly on the database.
Result: out of memory.
Size of database 2176 MB
Från: Christian Grün christian.gruen@gmail.com Skickat: den 9 juli 2016 21:53
Till: Henning Phan Kopia: BaseX Ämne: Re: [basex-talk] Removing the xmlns attribute and/or adding the
prefix
Running it directly lead to the same result, out of memory.
Is it really the delete operation that causes OOM? That’d be rather unusual, so feel free to give me more details.
If it’s the subsequent insert that causes the troubles, could you possibly give us some details on the size of the database after the optimize call? Did you try to fully optimize the database (…using the ALL flag or db:optimize($db, true())…)?
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)
On Sat, Jul 9, 2016 at 1:58 PM, Henning Phan henningphan@hotmail.com wrote:
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 - Social 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
basex-talk@mailman.uni-konstanz.de