2017-05-08 34 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呈現空白頁面,我想這是一個錯誤或缺乏支持。