异步调用:灵活调用xsl来解析xml文档(js异步)



1.新建个vs2003web工程,取名为XMLTest 2.将工程目录下WebForm1.aspx中内容全部删除只留下顶部条语句:

<%@ Page language=\"c#\" Codebehind=\"WebForm1.aspx.cs\" AutoEventWireup=\"false\" Inherits=\"XMLTest.WebForm1\" %>


3.修改WebForm1.aspx.cs中内容在Page_Load中加入:

以下是引用片段:
XmlDocument doc= XmlDocument;
String xmlfile=.Empty;
xmlfile=Context.Request.PhysicalApplicationPath+(Request.QueryString[\"sel\"].\"xml\"?\"\\\\hello.xml\":\"\\\\hello.xsl\");
doc.Load(xmlfile);
Response.Write(doc.InnerXml);


4.在工程根目录下新增test.htm,并设为工程首页:

以下是引用片段:
<html>
<head>
<title></title>
</head>
<body>
<div id=\"resTree\"></div>
<FONT face=\"宋体\"></FONT><input type=\"button\" value=\"执行\" _disibledevent=> <script language=\"JScript\">
var srcTree,xsltTree,xt;
var http_request = false;

function GetXml
{
srcTree = ActiveXObject(\"Msxml2.FreeThreadedDOMDocument\");
srcTree.async=false;
xsltTree= ActiveXObject(\"Msxml2.FreeThreadedDOMDocument\");
xsltTree.async = false;
xt= ActiveXObject(\"MSXML2.XSLTemplate\");
resTree.innerHTML=\"\";
makeRequest(\"WebForm1.aspx?sel=xml\",GetXml_CB);
}

function makeRequest(url,callback) {
http_request = false;
(window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = XMLHttpRequest;
(http_request.overrideMimeType) {
http_request.overrideMimeType(\'text/xml\');
}
} (window.ActiveXObject) { // IE
try {
http_request = ActiveXObject(\"Msxml2.XMLHTTP\");
} catch (e) {
try {
http_request = ActiveXObject(\"Microsoft.XMLHTTP\");
} catch (e) {}
}
}

(!http_request) {
alert(\'Giving up :( Cannot create an XMLHTTP instance\');
false;
}
http_request.onreadystatechange = callback;
http_request.open(\'GET\', url, true);
http_request.send(null);
}

function GetXml_CB {

(http_request.readyState 4) {
(http_request.status 200) {
srcTree.loadXML(http_request.responseText);
makeRequest(\"WebForm1.aspx?sel=xsl\",GetXsl_CB);
} {
alert(\'There was a problem with the request.\');
}
}

}

function GetXsl_CB{
(http_request.readyState 4) {
(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;
} {
alert(\'There was a problem with the request.\');
}
}

}

function makeRequest(url,callback) {
http_request = false;
(window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = XMLHttpRequest;
(http_request.overrideMimeType) {
http_request.overrideMimeType(\'text/xml\');
}
} (window.ActiveXObject) { // IE
try {
http_request = ActiveXObject(\"Msxml2.XMLHTTP\");
} catch (e) {
try {
http_request = ActiveXObject(\"Microsoft.XMLHTTP\");
} catch (e) {}
}
}

(!http_request) {
alert(\'Giving up :( Cannot create an XMLHTTP instance\');
false;
}
http_request.onreadystatechange = callback;
http_request.open(\'GET\', url, true);
http_request.send(null);
}

</script>

</body>
</html>


5.运行工程看看效果吧!

hello.xml(注意:我xml文档中并没有指定对应xsl解析文件名)

以下是引用片段:
<?xml version=\'1.0\'?>

<fast-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>
</fast-menu>

hello.xsl

以下是引用片段:
<?xml version=\"1.0\"?>
<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">
<xsl:template match=\"/fast-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>
至 <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\"/> 嘿嘿
</SPAN>
</DIV>
</xsl:for-each>

</xsl:template>
</xsl:stylesheet>


xml文档只有纯粹数据如果需要显示到html页面中般需要使用定制xsl文档来解析或者手工通过js来读取xml中值显示到html中dom树中当使用xsl文档来解析时相应xml文档中必须指定对应xsl文档才能正常显示但当有些动态输出xml文档时并没有指定相应xsl文档这时就必须通过其它途径来加载相应xsl文档来解析当然在服务器端输出xml文档时通过些xml api也可以实现我这儿描述是通过js来实现种方式用这种方式就抛开了服务器平台限制服务器端只需要输出相应xml文档(.net/j2ee都可以)并且将对应xsl文档输出给客户端(可以输出流或直接在客户端加载xsl文档)

这里有几个需要注意地方我们般是使用Msxml2.Document组件来加载xml文档但当动态使用xsl解析xml文档时必须使用Msxml2.FreeThreadedDOMDocument这种自由线程组件同时使用MSXML2.XSLTemplate模板组件来加载xml,xsl数据通过MSXML2.XSLTemplatetransform思路方法就可以动态用xsl来解析xml数据了,另外IE5开始系统默认xml组件是msxml2如果需要使用更新msxml组件需要安装更新msxml组件包并指定新名称例如Msxml2.FreeThreadedDOMDocument.4.0现在最新msxml组件是6.0beta可在M$网站WebSite下载

Tags:  如何调用js js调用 .net异步调用 异步调用

延伸阅读

最新评论

发表评论