在Firefox和IE中使用XSLT实现XML到XML的转换
我将几种XML格式转换为一种标准。 我的XSL如下所示:在Firefox和IE中使用XSLT实现XML到XML的转换,xml,xslt,cross-browser,mime-types,pretty-print,Xml,Xslt,Cross Browser,Mime Types,Pretty Print,我将几种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"/&
<?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>
1362242627
测试22
2362625609
测试4
736274650
测试76
为什么它不能在Firefox17、IE9和Google Chrome等浏览器中正确显示?它们将其显示为普通文本,但返回类型为“text/xml”。
它只能在Opera中正常工作。我认为问题在于判断什么是“正确”显示。Firefox或IE等浏览器假设,一旦将带有
XML样式表类型的text/xsl
处理指令的XML文档加载到浏览器窗口中,您希望将XML转换为浏览器知道要呈现的内容,如HTML或现在的(X)HTML+SVG(或+MathML)。但是,样式表接受XML输入并将其转换为浏览器未知的XML结果格式,因此它所做的只是在结果树中呈现文本节点的内容。Opera似乎将XML输入转换为XML结果,但它似乎意识到结果格式未知,因此决定呈现结果的源树。这可能是您喜欢的,但我不确定是否有规范要求这种行为 如果在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 </child>
<child>
<grandchild>	 1.1 </grandchild>
</child>
<child>
<grandchild>
<great-grandchild>	 	 1.1.1</great-grandchild>
</grandchild>
</child>
</root>
</xsl:template>
</xsl:stylesheet>
参考资料
这似乎是一个很好的答案。我刚才使用了一些工具来通过XSL进行转换,并且已经正确地进行了转换。
<?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>