【问题标题】:XML: not working w/ embedded XSLTXML:不适用于/嵌入式 XSLT
【发布时间】: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:importxsl:output 元素在您放置它们的位置没有意义——它们属于xsl:stylesheet 元素的子元素。我不确定修复它是否真的能解决你的问题,但这将是朝着正确方向迈出的一步。
  • 还要注意,xsl:importxsl:output 元素现在的位置没有“xsl”命名空间前缀的范围内声明,所以即使它们在那个位置,他们不会像你期望的那样表达他们的意思。
  • 谢谢!那些是复制粘贴错误。 (我开始写我的问题,根据类似问题尝试了一些事情,然后将更新粘贴到错误的位置。)它们在我的 XML 文档中的正确位置,现在也在问题中。
  • 当您说您希望浏览器“显示[] XML”时,您的意思是您希望它应用指定的转换并将 html 结果呈现为 html?
  • 是的。我会说得更清楚。

标签: xml xslt


【解决方案1】:

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

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

这样我认为你可以让 Mozilla 浏览器和 Chrome 支持像下面这样的简单示例(它基本上有你的 XSLT,只有 XPath 选择 doc/breakfast_menu/food 更正以说明文档结构)

<?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。我认​​为 Microsoft 浏览器(如 Edge 或 IE)不曾支持带有 ID 的嵌入式 &lt;?xml-stylesheet type="text/xsl" href="#style1"?&gt; 处理指令属性。

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

<?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 为何呈现空白页面,我猜这是一个错误或缺乏支持。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    • 2012-11-29
    • 1970-01-01
    • 2022-07-30
    • 1970-01-01
    • 2014-03-07
    • 1970-01-01
    相关资源
    最近更新 更多