XML to XML transformation with XSLT in Firefox and IE

给你一囗甜甜゛ 提交于 2019-12-07 14:03:01

问题


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.


回答1:


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.




回答2:


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

  • White Space in XML Documents


来源:https://stackoverflow.com/questions/13659802/xml-to-xml-transformation-with-xslt-in-firefox-and-ie

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