XSLT <xsl: output> support in major browsers
I want to convert an input XML document to XHTML via XSLT. In my stylesheet, I use xsl: output with the following attributes:
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" />
Converting to XTHML 1.0 Strict works fine when I use the XSLT processor in editors like XML Copy or Editix. It works as expected when I use the xsltproc command line as well.
But when I link my stylesheet ("myfile.xsl") to the source XML document ("myfile.xml") as follows:
<?xml-stylesheet type="text/xsl" href="myfile.xsl"?>
if I try to look at "myfile.xml" now in the main browsers (Chrome, IE or Mozilla), none of them can convert the XML document to the expected XHTML. However, with Opera, it works great.
Is there something wrong with my XSLT (namely, xsl: output) or is it a flaw in the XSLT implementation of the main browsers (IE, Chrome, Mozilla)?
The problem only occurs when I use the attribute = "xml" in method. If I use the = "html" method, it works in all browsers. But I need to generate XHTML, not HTML, so I use the = "xml" method along with the doctype-system and doctype-public attributes in xsl: output
Well, you really didnโt tell us how browsers fail. If you want to create an XHTML output, make sure you use the XHTML namespace for your result elements, i.e. put
<xsl:stylesheet xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" /> <xsl:template match="/"> <html>...<xsl:apply-templates/>...</html> </xsl:template> </xsl:stylesheet>
in your code to make sure that the result elements are XHTML elements (and not XML elements in the namespace that have local names like "html" but are not recognized as XHTML).
I am sure that the Firefox / Mozilla browser thus recognizes XHTML elements using the xml
output method. And IE 9 and 10, I think I'm not sure that older versions of IE with limited XHTML support will work.
To give an example, the XML input http://home.arcor.de/martin.honnen/xslt/test2013040601.xml is converted to xml
via http://home.arcor.de/martin.honnen/xslt/test2013040601.xsl and excellent works with IE 10 and the current version of Firefox and Chrome on Windows 8.
There are several major issues with client XSLT:
- XHTML document URLs are blocked by W3C in IE, so a patch is required
- Firefox's XML serializer is used to output XHTML, so it will revert to text if the XHTML namespace is not used
media-type
attribute must be defined astext/html
for Chrome
Here is a stylesheet with a link that will work when saved as html5.xml
:
<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="html5.xml"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" > <xsl:output method="xml" encoding="utf-8" version="" indent="yes" standalone="no" media-type="text/html" omit-xml-declaration="no" doctype-system="about:legacy-compat" /> <xsl:template match="xsl:stylesheet"> <xsl:apply-templates/> </xsl:template> <xsl:template match="/"> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> </head> <body> <xsl:text>hi</xsl:text> </body> </html> </xsl:template> </xsl:stylesheet>
Here are some unrelated questions that explain other issues between browsers:
References
Most browsers only support XSLT 1.0, you should take a look at SaxonCE to add support for XSLT 2.0
Saxon-CE (client version) is an implementation of Saxonica XSLT 2.0 for use in web browsers.
Features
Beware of XPath 2.0 support .