Re: [xsl] Controlling namespace in output

Subject: Re: [xsl] Controlling namespace in output
From: Peter Desjardins <peter.desjardins.us@xxxxxxxxx>
Date: Fri, 17 Dec 2010 00:15:28 -0500
Hi. Thanks for your input.

I rewrote my stylesheet so it transforms instead of copies. Is this
something like what you meant?

Peter

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="root">
        <xsl:element name="section"
namespace="http://docbook.org/ns/docbook";>
            <xsl:attribute name="version">5.0</xsl:attribute>
            <xsl:attribute name="xml:id">myIdentifier</xsl:attribute>
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>
    <xsl:template match="*">
        <xsl:variable name="elementName">
            <xsl:value-of select="name()" />
        </xsl:variable>
        <xsl:element name="{$elementName}"
namespace="http://docbook.org/ns/docbook";>
            <xsl:for-each select="@*">
                <xsl:copy-of select="." />
            </xsl:for-each>
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

On Thu, Dec 16, 2010 at 6:37 PM, G. Ken Holman
<gkholman@xxxxxxxxxxxxxxxxxxxx> wrote:
> At 2010-12-16 18:27 -0500, Peter Desjardins wrote:
>>
>> I've a few articles on controlling namespaces in output but I could
>> use a little advice. I'm trying to create DocBook 5.0 output. My
>> processor is Saxon HE 9.2.1.1.
>>
>> I added the DocBook namespace to the root element of my output.
>
> Fine ... so you've created the document element in the DocBook namespace,
> but remember your source elements are in no namespace.
>
>> Why do its children have null namespaces (xmlns="")?
>
> Because you copied them from the source tree and the elements in the source
> tree are not in any namespace.
>
>> How can I prevent them from having explicit namespaces?
>
> Don't copy them, transform them.  You have to transform the input element
> that is in no namespace into an element in the DocBook namespace with the
> same local name.  Then the output serialization will know that no namespace
> declaration is needed because the namespace declaration on the document
> element is in scope.
>
> Is that enough to tell you what is wrong, or do you need to see an example
> of how that transformation would be done?
>
> I hope this helps.
>
> . . . . . . . . . . Ken
>
>> Here are the source, stylesheet, desired output, and actual output.
>> Thanks for your help!
>>
>> Peter
>>
>> **********************************
>> Source XML:
>>
>> <root>
>>  <title>A Section Title</title>
>>  <para>Hello</para>
>> </root>
>>
>> **********************************
>> XSLT 2.0 stylesheet:
>>
>> <xsl:stylesheet version="2.0"
>> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
>>        <xsl:output method="xml" indent="yes"/>
>>        <xsl:template match="root">
>>                <xsl:element name="section"
>> namespace="http://docbook.org/ns/docbook";>
>>                        <xsl:attribute name="version">5.0</xsl:attribute>
>>                        <xsl:attribute
>> name="xml:id">myIdentifier</xsl:attribute>
>>                        <xsl:copy-of select="*"/>
>>                </xsl:element>
>>        </xsl:template>
>> </xsl:stylesheet>
>>
>> **********************************
>> Desired output:
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <section xmlns="http://docbook.org/ns/docbook"; version="5.0"
>> xml:id="myIdentifier"
>>  <title>A Section Title</title>
>>  <para>Hello</para>
>> </section>
>>
>> **********************************
>> Actual output:
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <section xmlns="http://docbook.org/ns/docbook"; version="5.0"
>> xml:id="myIdentifier">
>>   <title xmlns="">A Section Title</title>
>>   <para xmlns="">Hello</para>
>> </section>
>
>
> --
> Contact us for world-wide XML consulting & instructor-led training
> Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
> G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
> Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread