Thursday, July 14, 2011

Blank XMLNS Woes

Last week I was working on generating some XHTML via XML and XSLT. One of the goals that I had for the project was to make the output XHTML validate without any errors, using the W3C Markup Validator. I was able to get pretty quickly down to the point of only a handful of errors. Unfortunately, these errors were all regarding an xmlns="" attribute that was appearing on a number of HTML tags.

Further investigation revealed that the attribute was appearing only when I included xmlns="http://www.w3.org/1999/xhtml" on my html tag. Also, I was able to narrow down the issue to HTML markup that was generated in additional XSLT files that I included from my main XSLT file.

I did some additional research, as I wasn't quite sure yet where my clues were leading me. Articles such as this one from Bytes helped me to come down to the root of my issue. The HTML tags with the xmlns="" attributes were not in the same XML namespace as my html tag. In order to fix the issue, I had to move the xmlns attribute from my generated html tag to the xsl:stylesheet tag of each XSLT file that I was using.

The resulting tag looked like this:

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

Running a transform after making this change resulted in the xmlns attribute appearing only on the html tag, as expected. Once this was done, I was able to validate my XHTML successfully.