Dropping namespaces on import, serialization
Thursday, 22 July 2021 · 3 messages · last reply 23 Jul 2021
JR
Jonathan Robie22 Jul 2021 17:32 #
I am playing with datasets in which namespaces are unhelpful because they make output harder to read. For production use, though, I want the namespaces to be there.
Ideally, I would like to tell BaseX to drop namespaces while serializing. Sometimes. Ideally, I would like to be able to set this as an option in a .bxs file. Is that possible?
Thanks!
Jonathan
LQ
Liam R. E. Quin22 Jul 2021 21:41 #
Quoted text · Jonathan Robie · 11 lines
On Thu, 2021-07-22 at 09:32 -0400, Jonathan Robie wrote: > I am playing with datasets in which namespaces are unhelpful because > they > make output harder to read. For production use, though, I want the > namespaces to be there. > > Ideally, I would like to tell BaseX to drop namespaces while > serializing. > Sometimes. Ideally, I would like to be able to set this as an option > in a > .bxs file. Is that possible?
You could run transform() with some simple XSLT to do this easily. Something like this maybe:
<xsl:stylesheet version="3"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclode-result-prefixes="#all"Quoted text · Jonathan Robie · 1 line
>
<xsl:mode on-no-match="shallow-copy" />
<xsl:template match="*">
<xsl:element name="local-name()">
<xsl:apply-templates select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>Quoted text · Jonathan Robie · 1 line
>
And that can appear literally in your XQuery and passed as the stylesheet node to transform.
Quoted text · Jonathan Robie · 3 lines
> Thanks! > > Jonathan
--
Liam Quin, https://www.delightfulcomputing.com/
Available for XML/Document/Information Architecture/XSLT/
XSL/XQuery/Web/Text Processing/A11Y training, work & consulting. Barefoot Web-slave, antique illustrations: http://www.fromoldbooks.org
CG
For testing purposes, you can always set the BaseX
STRIPNS option when creating a database.
If you want to have your database untouched, a solution in XQuery looks as follows (…we should finally introduce a strip-namespaces() function):declare function local:strip($n) {
typeswitch($n)
case document-node() return
document { $n/(@*, node()) ! local:strip(.) }
case element() return
element { local-name($n) } { $n/(@*, node()) ! local:strip(.) }
case attribute() return
attribute { local-name($n) } { $n }
default return
$n
};
local:strip(
<x xmlns='x' a:a='A' xmlns:a='a'>
<?pi PI ?>BLA<!-- COMMENT -->
<c a:a='A'/>
</x>
)Quoted text · Jonathan Robie · 9 lines
On Thu, Jul 22, 2021 at 3:32 PM Jonathan Robie <jonathan.robie at gmail.com> wrote: > > I am playing with datasets in which namespaces are unhelpful because they make output harder to read. For production use, though, I want the namespaces to be there. > > Ideally, I would like to tell BaseX to drop namespaces while serializing. Sometimes. Ideally, I would like to be able to set this as an option in a .bxs file. Is that possible? > > Thanks! > > Jonathan
Written for BaseX 9.5. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery