XSLT wont allow me to use self-closing img and br tags

I have some XSLT that gets rendered in the Sitecore 6 CMS, but I don't think that this issue is specific to the product.

If I have a self-closing img or br tag, like so:

<br />
<img src="your.example.com" />

The resulting output would be:

<img src="your.example.com">

The output method of the XSLT file is HTML. Is it supposed to be XML? I'm guessing that self-closing tags are not valid HTML, but is setting it to XML going to cause problems in my output?

13.10.2009 14:55:19

That is just fine. You choose HTML, and <br> tags are allowed in HTML. Choose XML and then you will have what you want.

And yes, you should use XML method if you want self-closing tags. I'm guessing you want XHTML output, and XHTML is a XML document.

13.10.2009 14:59:45
Note that if you have this in your xsl: <xsl:element name="a"><xsl:attribute name="href"><xsl:value-of select="link" /></xsl:attribute></xsl:element>, it will be replaced by <a href="yourlink"/> wich is not accepted by browsers...
Gilbou 23.06.2011 09:01:55
Since XSLT 2.0 you can specify also XHTML as output method: <xsl:output method="xhtml" />.
bluish 26.09.2011 14:07:21
Sitecore 6.5.0 still does not support method xhtml.
Anton 26.01.2012 11:35:19

On the top of your stylesheet you can specify to use XML as the output format and you can also set a specific DOCTYPE, for example:

<xsl:output method="xml" 
    doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
    cdata-section-elements="script style"
13.10.2009 15:02:41

When you do get self closing tags working, you may run accross some odd bugs. Here are some examples:

A couple years ago in IE, I my whole rendered page was blank, but view source showed the full HTML. The problem was a self closing title tag (<title/>).

Also, self closing script tags (<script src="code.js"/>) can cause the JavaScript files not to load, so inside of your XSLT, you may need to have some text inside the script tag to keep it from self closing and get it to work.

<script src="code.js>//</script>
14.10.2009 00:18:13
AFAIR this (without text): <script src="code.js></script> will work too, but indeed, this: <script src="code.js"/> couses problems in IE.
prostynick 13.10.2009 18:45:53
Good point, it was the xslt process that needed the text, not the browser. I updated the answer to reflect this.
Kevin Hakanson 14.10.2009 00:19:12
empty <div> tags will also cause problems.
Adam Weber 15.10.2009 15:00:33