views:

2026

answers:

2

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?!

A: 

Perhaps I misunderstand the question, but I think you should do this:

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

<xsl:output method="xml" omit-xml-declaration="no" standalone="yes"  indent="yes"/>

<xsl:template match="myMatchedNode">
  <tag>Some text i want inserted into the xsl</mynamespace>

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

</xsl:stylesheet>

That should allow you to use your namespace tags as the default namespace and the output document will have them as the default namespace as well. Your input document can still use the nasty mynamespace:tag syntax.

You could try doing this, too:

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

I'm not entirely sure that will replace mynamespace:tag with just tag though, and if it does, it may be implementation dependent.

easel
No, i need <mynamespace:tag> as the default namespace is something else. This XSL is a part of a XSLT pipeline.
JavaRocky
+3  A: 

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"&gt;

  <!-- 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.

Evan Lenz
You sir, are a legend. Insane link! Thank you!
JavaRocky
Oh! And i have your book. x2 Legend status.
JavaRocky
Thanks for your too-kind words. :-)
Evan Lenz