the singularity of being and nothingness
Interesting XSLT Gotcha
Today, I got around to trying to tackle some of the XHTML validation errors we've been receiving on our sites at work. One of the peskiest errors was what appeared to be a simple unclosed <img> tag–easy to fix, right? Well, I promptly navigated to the xslt file, opened it up, and quickly found the offending tag.
…
…it was already closed.
…
…hmmm…
Honestly, I couldn't figure out what was going on. Just to make sure, I recopied the most recent, seemingly correct version.
No change.
I went back to the file, made ABSOLUTELY sure that the tag was closed and that there were no weird quotes floating around.
No change.
By now, I was desperate. I even started commenting out random bits of code around the <img> tag until I was POSITIVE that nothing else was causing this issue.
No change.
Blurg.
Well, it turns out the fix is pretty easy, and it all has to do with the "method" attribute of the <xsl:output> directive. On my xslt file (which I did not create, BTW), the method was set to "HTML" like so:
<xsl:output method="HTML" …./>
For whatever reason, this method of output can do funky things like remove the self-closing parts of tags. Dumb.
Anyway, I simply switched the output method to XML and it resolved the issue. Not only that, but it knocked out about 10 other validation errors that I hadn't even started looking at yet. Woot!
So if you're interested in learning more about this xsl:output nonsense, be sure to check out the W3C definition…it's a riveting read, but does–at least–provide the right answer 🙂
Print article | This entry was posted by existdissolve on October 8, 2009 at 11:26 am, and is filed under Web Development. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about -1916 years ago
It’s a common mistake, but if you consider that XSLT 1.0 was finalized in 1998/99, long before XHTML came about, you may understand this "pirky" behavior. HTML is *not* XML and self-closing did not exist at the time. Hence the different output method.
Another misconception is that XSLT can only create and parse XML. Though you don’t mention that here, this is not true. Both HTML and TEXT are allowed output methods that are not XML.
Since XSLT 2.0, the output method XHTML was added. This was necessary, because <br/> is valid XML, but XHTML needs it as <br />. Also, <script src=…/> is valid XML, but XHTML needs it as <script src=…></script>.
With XSLT 1.0, you will find that empty elements like <script> are self-closed. Browsers like Internet Explorer 6 and 7 will not understand this and will not parse these elements correctly or even at all (same with the space in the <br /> or <p /> versus <p></p>).
Be careful with the change from HTML output (which is best compared with HTML 3.2 or 4) to XML output to force correct XHTML. Any non-empty element must have content, even if it doesn’t on your page. The "de facto" way to do this is the simple trick of adding a piece of comment: <xsl:comment /> in the elements that cannot be self-closed (script, style, p, div, span to name a few).
— Abel —
about -1916 years ago
Abel–
Wow, thanks for the explanation! I appreciate the detail, and I’ll have to read up on this a bit more to complete my understanding. Thanks again!