XML to XML transformation with XSLT in Firefox and IE

穿精又带淫゛_ 提交于 2019-12-05 21:34:45

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 type text/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.

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:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="newline-indent.xml"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns=""
                >

<!-- Output HTML doctype with text/html content-type and without XML declaration-->
<xsl:output method="text" encoding="utf-8" version="1.0" media-type="text/plain" indent="yes" standalone="no" omit-xml-declaration="no"/>

<!-- Output the HTML markup-->
<xsl:template xml:space="preserve" match="/">
  <root>
    <child>1 &#13;&#10;</child>   
    <child>
      <grandchild>&#09; 1.1  &#13;&#10;</grandchild>
    </child>
    <child>
      <grandchild>
        <great-grandchild>&#09; &#09; 1.1.1</great-grandchild>
      </grandchild>
    </child>
  </root>
</xsl:template>
</xsl:stylesheet>

The following comment from a Mozilla bug explains why the XML serialization does not work for the XML namespace:

In the current version of Gecko, the XML serializer is used to serialize XHTML content.

Use a style element and literal spaces in the tags to format the output in IE:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="newline-indent-ie.xml"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns=""
                >

<xsl:output method="xml" encoding="utf-8" version="1.0" media-type="application/xml" indent="yes" standalone="no" omit-xml-declaration="no"/>

<xsl:template match="/">
  <style>* { white-space:pre-wrap; }</style>
  <root>
    <child  >1</child>   
    <child>
      <grandchild  >1.1</grandchild>
    </child>
    <child>
      <grandchild>
        <great-grandchild  >1.1.1</great-grandchild>
      </grandchild>
    </child>
  </root>
</xsl:template>
</xsl:stylesheet>

References

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!