XSL: Avoid exporting namespace definitions to resulting XML documents

pypmannetjies picture pypmannetjies · May 13, 2009 · Viewed 60.4k times · Source

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.

Answer

Dirk Vollmar picture Dirk Vollmar · May 13, 2009

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.