问题
I have an XML file (dynamically created) and an XSL style sheet; if I open the XML file within Firefox, I get a nice table of output.
However, I want to render this data within an HTML page. I've tried using:
HTML file:
<html>
<body>
<xml src="test_data.xml">
</xml>
</body>
</html>
But I can't get anything to render. Both my XML file and my XSL file are in the same directory.
I've tried removing <html>
and <body>
from my XSL output, but still get no results.
Online I've read that some methods differ for IE and Firefox; how can I render an XML file within an HTML page in Firefox?
XML file (test_data.xml):
<?xml-stylesheet type="text/xsl" href="report_proteins.xsl"?>
<group_list>
<protein_group>
<protein name="A_1" />
<protein name="A_2" />
</protein_group>
<protein_group>
<protein name="B_1" />
</protein_group>
<protein_group>
<protein name="C_1" />
<protein name="C_2" />
<protein name="C_3" />
</protein_group>
</group_list>
XSL file (report_proteins.xsl):
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>MAP proteins</h2>
<table border="0">
<tr>
<th bgcolor="#E7FFCC">Group number</th>
<th bgcolor="#D2FBFF">Proteins</th>
</tr>
<xsl:for-each select="group_list/protein_group">
<tr>
<td>
<xsl:number />
</td>
<td>
<xsl:for-each select="protein">
<xsl:value-of select="@name"/><xsl:text> </xsl:text>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
回答1:
There is no such xml
node in HTML, so that is the reason why it doesn't work. You don't need the HTML file at all. Your XSLT sheet generates a full XHTML document, and the browser should be able to render that as it is if you open the XML find in the browser.
You should add the XHTML namespace to the XSLT stylesheet.
回答2:
Use iframe element.
<iframe src="test_data.xml"></iframe>
回答3:
You could also wrap it inside an iframe tag instead of an xml tag:
<html>
<body>
<iframe src="test_data.xml"></iframe>
</xml>
</body>
</html>
回答4:
Generally speaking, no, this is not supported by web browsers.
You should probably do your transform on the server, and push html to the browser.
I don't recommend it, but if you must do this client-side, have a look at http://goog-ajaxslt.sourceforge.net/ which supposedly implements xslt in javascript (I've never been foolish enough to try it, so I don't know if it actually works).
来源:https://stackoverflow.com/questions/7816500/embed-xml-in-html-firefox-compatible