【问题标题】:XML to XML transformation with XSLT in Firefox and IE在 Firefox 和 IE 中使用 XSLT 将 XML 转换为 XML
【发布时间】:2012-12-01 13:16:17
【问题描述】:

我将几种 XML 格式转换为一种标准。 我的 XSL 如下所示:

<?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>

我的示例 XML 是

<?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>

为什么在 Firefox 17、IE9 和 Google Chrome 等浏览器中无法正确显示?它们像普通文本一样显示它,但是返回类型是“text/xml”。 它仅在 Opera 中正常工作。

【问题讨论】:

    标签: xml xslt cross-browser mime-types pretty-print


    【解决方案1】:

    我认为问题在于分辨什么是“正确”的显示。像 Firefox 或 IE 这样的浏览器假定,一旦您将带有 xml-stylesheet 类型为 text/xsl 的处理指令的 XML 文档加载到浏览器窗口中,您就希望将 XML 转换为浏览器知道要呈现的内容,例如 HTML 或现在( X)HTML 加上 SVG(或加上 MathML)。然而,您的样式表接受 XML 输入并将其转换为浏览器未知的某种 XML 结果格式,因此它所做的只是呈现结果树中文本节点的内容。 Opera 似乎将 XML 输入转换为 XML 结果,但随后它似乎认识到结果格式是未知的,因此决定呈现结果的源树。这可能是您更喜欢的,但我不确定是否有规范要求这种行为。

    【讨论】:

    • 这似乎是一个很好的答案。我刚才使用了一些工具通过 XSL 进行转换,它已经正确转换了。
    【解决方案2】:

    如果在 Firefox 和 Chrome 中使用 xsl:output="text",则可以保留换行符和制表符。具有讽刺意味的是,IE 忽略了文本模式下的缩进。下面是一个自引用样式表,可以证明这一点:

    <?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>
    

    来自Mozilla bug 的以下评论解释了为什么 XML 序列化不适用于 XML 命名空间:

    在当前版本的 Gecko 中,XML 序列化器用于序列化 XHTML 内容。

    在标签中使用 style 元素和文字空格来格式化 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>
    

    参考文献

    【讨论】:

      猜你喜欢
      • 2016-08-24
      • 1970-01-01
      • 1970-01-01
      • 2019-12-05
      • 2019-04-11
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多