XQuery output without namespace declaration
Wednesday, 25 August 2021 · 9 messages
YA
Yann NICOLAS (ABES)25 Aug 2021 15:12 #
Hi all,
I am looking for the equivalent instruction of XSLT attribute @omit-xml-declaration in XQuery.
My input is :
Quoted text · 11 lines
> <marc> > <marc:record *xmlns:marc="http://www.loc.gov/MARC21/slim" > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"*> > <marc:leader>01321nam a22002772a 4500</marc:leader> > <c level="item"> > <did> > <unitid type="division">1808001595</unitid> > </did> > </c> > </marc:record> > <marc>
My query is :
Quoted text · 5 lines
> declare namespace marc="http://www.loc.gov/MARC21/slim"; > > for $record in db:open('marcxml_full')/marc/marc:record > return > insert node $record/c into db:open('uk_parlement_ead')/dsc
The ouput is :
Quoted text · 6 lines
> <c *xmlns:marc="http://www.loc.gov/MARC21/slim" > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" *level="item"> > <did> > <unitid type="division">1808001595</unitid> > </did> > </c>
As you can see, there are no marc: elements in this output, hence no need for the marc: namespace.
How to get rid of @xmlns:marc and xmlns:xsi ?
Thanks a lot for your help !
Regards
Yann
MH
Martin Honnen25 Aug 2021 15:20 #
Quoted text · Yann NICOLAS (ABES) · 7 lines
Am 25.08.2021 um 15:12 schrieb Yann NICOLAS (ABES): > > Hi all, > > I am looking for the equivalent instruction of XSLT attribute > @omit-xml-declaration in XQuery. >
In the Query prolog you can declare
declare namespace output =
"http://www.w3.org/2010/xslt-xquery-serialization";
declare option output:method 'xml';
declare option output:omit-xml-declaration 'yes';Quoted text · Yann NICOLAS (ABES) · 32 lines
> My input is : > >> <marc> >> <marc:record *xmlns:marc="http://www.loc.gov/MARC21/slim" >> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"*> >> <marc:leader>01321nam a22002772a 4500</marc:leader> >> <c level="item"> >> <did> >> <unitid type="division">1808001595</unitid> >> </did> >> </c> >> </marc:record> >> <marc> > > My query is : > >> declare namespace marc="http://www.loc.gov/MARC21/slim"; >> >> for $record in db:open('marcxml_full')/marc/marc:record >> return >> insert node $record/c into db:open('uk_parlement_ead')/dsc > The ouput is : > >> <c *xmlns:marc="http://www.loc.gov/MARC21/slim" >> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" *level="item"> >> <did> >> <unitid type="division">1808001595</unitid> >> </did> >> </c> > As you can see, there are no marc: elements in this output, hence no > need for the marc: namespace. > How to get rid of @xmlns:marc and xmlns:xsi ?
That seems to be a different issue from the XML declaration, if the nodes you copy from the one db into the other have a namespace in scope than that namespace is copied through.
Perhaps
insert node $record/c ! element { node-name() } { @*, node() } into
db:open('uk_parlement_ead')/dscdoes what you need.
YA
Yann NICOLAS (ABES)25 Aug 2021 16:50 #
Quoted text · Martin Honnen · 20 lines
Le 25/08/2021 à 15:20, Martin Honnen a écrit : > > > Am 25.08.2021 um 15:12 schrieb Yann NICOLAS (ABES): >> >> Hi all, >> >> I am looking for the equivalent instruction of XSLT attribute >> @omit-xml-declaration in XQuery. >> > > In the Query prolog you can declare > > > declare namespace output = > "http://www.w3.org/2010/xslt-xquery-serialization"; > > declare option output:method 'xml'; > declare option output:omit-xml-declaration 'yes'; >
Thanks Martin.
In the user interface, it is working when the xml source is stored in a let variable, but it's not when the xml source is retrieved from a BaseX db.
Quoted text · Martin Honnen · 12 lines
> declare namespace marc="http://www.loc.gov/MARC21/slim"; > > declare namespace output = > "http://www.w3.org/2010/xslt-xquery-serialization"; > > declare option output:method 'xml'; > declare option output:omit-xml-declaration 'yes'; > > for $record in *db:open('marcxml_full')*//marc:record[1] > > return > $record/c
YA
Yann NICOLAS (ABES)25 Aug 2021 17:16 #
Actually, i am trying to omit the *namespaces*, not the xml declaration. Sorry for the mistake in my first post, Martin.
So i am looking for something like : *@exclude-result-prefixes* (as found in XSLT 3.0).
And indeed, in BaseX (9.5.1), it seems to be a different behaviour if the xml source is stored in a let variable or in a db. If the xml source is stored in a variable, the exclusion of prefixes is automatic, as in :
Quoted text · Yann NICOLAS (ABES) · 19 lines
> declare namespace marc="http://www.loc.gov/MARC21/slim"; > > let $xml := > <marc> > <marc:record xmlns:marc="http://www.loc.gov/MARC21/slim" > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> > <marc:leader>01321nam a22002772a 4500</marc:leader> > <c level="item"> > <did> > <unitid type="division">1808001595</unitid> > </did> > </c> > </marc:record> > </marc> > > for $record in $xml/marc:record[1] > > return > $record/c
which gives a nice :
Quoted text · Yann NICOLAS (ABES) · 47 lines
> <c level="item"> > <did> > <unitid type="division">1808001595</unitid> > </did> > </c> Le 25/08/2021 à 16:50, Yann NICOLAS (ABES) a écrit : > > > Le 25/08/2021 à 15:20, Martin Honnen a écrit : >> >> >> Am 25.08.2021 um 15:12 schrieb Yann NICOLAS (ABES): >>> >>> Hi all, >>> >>> I am looking for the equivalent instruction of XSLT attribute >>> @omit-xml-declaration in XQuery. >>> >> >> In the Query prolog you can declare >> >> >> declare namespace output = >> "http://www.w3.org/2010/xslt-xquery-serialization"; >> >> declare option output:method 'xml'; >> declare option output:omit-xml-declaration 'yes'; >> > Thanks Martin. > > In the user interface, it is working when the xml source is stored in > a let variable, but it's not when the xml source is retrieved from a > BaseX db. > >> declare namespace marc="http://www.loc.gov/MARC21/slim"; >> >> declare namespace output = >> "http://www.w3.org/2010/xslt-xquery-serialization"; >> >> declare option output:method 'xml'; >> declare option output:omit-xml-declaration 'yes'; >> >> for $record in *db:open('marcxml_full')*//marc:record[1] >> >> return >> $record/c >
MH
Martin Honnen25 Aug 2021 17:20 #
Quoted text · Yann NICOLAS (ABES) · 5 lines
Am 25.08.2021 um 17:16 schrieb Yann NICOLAS (ABES): > > Actually, i am trying to omit the *namespaces*, not the xml > declaration. Sorry for the mistake in my first post, Martin. >
Did you see my other suggestion in the first answer to try to change your XQuery code to use
insert node $record/c ! element { node-name() } { @*, node() } into
db:open('uk_parlement_ead')/dscYA
Yann NICOLAS (ABES)25 Aug 2021 17:41 #
Quoted text · Martin Honnen · 15 lines
Le 25/08/2021 à 17:20, Martin Honnen a écrit : > > > Am 25.08.2021 um 17:16 schrieb Yann NICOLAS (ABES): >> >> Actually, i am trying to omit the *namespaces*, not the xml >> declaration. Sorry for the mistake in my first post, Martin. >> > > Did you see my other suggestion in the first answer to try to change > your XQuery code to use > > insert node $record/c ! element { node-name() } { @*, node() } > into db:open('uk_parlement_ead')/dsc >
Oh, i missed it ! Sorry.
It is working ... at the <c> level :
Quoted text · Martin Honnen · 21 lines
> <c level="item"> > <did *xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > xmlns:marc="http://www.loc.gov/MARC21/slim"*> > <unitid type="division">1808001595</unitid> > <unittitle>Select Committee on Acts regarding Use of Broad Wheels, > and Regulations for Preservation of Turnpike Roads and Highways of > United Kingdom First Report, Appendix</unittitle> > <unitdate>1808</unitdate> > <physfacet>Vol.II, 135 pp.</physfacet> > <extent>225 pages</extent> > <origination>xxxxxxxxxxxxxx xxxxxxxxx xxxxxxxxx</origination> > <dao>HTTP://parlipapers.proquest.com/parlipapers/docview/t70.d75.1808-001595</dao> > </did> > <controlaccess xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > xmlns:marc="http://www.loc.gov/MARC21/slim"> > <subject role="sujet">Roads and bridges</subject> > <subject role="sujet">Transport and communication</subject> > <subject role="sujet">Turnpike roads</subject> > <subject role="sujet">Turnpike roads and trusts</subject> > </controlaccess> > </c>
MH
Martin Honnen25 Aug 2021 17:54 #
Quoted text · Yann NICOLAS (ABES) · 15 lines
Am 25.08.2021 um 17:41 schrieb Yann NICOLAS (ABES): >> >> Did you see my other suggestion in the first answer to try to change >> your XQuery code to use >> >> insert node $record/c ! element { node-name() } { @*, node() } >> into db:open('uk_parlement_ead')/dsc >> > Oh, i missed it ! Sorry. > > It is working ... at the <c> level : > >> <c level="item"> >> <did *xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >> xmlns:marc="http://www.loc.gov/MARC21/slim"*>
I see, I guess one way would be to write a recursive function you pass that
c element to that then recreates any element like in the above snippet, only recursively e.g.declare function local:strip-namespaces($node as node()) as node()
{
typeswitch($node)
case element()
return element { node-name($node) } { $node/@*,
$node/node()!local:strip-namespaces(.) }
default
return $node};
and then use e.g.
insert node $record/c ! local:strip-namespaces(.) into
db:open('uk_parlement_ead')/dscHopefully there is an easier way using XQuery update instructions but I am not good enough with them to know.
CG
Christian Grün25 Aug 2021 18:00 #
Hi Yann,
The common way to do this is as Martin proposed: You can strip namespaces via a recursive function. See [1] for an example.
As this is a frequently asked feature, it would about time to define a mode or built-in function for this purpose in BaseX. An equivalent solution for exclude-result-prefixes could also be proposed for XQuery 4.0 [2]…
Salutations,
Christian
[1] https://www.mail-archive.com/basex-talk at mailman.uni-konstanz.de/msg13678.html [2] https://github.com/qt4cg/qtspecs/issues
Quoted text · Martin Honnen · 39 lines
On Wed, Aug 25, 2021 at 5:54 PM Martin Honnen <martin.honnen at gmx.de> wrote: > > > Am 25.08.2021 um 17:41 schrieb Yann NICOLAS (ABES): > > Did you see my other suggestion in the first answer to try to change your XQuery code to use > > insert node $record/c ! element { node-name() } { @*, node() } into db:open('uk_parlement_ead')/dsc > > Oh, i missed it ! Sorry. > > It is working ... at the <c> level : > > <c level="item"> > <did xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:marc="http://www.loc.gov/MARC21/slim"> > > > > I see, I guess one way would be to write a recursive function you pass that `c` element to that then recreates any element like in the above snippet, only recursively e.g. > > > declare function local:strip-namespaces($node as node()) as node() > { > typeswitch($node) > case element() > return element { node-name($node) } { $node/@*, $node/node()!local:strip-namespaces(.) } > default > return $node > > }; > > > and then use e.g. > > > insert node $record/c ! local:strip-namespaces(.) into db:open('uk_parlement_ead')/dsc > > > Hopefully there is an easier way using XQuery update instructions but I am not good enough with them to know.
YA
Yann NICOLAS (ABES)25 Aug 2021 18:13 #
@Martin You solved my problem ! Thanks a lot because i have learned a lot today (! operator, typeswitch).
@Christian Indeed it would be a convenient feature in BaseX, even better in the spec.
Yann
Quoted text · Christian Grün · 58 lines
Le 25/08/2021 à 18:00, Christian Grün a écrit : > Hi Yann, > > The common way to do this is as Martin proposed: You can strip > namespaces via a recursive function. See [1] for an example. > > As this is a frequently asked feature, it would about time to define a > mode or built-in function for this purpose in BaseX. An equivalent > solution for exclude-result-prefixes could also be proposed for XQuery > 4.0 [2]… > > Salutations, > Christian > > [1] https://www.mail-archive.com/basex-talk at mailman.uni-konstanz.de/msg13678.html > [2] https://github.com/qt4cg/qtspecs/issues > > > > > On Wed, Aug 25, 2021 at 5:54 PM Martin Honnen <martin.honnen at gmx.de> wrote: >> >> Am 25.08.2021 um 17:41 schrieb Yann NICOLAS (ABES): >> >> Did you see my other suggestion in the first answer to try to change your XQuery code to use >> >> insert node $record/c ! element { node-name() } { @*, node() } into db:open('uk_parlement_ead')/dsc >> >> Oh, i missed it ! Sorry. >> >> It is working ... at the <c> level : >> >> <c level="item"> >> <did xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:marc="http://www.loc.gov/MARC21/slim"> >> >> >> >> I see, I guess one way would be to write a recursive function you pass that `c` element to that then recreates any element like in the above snippet, only recursively e.g. >> >> >> declare function local:strip-namespaces($node as node()) as node() >> { >> typeswitch($node) >> case element() >> return element { node-name($node) } { $node/@*, $node/node()!local:strip-namespaces(.) } >> default >> return $node >> >> }; >> >> >> and then use e.g. >> >> >> insert node $record/c ! local:strip-namespaces(.) into db:open('uk_parlement_ead')/dsc >> >> >> Hopefully there is an easier way using XQuery update instructions but I am not good enough with them to know.
Written for BaseX 9.6. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery