XSLT <xsl:output> support in main browsers
Asked Answered
B

4

1

I want to transform an input XML document into XHTML via XSLT. In my stylesheet I'm using 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" />

The transformation into XTHML 1.0 Strict works fine when I use the XSLT Processor in editors like XML Copy Editor or Editix. It works as expected when I use the command line xsltproc, too.

But when I link my stylesheet ("myfile.xsl") into the original XML document ("myfile.xml") like this:

<?xml-stylesheet type="text/xsl" href="myfile.xsl"?>

if I try to watch now "myfile.xml" in the major browsers (Chrome, IE or Mozilla), none of them is capable of transforming the XML document in the expected XHTML. With Opera, however, it works perfectly.

Is there something wrong in my XSLT (namely in the xsl:output) or is this a flaw in the XSLT implementation of the major browsers (IE, Chrome, Mozilla)?

The problem occurs only when I use the attribute method="xml" in . If I use method="html", it works in all browsers. But I need to generate XHTML, not HTML, that's why I use method="xml" along with the doctype-system & doctype-public attributes in xsl:output

Baelbeer answered 6/4, 2013 at 12:44 Comment(0)
M
2

Well you haven't really told us in what way the browsers fail. If you want to create XHTML output then 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 the result elements are XHTML elements (and not XML elements in no namespace that happen to have local names like 'html' but are not recognizable as XHTML).

I am pretty sure that Firefox/Mozilla browsers that way with output method xml recognize the XHTML elements. And IE 9 and 10 as well I think, I am not sure older versions of IE with limited XHTML support will work.

To give you an example, the XML input http://home.arcor.de/martin.honnen/xslt/test2013040601.xml is transformed to xml output via http://home.arcor.de/martin.honnen/xslt/test2013040601.xsl and works fine with IE 10 and current version of Firefox and Chrome on Windows 8.

Mckinney answered 6/4, 2013 at 13:15 Comment(3)
The expected result of the transformation was a hmlt table. Instead, Mozilla & IE showed just a string with the concatenation of the text contents of all the cells. Chrome was showing nothing, just a blank page. I've added the default xhtml namespace to the stylesheet, as you suggested. Mozilla & IE are showing the table ok, now. Chrome keeps showing a blank page (?). By the way, I'm using XSLT 1.0Baelbeer
Does the example I posted (home.arcor.de/martin.honnen/xslt/test2013040601.xml) not show a HTML unordered list in your Chrome version? A blank page is odd, unless you are loading your documents from the file system where I think Chrome is very restrictive as to what resources a file can load, I don't think it applies any XSLT for documents loaded from the file system. Check its error console. The IE/Mozilla behaviour is as expected I would say, if you create a result element in XML mode in no namespace then it can't show as HTML table, it lacks the namespace making it an XHTML element.Mckinney
That may be the reason, since I'm loading the files from the local disk. Thanks!Baelbeer
I
2

I believe most of the browsers don't serialize the output when running a transformation using the xml-stylesheet PI. They simply create a result tree and then render it. If they aren't serializing the result tree, they should quite rightly ignore the xsl:output declaration.

Indeterminacy answered 6/4, 2013 at 13:9 Comment(2)
@cortezthekiller: I would be hesitant about giving a thumbs-up until I understood what the difference in behavior was. A browser supporting non-standard input is not necessarily a good thing.Slam
You are right: I don't fully understand why I should put the default xhtml namespace in the XSLT stylesheet. Actually if I don't declare the xhtml namespace in my stylesheet, the result of the transformation performed by the xsltproc command line processor is accepted by the W3C validator as valid XHTML 1.0 Strict. Does it mean that xsltproc is supporting non-standard input as well?Baelbeer
M
2

Well you haven't really told us in what way the browsers fail. If you want to create XHTML output then 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 the result elements are XHTML elements (and not XML elements in no namespace that happen to have local names like 'html' but are not recognizable as XHTML).

I am pretty sure that Firefox/Mozilla browsers that way with output method xml recognize the XHTML elements. And IE 9 and 10 as well I think, I am not sure older versions of IE with limited XHTML support will work.

To give you an example, the XML input http://home.arcor.de/martin.honnen/xslt/test2013040601.xml is transformed to xml output via http://home.arcor.de/martin.honnen/xslt/test2013040601.xsl and works fine with IE 10 and current version of Firefox and Chrome on Windows 8.

Mckinney answered 6/4, 2013 at 13:15 Comment(3)
The expected result of the transformation was a hmlt table. Instead, Mozilla & IE showed just a string with the concatenation of the text contents of all the cells. Chrome was showing nothing, just a blank page. I've added the default xhtml namespace to the stylesheet, as you suggested. Mozilla & IE are showing the table ok, now. Chrome keeps showing a blank page (?). By the way, I'm using XSLT 1.0Baelbeer
Does the example I posted (home.arcor.de/martin.honnen/xslt/test2013040601.xml) not show a HTML unordered list in your Chrome version? A blank page is odd, unless you are loading your documents from the file system where I think Chrome is very restrictive as to what resources a file can load, I don't think it applies any XSLT for documents loaded from the file system. Check its error console. The IE/Mozilla behaviour is as expected I would say, if you create a result element in XML mode in no namespace then it can't show as HTML table, it lacks the namespace making it an XHTML element.Mckinney
That may be the reason, since I'm loading the files from the local disk. Thanks!Baelbeer
P
0

There are a few underlying issues with client-side XSLT:

  • The XHTML doctype URLs are blocked by the W3C in IE, so a patch is necessary
  • The XML serializer in Firefox is used to output XHTML, so it will fallback to text if the XHTML namespace is not used
  • The media-type attribute needs to be defined as text/html for Chrome

Here is a self-referencing stylesheet which 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 which explain other cross-browser issues:

References

Phlegethon answered 21/8, 2013 at 0:1 Comment(0)
E
-1

Most browser only support XSLT 1.0, You should've a look at SaxonCE to add support for XSLT 2.0

Saxon-CE (client edition) is Saxonica's implementation of XSLT 2.0 for use on web browsers.

Features

Beware of the XPath 2.0 support.

Eschew answered 6/4, 2013 at 12:50 Comment(2)
OK, but what does this have to do with the question? <xsl:output> is supported in XSLT 1.0, so why is 2.0 important?Slam
Could be problematic if you'd used XPath 2.0, but you You didn't describe your XSLT.Thynne

© 2022 - 2024 — McMap. All rights reserved.