Reputation: 215
I have the following XML file, with embedded 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>
The imported stylesheet has the following content:
<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>
In both Chrome and IE, it opens as a document tree, rather than applying the transform and displaying the HTML result.
I added the "doc" root element based on this question. I did run the doc through an online validator with no errors.
Upvotes: 2
Views: 363
Reputation: 167571
In general there is poor support for embedding XSLT directly into an XML document in the browser world but if you want to do that then you have to take the steps described in your linked answer, you have not taken most of them.
You will have to make sure the XML has an xml-stylesheet
processing instruction linking to to the embedded xsl:stylesheet
element that needs to have some id
attribute defined in an internal DTD subset as an ID
attribute.
That way I think you can get Mozilla browsers and Chrome to support simple examples like the following (it basically has your XSLT, only with the XPath selection doc/breakfast_menu/food
corrected to account for the document structure)
<?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>
This is online at https://martin-honnen.github.io/xslt/2017/test2017050902.xml and works fine for me with current versions of Firefox and Chrome on Windows 10. I don't think Microsoft browsers like Edge or IE have ever supported the embedded <?xml-stylesheet type="text/xsl" href="#style1"?>
processing instruction with an ID attribute.
As for using an imported stylesheet, the following example
<?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>
with the stylesheet then doing
<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>
works fine for me at https://martin-honnen.github.io/xslt/2017/test2017050901.xml only with Mozilla browsers like Firefox, I am not sure why Chrome renders a blank page, I guess it is a bug or lack of support.
Upvotes: 1