XML:不工作与嵌入式 XSLT



我有以下XML文件,嵌入了XSLT。

<?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 结果。

我根据这个问题添加了"doc"根元素。我确实通过在线验证器运行了文档,没有错误。

一般来说,对将 XSLT 直接嵌入浏览器世界中的 XML 文档的支持很差,但如果你想这样做,那么你必须采取链接答案中描述的步骤,你还没有采取其中的大部分。

您必须确保 XML 具有链接到嵌入式xsl:stylesheet元素的xml-stylesheet处理指令,该元素需要在内部 DTD 子集中将某些id属性定义为ID属性。

这样,我认为您可以让Mozilla浏览器和Chrome支持以下简单示例(它基本上具有XSLT,仅doc/breakfast_menu/food更正XPath选择以考虑文档结构)

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="#style1"?>
<!DOCTYPE doc [
<!ELEMENT xsl:stylesheet (#PCDATA)>
<!ATTLIST xsl:stylesheet
id ID #IMPLIED>
]>
<doc>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" id="style1">
<xsl:template match="/">        
<html>
<head>
<title>Food list</title>
</head>
<body>              
<xsl:for-each select="doc/breakfast_menu/food">
<p>Food: <xsl:value-of select="name"/></p>
</xsl:for-each>             
</body>
</html> 
</xsl:template>
<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>

这是在线的 https://martin-honnen.github.io/xslt/2017/test2017050902.xml,对我来说在Windows 10上的当前版本的Firefox和Chrome上工作得很好。我认为像 Edge 或 IE 这样的浏览器Microsoft从未支持过带有 ID 属性的嵌入式<?xml-stylesheet type="text/xsl" href="#style1"?>处理指令。

至于使用导入的样式表,以下示例

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="#style1"?>
<!DOCTYPE doc [
<!ELEMENT xsl:stylesheet (#PCDATA)>
<!ATTLIST xsl:stylesheet
id ID #IMPLIED>
]>
<doc>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" id="style1">
<xsl:import href="test2017050901.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>
<head>
<title>Food list</title>
</head>
<body>              
<xsl:for-each select="doc/breakfast_menu/food">
<p>Food: <xsl:value-of select="name"/></p>
</xsl:for-each>             
</body>
</html> 
</xsl:template>
</xsl:stylesheet>

对我来说 https://martin-honnen.github.io/xslt/2017/test2017050901.xml 仅适用于像 Firefox 这样的 Mozilla 浏览器,我不确定为什么 Chrome 呈现空白页,我想这是一个错误或缺乏支持。

最新更新