Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT root tag namespace instead of element attribute namespace

I have a XSL file to transfer another XSL file. I want the namespace declaration to be on the root tag, instead of it being repeated on every single element!!

Here is my stylesheet:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:mynamespace="somenamespace" version="2.0">
    <xsl:output method="xml" omit-xml-declaration="no" standalone="yes" indent="yes" />
    <xsl:template match="myMatchedNode">
        <mynamespace:tag>Some text i want inserted into the xsl</mynamespace:tag>
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" /></xsl:copy>
    </xsl:template>
</xsl:stylesheet>

It outputs something like this:

....

<mynamespace:tag xmlns:mynamespace="somenamespace">Some text i want inserted into the xsl</mynamespace:tag>

....

How do i force the namespace declaration onto the root tag of the result?!

like image 876
JavaRocky Avatar asked May 19 '09 22:05

JavaRocky


People also ask

How do I create a namespace in XSLT?

You'll usually find a namespace declaration in a document element's start-tag, and the XSLT processor passes this declaration along to the start-tag of the result document's document element. It also passes all references to that namespace along to the result tree, making the result document a working XLink document.

What is namespace in XSLT?

The namespace http://www.w3.org/1999/XSL/Transform is referred to as "the XSLT namespace". The prefix xsl is conventionally used to refer to this namespace (and is so used both within this document and within the XSLT specification), but it has no special status: any prefix may be used.

What is root node in XSLT?

In XPath 1.0 (and therefore XSLT 1.0), the container is called the "root node", and the spec uses the term "document element" for the outermost element, though it doesn't play a very significant role, largely because the model supports document nodes having multiple element children.

What is the root element of any xsl file?

The <xsl:stylesheet> element is typically the root element of an XSLT stylesheet.


1 Answers

You need to insert the namespace node onto the document element of your result tree. There are several ways to do this, depending on whether you're using XSLT 1.0 or 2.0. The following is a 2.0 solution. I'm assuming that you're doing a modified identity transform on the input document (your question didn't really specify):

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

  <!-- special rule for the document element -->
  <xsl:template match="/*">
    <xsl:copy>
      <!-- Add a namespace node -->
      <xsl:namespace name="mynamespace" select="'somenamespace'"/>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- the identity template -->
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- the rest of your rules -->

</xsl:stylesheet>

For complete coverage of all the different techniques for controlling namespaces in your output document, check out the "Not enough namespaces" section of the "Namespaces in XSLT" article on my website.

like image 136
Evan Lenz Avatar answered Oct 19 '22 17:10

Evan Lenz