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

时间:2006-11-30 07:55:22  来源:  作者:

1.新建一个vs2003的web工程,取名为XMLTest2GA第一天空网络
     2.将工程目录下的WebForm1.aspx中内容全部删除,只留下顶部的一条语句:2GA第一天空网络

     <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="XMLTest.WebForm1" %>2GA第一天空网络

2GA第一天空网络
3.修改WebForm1.aspx.cs中内容,在Page_Load中加入:2GA第一天空网络

以下是引用片段:2GA第一天空网络
    XmlDocument doc=new XmlDocument(); 2GA第一天空网络
    String xmlfile=string.Empty; 2GA第一天空网络
    xmlfile=Context.Request.PhysicalApplicationPath+(Request.QueryString["sel"].ToString()=="xml"?"\hello.xml":"\hello.xsl"); 2GA第一天空网络
    doc.Load(xmlfile); 2GA第一天空网络
    Response.Write(doc.InnerXml); 2GA第一天空网络

2GA第一天空网络
     4.在工程根目录下新增test.htm,并设为工程首页:2GA第一天空网络

以下是引用片段:2GA第一天空网络
<html> 2GA第一天空网络
<head> 2GA第一天空网络
  <title></title> 2GA第一天空网络
</head> 2GA第一天空网络
<body> 2GA第一天空网络
  <div id="resTree"></div> 2GA第一天空网络
  <FONT face="宋体"></FONT><input type="button" value="执行" onclick="GetXml()"><BR> 2GA第一天空网络
  <script language="JScript"> 2GA第一天空网络
  var srcTree,xsltTree,xt; 2GA第一天空网络
  var http_request = false; 2GA第一天空网络
    2GA第一天空网络
  function GetXml() 2GA第一天空网络
  {     2GA第一天空网络
   srcTree = new ActiveXObject("Msxml2.FreeThreadedDOMDocument"); 2GA第一天空网络
    srcTree.async=false; 2GA第一天空网络
    xsltTree= new ActiveXObject("Msxml2.FreeThreadedDOMDocument"); 2GA第一天空网络
    xsltTree.async = false; 2GA第一天空网络
    xt=new ActiveXObject("MSXML2.XSLTemplate"); 2GA第一天空网络
   resTree.innerHTML=""; 2GA第一天空网络
    makeRequest("WebForm1.aspx?sel=xml",GetXml_CB); 2GA第一天空网络
  } 2GA第一天空网络
     2GA第一天空网络
    function makeRequest(url,callback) { 2GA第一天空网络
        http_request = false; 2GA第一天空网络
        if (window.XMLHttpRequest) { // Mozilla, Safari,... 2GA第一天空网络
            http_request = new XMLHttpRequest(); 2GA第一天空网络
            if (http_request.overrideMimeType) { 2GA第一天空网络
                http_request.overrideMimeType('text/xml'); 2GA第一天空网络
            } 2GA第一天空网络
        } else if (window.ActiveXObject) { // IE 2GA第一天空网络
            try { 2GA第一天空网络
                http_request = new ActiveXObject("Msxml2.XMLHTTP"); 2GA第一天空网络
            } catch (e) { 2GA第一天空网络
                try { 2GA第一天空网络
                    http_request = new ActiveXObject("Microsoft.XMLHTTP"); 2GA第一天空网络
                } catch (e) {} 2GA第一天空网络
            } 2GA第一天空网络
        } 2GA第一天空网络

        if (!http_request) { 2GA第一天空网络
            alert('Giving up :( Cannot create an XMLHTTP instance'); 2GA第一天空网络
            return false; 2GA第一天空网络
        } 2GA第一天空网络
        http_request.onreadystatechange = callback; 2GA第一天空网络
        http_request.open('GET', url, true); 2GA第一天空网络
        http_request.send(null); 2GA第一天空网络
    } 2GA第一天空网络

    function GetXml_CB() { 2GA第一天空网络

        if (http_request.readyState == 4) { 2GA第一天空网络
            if (http_request.status == 200) {     2GA第一天空网络
    srcTree.loadXML(http_request.responseText); 2GA第一天空网络
    makeRequest("WebForm1.aspx?sel=xsl",GetXsl_CB); 2GA第一天空网络
            } else { 2GA第一天空网络
                alert('There was a problem with the request.'); 2GA第一天空网络
            } 2GA第一天空网络
        } 2GA第一天空网络

    } 2GA第一天空网络
     2GA第一天空网络
    function GetXsl_CB(){ 2GA第一天空网络
       if (http_request.readyState == 4) { 2GA第一天空网络
         if (http_request.status == 200) { 2GA第一天空网络
       xsltTree.loadXML(http_request.responseText); 2GA第一天空网络
       xt.stylesheet=xsltTree; 2GA第一天空网络
       var proc=xt.createProcessor(); 2GA第一天空网络
       proc.input=srcTree; 2GA第一天空网络
       proc.transform(); 2GA第一天空网络
       resTree.innerHTML=proc.output; 2GA第一天空网络
            } else { 2GA第一天空网络
                alert('There was a problem with the request.'); 2GA第一天空网络
            } 2GA第一天空网络
        } 2GA第一天空网络
   2GA第一天空网络
    } 2GA第一天空网络

    function makeRequest(url,callback) { 2GA第一天空网络
     http_request = false; 2GA第一天空网络
     if (window.XMLHttpRequest) { // Mozilla, Safari,... 2GA第一天空网络
            http_request = new XMLHttpRequest(); 2GA第一天空网络
            if (http_request.overrideMimeType) { 2GA第一天空网络
                http_request.overrideMimeType('text/xml'); 2GA第一天空网络
            } 2GA第一天空网络
        } else if (window.ActiveXObject) { // IE 2GA第一天空网络
            try { 2GA第一天空网络
                http_request = new ActiveXObject("Msxml2.XMLHTTP"); 2GA第一天空网络
            } catch (e) { 2GA第一天空网络
                try { 2GA第一天空网络
                    http_request = new ActiveXObject("Microsoft.XMLHTTP"); 2GA第一天空网络
                } catch (e) {} 2GA第一天空网络
            } 2GA第一天空网络
        } 2GA第一天空网络

        if (!http_request) { 2GA第一天空网络
            alert('Giving up :( Cannot create an XMLHTTP instance'); 2GA第一天空网络
            return false; 2GA第一天空网络
        } 2GA第一天空网络
        http_request.onreadystatechange = callback; 2GA第一天空网络
        http_request.open('GET', url, true); 2GA第一天空网络
        http_request.send(null); 2GA第一天空网络
    } 2GA第一天空网络

  </script> 2GA第一天空网络

</body> 2GA第一天空网络
</html> 2GA第一天空网络

2GA第一天空网络
     5.运行工程,看看效果吧! 2GA第一天空网络

     hello.xml(注意:我的xml文档中并没有指定对应的xsl解析文件名)2GA第一天空网络

以下是引用片段:2GA第一天空网络
<?xml version='1.0'?> 2GA第一天空网络

<breakfast-menu> 2GA第一天空网络
  <food> 2GA第一天空网络
    <name>Belgian Waffles</name> 2GA第一天空网络
    <price>$5.95</price> 2GA第一天空网络
    <description>Two of our famous Belgian Waffles  2GA第一天空网络
      with plenty of real maple syrup.</description> 2GA第一天空网络
    <calories>650</calories> 2GA第一天空网络
  </food> 2GA第一天空网络
  <food> 2GA第一天空网络
    <name>Strawberry Belgian Waffles</name> 2GA第一天空网络
    <price>$7.95</price> 2GA第一天空网络
    <description>Light Belgian waffles covered with  2GA第一天空网络
     strawberries and whipped cream.</description> 2GA第一天空网络
    <calories>900</calories> 2GA第一天空网络
  </food> 2GA第一天空网络
  <food> 2GA第一天空网络
    <name>Berry-Berry Belgian Waffles</name> 2GA第一天空网络
    <price>$8.95</price> 2GA第一天空网络
    <description>Light Belgian waffles covered  2GA第一天空网络
      with an assortment of fresh berries  2GA第一天空网络
      and whipped cream.</description> 2GA第一天空网络
    <calories>900</calories> 2GA第一天空网络
  </food> 2GA第一天空网络
  <food> 2GA第一天空网络
    <name>French Toast</name> 2GA第一天空网络
    <price>$4.50</price> 2GA第一天空网络
    <description>Thick slices made from our homemade  2GA第一天空网络
     sourdough bread.</description> 2GA第一天空网络
    <calories>600</calories> 2GA第一天空网络
  </food> 2GA第一天空网络
  <food> 2GA第一天空网络
    <name>Homestyle Breakfast</name> 2GA第一天空网络
    <price>$6.95</price> 2GA第一天空网络
    <description>Two eggs, bacon or sausage, toast,  2GA第一天空网络
      and our ever-popular hash browns.</description> 2GA第一天空网络
    <calories>950</calories> 2GA第一天空网络
  </food> 2GA第一天空网络
</breakfast-menu> 2GA第一天空网络

     hello.xsl2GA第一天空网络

以下是引用片段:2GA第一天空网络
<?xml version="1.0"?> 2GA第一天空网络
<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 2GA第一天空网络
  <xsl:template match="/breakfast-menu"> 2GA第一天空网络

        <xsl:for-each select="food"> 2GA第一天空网络
          <DIV STYLE="background-color:teal; color:white; padding:4px"> 2GA第一天空网络
            <SPAN STYLE="font-weight:bold; color:white"><xsl:value-of select="name"/></SPAN> 2GA第一天空网络
            至 <xsl:value-of select="price"/> 2GA第一天空网络
          </DIV> 2GA第一天空网络
          <DIV STYLE="margin-left:20px; margin-bottom:1em; font-size:10pt"> 2GA第一天空网络
            <xsl:value-of select="description"/> 2GA第一天空网络
            <SPAN STYLE="font-style:italic"> 2GA第一天空网络
              <xsl:value-of select="calories"/> 嘿嘿 2GA第一天空网络
            </SPAN> 2GA第一天空网络
          </DIV> 2GA第一天空网络
        </xsl:for-each> 2GA第一天空网络

  </xsl:template> 2GA第一天空网络
</xsl:stylesheet> 2GA第一天空网络

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

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

文章评论

共有 位天空网友发表了评论 查看完整内容

特别推荐
  • 文字广告
  • 文字广告
  • 文字广告
  • 文字广告
站长黑板报

24小时热门信息