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:
<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.
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>
Simple:
<xsl:stylesheet
version="1.0"
xmlns:xsl="..."
xmlns="default output namespace for unprefixed elements"
>
<!-- ... -->
</xsl:stylesheet>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With