Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I dynamically set the default namespace declaration of an XSLT transformation's output XML?

Tags:

xslt

I can do it, but not for the default namespace, using the <xsl:namespace>. If I try to do it for the default namespace:

<xsl:namespace name="" select"myUri"/>

it never works. It demands that I explicitly define the namespace of the element to be able to use the above null prefix declaration.
The reason I want this is because I have a task to transform an input XML file to another output xml. The output XML has many elements and i dont want to have to explicitly set the namespace for every element. Thats why I want to set the default and never bother again. But the default must be computed from some data in the source XML. It does not change during the whole transformation, but it is dependent on input XML data. Any solution?

EDIT 1: To sup up:

  1. I want to create a namespace dynamically and set it to be the default namespace of the output xml document. The uri of the namespace is derived from some data in the input XML.
  2. If I use <xsl:namespace> in my root output element, I cannot create a default namespace for it, only a prefixed one. And even with the prefixed one, it does not propagate to children.

EDIT 2: dkackman proposed:

<xsl:template match="root">
  <xsl:param name ="ns">my-computed-namespace</xsl:param>
  <xsl:element name="newRoot" namespace="{$ns}"/>
</xsl:template>

It almost solves the problem. Unfortunately the children are injected with ""(blank) namespace by the transformer. Here is what I get if I put a child element:

<newRoot xmlns="my-computed-namespace"> 
    <child xmlns=""> ... 
    </child> 
 </newRoot>

Why does the transformer put this xmlns="" in the children? If I can prevent this then I have found my solution.

like image 827
Paralife Avatar asked Mar 12 '10 13:03

Paralife


2 Answers

In addition to @Tomalak who has provided the precise answer, do note that <xsl:namespace> is not intended to create a namespace declaration to be used by the XSLT processor generally with all elements or attributes.

The purpose of <xsl:namespace> is to create a specific namespace node. This node has a limited scope only: the current element or attribute, and all children of the current node, if they do not re-assign the prefix to another namespace-uri.

Using <xsl:namespace> is necessary only if we want to create a namespace dynamically for a namespace-uri that must be generated dynamically (was not known statically at the start of the transformation). Such cases are extremely rare.

In all cases when the desired namspace-uri is known statically, simply declare this namespace at a suitable level of visibility (usually at the <xsl:stylesheet> instruction) and then simply use the associated prefix, anywhere this namespace must be used.

UPDATE: I have just confirmed in a dialog with specialists in another forum that this is not possible to do with <xsl:namespace>. It adds a namespace node with no name to the current element, but literal result elements are copied 1:1 and remain in their (no) namespace.

Here is how Dr. Michael Kay, the Editor of the W3C WG on XSLT explains this:

"You need to create elements and attributes with the correct expanded name at the time you create them. If that means using xsl:element, so be it. xsl:namespace can only be used to create additional namespace nodes to those that are created automatically for the prefixes/uris used in element and attribute names; it can't be used to modify the name of an element or attribute node. As always, to understand this you need to understand the data model for namespaces. An element/attribute name is a triple, containing (prefix, uri, localname). A namespace node is a pair (prefix, uri). There is a consistency rule that if an element or attribute name exists containing prefix=P uri=U then there must be a namespace node (P, U). The namespace fixup process ensures that this namespace node is created automatically when you create an element or attribute. xsl:namespace is there to allow you to create additional namespace nodes, typically for namespaces used in QName-valued content".

If such result is needed, the solution is to use a second pass and to convert any element belonging to "no namespace" to the desired new namespace.

This is the transformation to use in the second pass (the two passes can be combined into a single stylesheet/transformation):

<xsl:stylesheet  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:variable name="vUrl" select="'my:Url'"/>

 <xsl:template match="*[namespace-uri()='']">
   <xsl:element name="{name()}" namespace="{$vUrl}">
     <xsl:copy-of select="@*"/>
     <xsl:apply-templates/>
   </xsl:element>

 </xsl:template>
</xsl:stylesheet>

When the above transformation is applied on the following sample (pass-1-result) xml document:

<a>
  <b>
    <c/>
  </b>
</a>

The desired result is produced:

<a xmlns="my:Url">
    <b>
        <c/>
    </b>
</a>
like image 155
Dimitre Novatchev Avatar answered Oct 08 '22 13:10

Dimitre Novatchev


Simple:

<xsl:stylesheet
 version="1.0"
 xmlns:xsl="..."
 xmlns="default output namespace for unprefixed elements"
>
  <!-- ... -->
</xsl:stylesheet>
like image 23
Tomalak Avatar answered Oct 08 '22 15:10

Tomalak