DOCTYPE Output Using .NET and XSL Transforms
I’m using some of the YUI CSS files to make sure fonts look the same when viewed in IE and Firefox. For the Yahoo stuff to work correctly, you need a doctype of HTML 4.01 Strict so that the browser doesn’t go into quirks mode.
The HTML I’m creating is generated from a XSL transform using C# under the .NET Framework 2.0. I based my transform code off an example from MSDN and figured the XSLT file would handle the formatting options from within the xsl:output tag.
<xsl:output method='html' indent='yes' doctype-public='-//W3C//DTD HTML 4.01//EN' doctype-system='http://www.w3.org/TR/html4/strict.dtd' />
I had this problem where every time I ran the XSL transform, the resulting HTML document would have no doctype at all. The problem is that I was passing a XmlWriter to the XslCompiledTransform.Transform method, as is shown in most examples. It makes sense now that I think about it, though: strict HTML isn’t well-formed XML. I tried using XmlOutputMethod to force it into HTML, but, of course, this setting is obtained from the XSL declaration above.
If you’re not outputting an XML document, use a StreamWriter to write the transformed file.
XPathDocument xp = new XPathDocument(xml);
XslCompiledTransform xslt = new XslCompiledTransform();
StreamWriter sw = new StreamWriter(outfile);
xslt.Load(xsl);
xslt.Transform(xp, null, sw);
sw.Close();
For XHTML, I imagine the XmlWriter would work fine.



No Responses to “DOCTYPE Output Using .NET and XSL Transforms”
Please Wait
Leave a Reply