1. Create a new vs2003 web project and name it XMLTest
2. Delete all the contents of WebForm1.aspx in the project directory, leaving only one statement at the top:
<%@ Page language="c#" Codebehind="WebForm1. aspx.cs" AutoEventWireup="false" Inherits="XMLTest.WebForm1" %>
3. Modify the content in WebForm1.aspx.cs and add in Page_Load:
The following is a reference fragment:
XmlDocument doc=new XmlDocument();
String xmlfile=string.Empty;
xmlfile=Context.Request.PhysicalApplicationPath+(Request.QueryString["sel"].ToString()=="xml"?" \hello.xml":"\hello.xsl ");
doc.Load(xmlfile);
Response.Write(doc.InnerXml);
4. Add test.htm in the project root directory and set it as the project homepage:
The following is a quote:
<html>
<head>
<title></title>
</head>
<body>
<div id="resTree"></div>
<FONT face="宋体"></FONT><input type="button" value="Execute" onclick="GetXml()"><BR>
<script language="JScript">
var srcTree,xsltTree,xt;
var http_request = false;
function GetXml()
{
srcTree = new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
srcTree.async=false;
xsltTree= new ActiveXObject("Msxml2.FreeThreadedDOMDocument");
xsltTree.async = false;
xt=new ActiveXObject("MSXML2.XSLTemplate");
resTree.innerHTML="";
makeRequest("WebForm1.aspx?sel=xml",GetXml_CB);
}
function makeRequest(url,callback) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
http_request.onreadystatechange = callback;
http_request.open('GET', url, true);
http_request.send(null);
}
function GetXml_CB() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
srcTree.loadXML(http_request.responseText);
makeRequest("WebForm1.aspx?sel=xsl",GetXsl_CB);
} else {
alert('There was a problem with the request.');
}
}
}
function GetXsl_CB(){
if (http_request.readyState == 4) {
if (http_request.status == 200) {
xsltTree.loadXML(http_request.responseText);
xt.stylesheet=xsltTree;
var proc=xt.createProcessor();
proc.input=srcTree;
proc.transform();
resTree.innerHTML=proc.output;
} else {
alert('There was a problem with the request.');
}
}
}
function makeRequest(url,callback) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
http_request.onreadystatechange = callback;
http_request.open('GET', url, true);
http_request.send(null);
}
</script>
</body>
</html>
5. Run the project and see the effect!
hello.xml (Note: My xml document does not specify the corresponding xsl parsing file name)
The following is a quotation fragment:
<?xml version='1.0'?>
<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>Light Belgian waffles covered
with an assortment of 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>
hello.xsl
The following is a quoted fragment:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl=" http://www.w3.org/1999/XSL/Transform ">
<xsl:template match="/breakfast-menu">
<xsl:for-each select="food">
<DIV STYLE="background-color:teal; color:white; padding:4px">
<SPAN STYLE="font-weight:bold; color:white"><xsl:value-of select="name"/></SPAN>
to<xsl:value-of select="price"/>
</DIV>
<DIV STYLE="margin-left:20px; margin-bottom:1em; font-size:10pt">
<xsl:value-of select="description"/>
<SPAN STYLE="font-style:italic">
<xsl:value-of select="calories"/> Hey
</SPAN>
</DIV>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The xml document only contains pure data. If it needs to be displayed in the html page, it generally needs to use a customized xsl document to parse, or manually read the value in the xml through js and display it into the dom tree in the html. When using the xsl document When parsing, the corresponding xsl document must be specified in the corresponding xml document to display it normally. However, when some programs dynamically output the xml document, the corresponding xsl document is not specified. In this case, the corresponding xsl document must be loaded through other means. To parse, of course, when outputting XML documents on the server side, it can also be achieved through some XML APIs. What I describe here is a way to achieve it through JS. In this way, the limitations of the server platform are put aside. The server only needs to output the corresponding xml document (.net/j2ee is acceptable), and output the corresponding xsl document to the client (it can output a stream or directly in Client loads xsl document).
There are a few things to note here. We generally use the Msxml2.Document component to load xml documents, but when dynamically using xsl to parse xml documents, free thread components such as Msxml2.FreeThreadedDOMDocument must be used, and MSXML2.XSLTemplate must be used. Template components are used to load xml and xsl data. Through the transform method of MSXML2. Install the updated msxml component package and specify a new name, such as Msxml2.FreeThreadedDOMDocument.4.0. The latest msxml component is now 6.0beta, which can be downloaded from the M$ website.
Demo: http://www.21cz.cn/xmltest/test.htm
xml file view: http://www.21cz.cn/xmltest/hello.xml
View xsl file: http://www.21cz.cn/xmltest/hello.xsl