2017-05-08 37 views
1

我有以下带有嵌入式XSLT的XML文件。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结果。

我添加了基于this question的“doc”根元素。我没有错误地通过an online validator运行文档。

+2

'xsl:import'和'xsl:output'元素在放置它们的位置没有意义 - 它们属于'xsl:stylesheet'元素的子元素。我不确定修复这个问题是否能够真正解决您的问题,但这将是迈向正确方向的一步。 –

+2

请注意,现在,'xsl:import'和'xsl:output'元素没有'xsl'命名空间前缀的范围内声明,因此即使它们在该位置有意义,他们并不意味着你可能期望他们的意思。 –

+0

谢谢!这些是复制粘贴错误。 (我开始写下我的问题,根据类似的问题尝试了一些东西,然后将更新粘贴到错误的位置。)他们在我的XML文档中的正确位置,现在是问题。 –

回答

1

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

您必须确保XML有关联至需要在内部DTD子集定义为ID属性一些id属性嵌入xsl:stylesheet元素的xml-stylesheet处理指令。

这样,我认为你可以得到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和工作正常适用于我在当前版本的Firefox和谷歌浏览器在Windows 10.我不认为像边缘或IE浏览器的微软曾经支持嵌入式<?xml-stylesheet type="text/xsl" href="#style1"?>处理指令与ID属性。

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

<?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只与Mozilla浏览器如Firefox,我不知道为什么Chrome呈现空白页面,我想这是一个错误或缺乏支持。