gpt4 book ai didi

在 Firefox 和 IE 中使用 XSLT 将 XML 转换为 XML

转载 作者:数据小太阳 更新时间:2023-10-29 02:22:18 26 4
gpt4 key购买 nike

我做了从几种 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 中正常工作。

最佳答案

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

关于在 Firefox 和 IE 中使用 XSLT 将 XML 转换为 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13659802/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com