Page 1 of 1

Unwanted namespace not being excluded

Posted: Sat Jan 07, 2017 1:37 am
by mboudreau
Hi all,

I'm doing an XML-to-XML transformation via XSLT 2.0, and a namespace declaration from my source document is appearing in my output despite my adding it to the exclude-result-prefixes list. I'm relatively new to XSLT, so I'm not sure if I've misunderstood how to use the exclude-result-prefixes attribute, or I'm using Oxygen incorrectly, or if there's a bug.

My input document is an instance of NLM Journal Publishing 3.0, which looks like this in abbreviated form:

Code: Select all


<!DOCTYPE article PUBLIC "-//NLM//DTD Journal Publishing DTD v3.0 20080202//EN" "journalpublishing3.dtd">
<article article-type="research-article" dtd-version="3.0" xml:lang="en"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:mml="http://www.w3.org/1998/Math/MathML"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:atict="http://www.arbortext.com/namespace/atict">
<front>...</front>
<body>...</body>
</article>
I'm converting to an instance of JATS, and I want to remove the "atict" namespace declaration from the <article> element (and its descendants).

I'm starting with a very simple spreadsheet:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:atict="http://www.arbortext.com/namespace/atict"
exclude-result-prefixes="atict xs"
version="2.0">

<xsl:output method="xml"/>

<xsl:template match="article">
<article article-type="research-article" xml:lang="en" dtd-version="1.0"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:mml="http://www.w3.org/1998/Math/MathML"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:apply-templates/>
</article>
</xsl:template>

<xsl:template match="*">
<xsl:copy>
<xsl:for-each select="@*">
<xsl:copy/>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
In my output, the children of <article> (<front> and <body>) both have the "atict" namespace declaration.

What am I missing?

Re: Unwanted namespace not being excluded

Posted: Mon Jan 09, 2017 1:39 pm
by adrian
Hi,

"exclude-result-prefixes" applies to created element nodes. However, you're copying elements from somewhere else with xsl:copy. When using xsl:copy, "By default, the namespace nodes of the context node are automatically copied as well, but the attributes and children of the node are not automatically copied."
So, xsl:copy i's where you need to specify that you don't want the namespaces.

Code: Select all

<xsl:template match="*">
<xsl:copy copy-namespaces="no">
Regards,
Adrian

Re: Unwanted namespace not being excluded

Posted: Mon Jan 09, 2017 8:26 pm
by mboudreau
Thanks, Adrian!

Re: Unwanted namespace not being excluded

Posted: Tue Jan 31, 2017 8:07 pm
by mboudreau
I've just realized that although the namespace I'm trying to get rid of is no longer being added to the children (front, body, etc.) of the root element (article), it's being added to the root element itself.

My updated stylesheet looks like this:

Code: Select all


    <xsl:template match="article">
<article article-type="research-article" xml:lang="en" dtd-version="1.0"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:mml="http://www.w3.org/1998/Math/MathML"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:apply-templates select="*"/>
</article>
</xsl:template>

<xsl:template match="*">
<xsl:copy copy-namespaces="no">
<xsl:for-each select="@*">
<xsl:copy/>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
And my output has the "atict" namespace added to the <article> start tag but none of its descendants. I'm trying to banish the namespace completely, but it seems I haven't wrapped my head around one or more of these concepts.

Re: Unwanted namespace not being excluded

Posted: Thu Feb 02, 2017 5:43 pm
by adrian
Hi,

I don't obtain this with what you provided here. Do you still have xsl:stylesheet/@exclude-result-prefixes="atict xs"?
I don't see something in either XML source of stylesheet that would cause that namespace prefix declaration to appear.
Do you have other elements from that namespace (with the atict prefix) in the output?

If you need the element in the output to be in a different namespace than the one from the source, you cannot copy it, you must recreate it. Though this doesn't seem to be needed here. At least not from what you've shown in the XML source.
Something like:

Code: Select all

    <xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
Regards,
Adrian

Re: Unwanted namespace not being excluded

Posted: Sat Feb 04, 2017 12:23 am
by mboudreau
Ah! You're right, of course. After adding @copy-namespaces="no" to my generic template, I had removed 'atict' from @exclude-result-prefixes (and only noticed the effect recently because the <article> start tag runs off past the right margin of my Oxygen window). I put it back, and all is well.

While I have your ear, as it were, do you recommend any particular resources for learning and working with XSLT? I have Doug Tidwell's "XSLT" (2nd edition, O'Reilly) and Michael Kay's "XSLT 2.0 and XPath 2.0" (4th edition, Wrox). Both are more reference than tutorial.

Re: Unwanted namespace not being excluded

Posted: Mon Feb 06, 2017 5:01 pm
by Radu
Hi,

Adrian took a few days off, so to pick up the discussion, we do not have a particular tutorial recommendation for you.
We usually start the new Oxygen XML employees with this XSLT 1.0 tutorial:

http://www.zvon.org/comp/r/tut-XSLT_1.html

but they do not seem to have one available for XSLT 2.0. In time, working on various problems, looking in the XSLT specification to see what various functions to, google searching for solutions they start progressing.

You can also ask around on Stack Overflow for a good XSLT 2.0 tutorial, see what others have to say about it.

Regards,
Radu