views:

1152

answers:

3

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

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

result:

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

I am using msxsl for the transformation.

A: 

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
I'm assuming you meant exclude-result-prefixes?
0xA3
We're using extension-element-prefixes and it works just fine.
alamar
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
+6  A: 

You can use the exclude-result-prefixes attribute of the xsl:stylesheet element to suppress namespaces from 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:Namespace="http://www.something.com"
         exclude-result-prefixes="Namespace">

</xsl:stylesheet>

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

exclude-result-prefixes="ns1 ns2 ns3"

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.

0xA3
+1  A: 

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