20th February, 2012

XSL Transforms and Namespacing

How to remove the namespace when transforming with XSL

XML document

<?xml version="1.0" encoding="utf-8" ?>
<data xmlns="http://www.data.com/data">
    <results>
        <result>
            <firstname>Jeffrey</firstname>
            <lastname>Bigolow</lastname>
        </result>
        <result>
            <firstname>Jeffrey</firstname>
            <lastname>Bigolow</lastname>
        </result>
    </results>
</data>

Declare the namespaces in your XSL file

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

To remove the namespace of the original document, map it to the default namespace:

<xsl:namespace-alias result-prefix="#default" stylesheet-prefix="data" />

Declare your output type

<xsl:output method="xml" />

Now reference your incoming data with the data prefix in any XPATHs.

 

The opinions expressed here are my own and not those of my employer.