I did a transformation from few XML formats to one standard. My XSL looks like following one:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="list | store">
<list>
<xsl:for-each select="item | product | product-store">
<item>
<name>
<xsl:choose>
<xsl:when test="name"><xsl:value-of select="substring-before(name, ' ')" /></xsl:when>
<xsl:otherwise><xsl:value-of select="name | title" /></xsl:otherwise>
</xsl:choose>
</name>
<desc>
<xsl:choose>
<xsl:when test="name"><xsl:value-of select="substring-after(name, ' ')" /></xsl:when>
<xsl:otherwise><xsl:value-of select="desc" /></xsl:otherwise>
</xsl:choose>
</desc>
<nr><xsl:value-of select="index | number" /></nr>
</item>
</xsl:for-each>
</list>
</xsl:template>
</xsl:stylesheet>
my example XML is
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<?xml-stylesheet type="text/xsl" href="transform.xsl"?>
<list>
<item>
<index>1362242627</index>
<name>test 22</name>
</item>
<item>
<index>2362625609</index>
<name>test 4</name>
</item>
<item>
<index>736274650</index>
<name>test 76</name>
</item>
</list>
Why doesn't it display correctly in browsers like Firefox 17, IE9 and Google Chrome? They display it like a normal text, however the return type is "text/xml". It works correctly in Opera only.
The preseveration of newline and tab characters works if
xsl:output="text"
in Firefox and Chrome. Ironically, IE ignores the indentation in text mode. Here is a self-referencing stylesheet that demonstrates this:The following comment from a Mozilla bug explains why the XML serialization does not work for the XML namespace:
Use a
style
element and literal spaces in the tags to format the output in IE:References
I think the problem is to tell what is the "correct" display. Browsers like Firefox or IE assume that once you load an XML document with an
xml-stylesheet
processing instruction of typetext/xsl
into a browser window you want to transform the XML to something the browser knows to render, like HTML or these days like (X)HTML plus SVG (or plus MathML). Your stylesheet however takes the XML input and transforms it to some XML result format which is unknown to the browser so all it does is render the contents of text nodes in the result tree. Opera seems to transform the XML input to the XML result but then it seems to recognize that the result format is unknown and that way decides to render the source tree of the result. That might be what you prefer but I am not sure there is a spec demanding that behaviour.