Evil solution to the XSLT empty xmlns probem

I’m currently using XSLT, and I’ve come across the dreaded empty xmlns problem. My XML contains elements that do not have rules in my XSL stylesheet and most XSLT engines append the attribute xmlns="" when it gets confused about what namespace the element belongs to… I get bitten by this because I do not have an input DTD as the document is not meant to be used as anything but fodder to create XHTML.

Many people even have this problem when using XSLT to transform XHTML into XHTML… the input and output namespaces are the same, and they’re using properly formated and validated XHTML documents (complete with the doctype statement and the xmlns attribute on the html element).

Many people just want to force the transforming engine to blindly copy the elements as is over to the new document, and ignore the namespace issue. The below XSL should do this, use wisely.

<xsl:template match="*|@*">
    <xsl:element name="{ local-name( . ) }">
        <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
</xsl:template>

So, now I can go use XHTML in my input and have it spit back out unmolested.

Written by
Open Source software architect and technologist. He's just this guy, you know? Follow him him on Google+.
Published in
Transmissions from the Little Blue Marble

Published March 3rd, 2009

Comments

5 Responses

I tried that solution before, it doesn’t want to work right for me, thus what I did above.

Check out the identity template at the bottom of this page, http://dev.ektron.com/kb_article.aspx?id=492. It copies xhtml without the empty namespaces and has a decent explaination.

Rico dos Santos

My code disappeared from my post for some reason. If you want it you can email me at rico(dot)outask(at)gmail(dot)com

IE7 does properly do XSLT, so that might be your problem.

Rico dos Santos

this code did not work for me. it would try to turn attributes in my html in to elements in the result. I modified it a bit and this works for me.

I am sure that this is horribly inefficient. Also, this solution does not work in IE(tested in IE7). Elements that only have one tag do not work properly. For example, if the xsl:choose was not there tags like will be turned into . Any suggestions to improve this are greatly welcome.

Leave a Reply