xsl-list
[Top] [All Lists]

Re: [xsl] Namespaces in output

2021-09-22 09:46:48


On 22 Sep 2021, at 15:37, Terry Ofner tdofner(_at_)gmail(_dot_)com 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

Michael,

Thank you for the clarity. 

So, a modified identity template like the one below will take care of any 
elements not addressed in my stylesheet. Is there any danger in this approach?

<xsl:template match="*">
        <xsl:element name="{name()}" namespace="">
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:element>
</xsl:template>

It removes namespace information. Yes, there's a danger in doing that. But if 
it's what you want to do, then it's the right thing to do.

However, I would use `local-name()` rather than `name()`. If you don't want to 
copy the namespace from the source document, then you probably don't want to 
copy the prefix either.

Any LREs in my stylesheet should employ xsl:element and xsl:attribute at 
construction with namespace=“".

No, you can use LREs to construct elements in no namespace. Just make sure that 
there's an xmlns="" declaration in scope.

Similar constructions should be used throughout, avoiding <xsl:copy> and 
<xsl:copy-of> (except when copying attributes).

xsl:copy and xsl:copy-of are fine if you actually want to copy an element. If 
you want to change it, for example by putting it in a different namespace, then 
they aren't.

Michael Kay
Saxonica


Are the above statement accurate?

Terry



On Sep 22, 2021, at 9:53 AM, Michael Kay mike(_at_)saxonica(_dot_)com 
<mailto:mike(_at_)saxonica(_dot_)com> 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com 
<mailto:xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com>> wrote:

If an element in the result tree is in the XHTML namespace, that's because 
you put it there, and the way to get rid of the namespace declaration is to 
STOP putting it there.

The way you put it there depends on how you created the element. The three 
ways of creating an element are essentially

(a) xsl:element

(b) xsl:copy/copy-of

(c) literal result elements

With xsl:element, the new element goes in the namespace selected in the 
namespace attribute (as you indicate).

With xsl:copy/copy-of, it retains the namespace it had in the source document

With literal result elements, it retains the namespace that the LRE has in 
the stylesheet.

If you want to change the namespace of an element in the source document, 
you can't use xsl:copy/copy-of, you need to use xsl:element.

Michael Kay
Saxonica

On 22 Sep 2021, at 14:43, Terry Ofner tdofner(_at_)gmail(_dot_)com 
<mailto:tdofner(_at_)gmail(_dot_)com> 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com 
<mailto:xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com>> wrote:

I am processing html exported from InDesign documents. As such, Every html 
document comes with the standard doctype and namespace declarations: 

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml <http://www.w3.org/1999/xhtml>">

In the past, I have skirted the namespace tangle by manually removing the 
doctype and replacing <html xmlns="http://www.w3.org/1999/xhtml 
<http://www.w3.org/1999/xhtml>”> with an element name that the Oxygen xslt 
engine would not quibble about, such as <mybook>. Since I have too many 
documents to transform, I have left the doctype and html/namespace in place 
and modified the stylesheet namespace declarations like this:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform 
<http://www.w3.org/1999/XSL/Transform>" 
    version="3.0" 
    xmlns="http://www.w3.org/1999/xhtml <http://www.w3.org/1999/xhtml>" 
    xpath-default-namespace="http://www.w3.org/1999/xhtml 
<http://www.w3.org/1999/xhtml>" 
    xmlns:map="http://www.w3.org/2005/xpath-functions/map 
<http://www.w3.org/2005/xpath-functions/map>" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema 
<http://www.w3.org/2001/XMLSchema>"
    xmlns:saxon="http://saxon.sf.net/ <http://saxon.sf.net/>" 
    exclude-result-prefixes="xs map #default saxon"> 


Well and good. I can address the html elements directly in my stylesheet. 
However, the namespace http://www.w3.org/1999/xhtml 
<http://www.w3.org/1999/xhtml> appears in the root level element of all 
output documents:

<div xmlns="http://www.w3.org/1999/xhtml <http://www.w3.org/1999/xhtml>" 
class="apE">
    <p class="subhead_ap_topic">2.2 Yada Yada</p>
    <p class="subhead_ap_h2">Yuda</p>
    <p class="body_first">NNNN</p>
    <p class="subhead_ap_h4">MMMM</p>
    <p class="body_text">PPPP</p>
</div>

When I remove the namespace at the root level using <xsl:element name=“div” 
namespace="">, the http://www.w3.org/1999/xhtml 
<http://www.w3.org/1999/xhtml> namespace appears on all child elements:

<div class="apE">
    <p xmlns="http://www.w3.org/1999/xhtml <http://www.w3.org/1999/xhtml>" 
class="subhead_ap_topic">2.2 Yada Yada</p>
    <p xmlns="http://www.w3.org/1999/xhtml <http://www.w3.org/1999/xhtml>" 
class="subhead_ap_h2">Yuda</p>
    <p xmlns="http://www.w3.org/1999/xhtml <http://www.w3.org/1999/xhtml>" 
class="body_first">NNNN</p>
    <p xmlns="http://www.w3.org/1999/xhtml <http://www.w3.org/1999/xhtml>" 
class="subhead_ap_h4">MMMM</p>
    <p xmlns="http://www.w3.org/1999/xhtml <http://www.w3.org/1999/xhtml>" 
class="body_text">PPPP</p>
</div>

My question: Is it possible to remove all namespaces from my result 
documents—short of selecting * and rebuilding all elements using 
<xsl:element namespace=“”>? These namespace declarations don’t hamper the 
display of the resulting documents. However, text is text and the extra 
load, over pages and pages of such output can’t but add time to rendering 
pages. Of course, having one namespace declaration at the root level is 
preferable to having all child elements carrying a ns. 

Any help in this would be appreciated. 

Terry


XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/293509> (by 
email <applewebdata://85D959E5-EF8E-4F76-8F15-060D525FE427>)

XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/723745> (by 
email <applewebdata://85D959E5-EF8E-4F76-8F15-060D525FE427>)

XSL-List info and archive <http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe <http://lists.mulberrytech.com/unsub/xsl-list/293509> (by 
email <>)
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>