Re: [xsl] Effect of default namespace specifications on imports

Subject: Re: [xsl] Effect of default namespace specifications on imports
From: Ihe Onwuka <ihe.onwuka@xxxxxxxxx>
Date: Sun, 28 Oct 2012 08:36:30 +0000
On Sun, Oct 28, 2012 at 1:02 AM, Ihe Onwuka <ihe.onwuka@xxxxxxxxx> wrote:
> On Thu, Oct 25, 2012 at 5:04 PM, G. Ken Holman
> <gkholman@xxxxxxxxxxxxxxxxxxxx> wrote:
>> At 2012-10-25 16:29 +0100, Ihe Onwuka wrote:
>>>
>>> I am writing an XML conversion, part of which entails bringing things
>>> into the new namespace (from no namespace).
>>>
>>> I have specified namespace X as my default namespace and my XPath
>>> default namespace but all is not as expected.
>>
>>
>> I doubt that will work for you.  Your input is in no namespace so any
>> addressing you need of the input must be in no namespace, so the XPath
>> default namespace should not be set to anything.
>>
>>
>>> I suspect it is because some elements are handled by an imported
>>> identity template. Although this can be worked around this represents
>>> an opportunity to get some clarification as to what the precise rules
>>> are as relates to default namespace specfications and imported
>>> stylesheets.
>>
>>
>> There are no such rules regarding a relationship between default namespace
>> specifications and imported stylesheets.  Each stylesheet fragment is
>> standalone.  Any use of the XML default namespace or the XPath default
>> namespace is in scope of only the stylesheet fragment.  There is no
>> knowledge between stylesheet fragments of the use of the namespaces in other
>> fragments.
>>
>> And I suspect the identity template will not help you if you have to change
>> the namespace for each element.
>>
>
> Here is the algorithm I used to handle renamespacing and other
> template processing in a single pass (well it worked for me).
>
> Import the identity template.
> Make the new namespace the default namespace of the main stylesheet.
>
> The following template will renamespace all elements  that do not have
> an explicit template rule.
>
>   <xsl:template match="*">
>     <xsl:element name="{local-name()}">
>       <xsl:apply-templates/>
>     </xsl:element>
>   </xsl:template
>
> For your other template rules, any elements that you <xsl:copy> will
> stubbornly stay in the old namespace so replace any <xsl:copy>
> elements in other templates with <xsl:element name="{local-name()}">.

Above is the ambidextrous solution (XSLT 1.0 and 2.0)

For 2.0 only the <xsl:element name="{local-name}"> can be replaced by
<xsl:copy copy-namespaces="no">

Current Thread