【发布时间】:2017-05-08 14:35:26
【问题描述】:
我有以下带有嵌入式 XSLT 的 XML 文件。
<?xml version="1.0" encoding="UTF-8"?>
<doc>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" id = "style1">
<xsl:import href = "S1000D_compare.xsl"/>
<xsl:output method="html"/>
</xsl:stylesheet>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>Light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>Belgian waffles covered with assorted fresh berries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>French Toast</name>
<price>$4.50</price>
<description>Thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>
</breakfast_menu>
</doc>
导入的样式表有以下内容:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select = "breakfast_menu/food">
<p>Food: <xsl:value-of select = "name"/></p>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
在 Chrome 和 IE 中,它以文档树的形式打开,而不是应用转换并显示 HTML 结果。
我添加了基于this question 的“doc”根元素。我确实通过an online validator 运行了文档,没有出现任何错误。
【问题讨论】:
-
xsl:import和xsl:output元素在您放置它们的位置没有意义——它们属于xsl:stylesheet元素的子元素。我不确定修复它是否真的能解决你的问题,但这将是朝着正确方向迈出的一步。 -
还要注意,
xsl:import和xsl:output元素现在的位置没有“xsl”命名空间前缀的范围内声明,所以即使它们在那个位置,他们不会像你期望的那样表达他们的意思。 -
谢谢!那些是复制粘贴错误。 (我开始写我的问题,根据类似问题尝试了一些事情,然后将更新粘贴到错误的位置。)它们在我的 XML 文档中的正确位置,现在也在问题中。
-
当您说您希望浏览器“显示[] XML”时,您的意思是您希望它应用指定的转换并将 html 结果呈现为 html?
-
是的。我会说得更清楚。