52

I'd like to take data from some XML files and transform them into a new XML document. However, I do not want the definition of a namespace in the XSLT to occur in the result document.

In other words:

source:

<Namespace:Root xmlns:Namespace="http://www.something.com">

stylesheet:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:Namespace="http://www.something.com">

result:

<resultRoot xmlns:Namespace="http://www.something.com">
<!--I don't want the Namespace definition above-->

I am using msxsl for the transformation.

Cœur
  • 37,241
  • 25
  • 195
  • 267
pypmannetjies
  • 25,734
  • 7
  • 39
  • 49

3 Answers3

93

You can use the exclude-result-prefixes attribute of the xsl:stylesheet element to avoid emitting namespace prefixes into the output document:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         xmlns:prefix1="http://www.something.com"
         exclude-result-prefixes="prefix1">

</xsl:stylesheet>

To suppress multiple namespaces from the output document specify them separated by whitespace:

exclude-result-prefixes="prefix1 prefix2 prefix3"

From the XSLT specification:

When a stylesheet uses a namespace declaration only for the purposes of addressing the source tree, specifying the prefix in the exclude-result-prefixes attribute will avoid superfluous namespace declarations in the result tree.

Cheeso
  • 189,189
  • 101
  • 473
  • 713
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
15

divo's answer was already chosen, and appropriately so.

But if you're interested in digging deeper, check out the "Too many namespaces" section in my magnum opus on the wildly popular topic of "Namespaces in XSLT". (Yes, that's meant to be tongue-in-cheek. :-) )

Evan Lenz
  • 4,086
  • 1
  • 19
  • 18
  • Hi @Evan! A very helpful link, indeed! The trick for not having the undesired namespaces while using copy is just what I wanted. What surprised me, though, is that this trick really does not work for XSLT 2.0 (e.g. using XALAN) but you have to follow the path that you sketched for 2.0. In XSLT 1.0 (e.g. using `xsltproc`) it worked fine. So switching from a XSLT 1.0 to a XSLT 2.0 processors can actually change the output significantly although semantically the extra namespaces should not be a problem downstream except maybe for volume aspects. – Marcus Rickert Oct 27 '13 at 11:36
  • This is the REAL answer to the ACTUAL question! Tanks for providing me with a solution for XSLT 1.0! – JERKER Jun 27 '18 at 08:10
-4

use extension-element-prefixes="Namespace"

like:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:datetime="http://exslt.org/dates-and-times"
xmlns:str="http://exslt.org/strings"
xmlns:exsl="http://exslt.org/common"
xmlns:uw="xalan://ru.sbtc.util.XSLUtil"
extension-element-prefixes="exsl str datetime uw"
version="1.0">
alamar
  • 18,729
  • 4
  • 64
  • 97
  • I'm assuming you meant exclude-result-prefixes? – Dirk Vollmar May 13 '09 at 09:43
  • We're using extension-element-prefixes and it works just fine. – alamar May 13 '09 at 09:52
  • 3
    extension-element-prefixes does have the same effect, but it has an additional effect. Any elements that you put in one of those namespaces will be interpreted as an extension element (rather than a literal result element). That may well be appropriate for the examples you have in your answer. But if you don't want that additional behavior, then just use exclude-result-prefixes – Evan Lenz May 13 '09 at 10:08