如何把 XML 数据显示为 HTML(加载XML/XSL几种方式)
加载XSL和XML通常有几种方式:
1、 客户端加载(javascript)
<html>
<body>
<script type="text/javascript">
var xmlDoc=null;
if (window.ActiveXObject)
{
// code for IE
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
else if (document.implementation.createDocument)
{
// code for Mozilla, Firefox, Opera, etc.
xmlDoc=document.implementation.createDocument("","",null);
}
else
{
alert('Your browser cannot handle this script');
}
if (xmlDoc!=null)
{
xmlDoc.async=false;
xmlDoc.load("cd_catalog.xml");
document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("CD");
for (i=0;i<x.length;i++)
{
document.write("<tr>");
document.write("<td>");
document.write(
x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
document.write("</td>");
document.write("<td>");
document.write(
x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
document.write("</td>");
document.write("</tr>");
}
document.write("</table>");
}
</script>
</body>
</html>
2、 服务器端加载(Asp.net)
<%@ Control Language="c#" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<%@ Import Namespace="System.Xml.XPath" %>
<script runat="server" language="c#">
public string xmlSource, xslSource;
void Page_Load(){
XmlDocument docXml = new XmlDocument();
docXml.Load(Server.MapPath(xmlSource));
XslTransform docXsl = new XslTransform();
docXsl.Load(Server.MapPath(xslSource));
docXsl.Transform(docXml,null,Response.Output);
// chapter.Text = docXml.TransformNode(docXsl);
}
</script>