views:

1636

answers:

4

Hi, I am using xslt to transform an xml file to html. The .net xslt engine keeps serving me self-closing tags for empty tags.

Example:

<div class="test"></div>

becomes

<div class="test" />

The former is valid html, while the latter is illegal html and renders badly. My question is : How do I tell the xslt engine (XslCompiledTransform) to not use self-closing tags.

If it's not possible, how can I tell my browser (IE6+ in this case) to interpret self-closing tags correctly.

Thank you.

+5  A: 

Change your xsl:output method to be html (instead of xml).

Or add it if you haven't already got the element

<xsl:output method="html"/>
Harry Lime
It dosen't seem to work. I've tried various combinaison of <xsl:output ...> but he keeps giving me xml.
Do you have two <xsl:output> entries by any chance? I'm not using .NET, but Xalan in Java allows that
Harry Lime
No, it's not my case.
A: 

You can't tell your browser to handle invalid HTML as HTML -- you're lucky it understands malformed HTML at all. :)

Definitely do this in your stylesheet:

<xsl:output method="html"/>

But, if your source document has namespaces, this won't do the trick. XSLT processors seem to silently change the output method back to XML if namespace nodes are present in the output.

You need to replace all instances of <xsl:copy-of> and <xsl:copy> with creations of elements with just the local name, e.g.

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

See

etc.

Steven Huwig
I have only the default namespace : xmlns:xsl="http://www.w3.org/1999/XSL/TransformAs you said, I think he switch back to xml.
I meant the namespace in your source data document, not the XSLT document.
Steven Huwig
Ah ok, sorry. I don't use namespace in the soruce document.I think it's the fact I use an XmlTextWriter that force the "xml behavior". Anyways, thank you very much for your help and for your time.
+4  A: 

If you are using XmlWriter as your ouput stream, use HTMLTextWriter instead. XMLWriter will reformat your HTML output back to XML.

catalpa
the problem is that TextWriter is abstract.
Ok, I just found about HtmlTextWriter (from ASP.net - System.Web.UI). I think it holds the solution.TY.