您现在的位置>>.Net中文社区>>XML编程

什么是MSXML

浏览量: 作者:阿牛 来源:博客园

MSXML是微软非托管代码栈中最为核心的XML服务集合,不但适合基于COM的开发应用,更是微软AJAX解决方案和客户端XSLT解决方案的核心组件。

安装后会在c:\windows\system32\目录下,如:msxml6.dll

测试代码:

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
  2. <HTML><HEAD><META content="IE=5.0000" http-equiv="X-UA-Compatible"> 
  3. <TITLE>XML Evaluator</TITLE> 
  4. <META content="text/html; charset=utf-8" http-equiv=Content-Type> 
  5. <STYLE type=text/css>.xml_source { 
  6.     FONT-FAMILY: "Lucida Console"; FONT-WEIGHT: bold 
  7. .output { 
  8.     BACKGROUND-COLOR: #ffffff; FONT-FAMILY: "Lucida Console", font-weight: bold; COLOR: #000000; FONT-SIZE: 16px 
  9. </STYLE> 
  10.  
  11. <SCRIPT language=JavaScript> 
  12. var area_obj; 
  13. var timer_enabled = false
  14.  
  15. var xml; 
  16. var xinput; 
  17. var output; 
  18. var htmloutput; 
  19. var old_xml = ""
  20. var old_xinput = ""
  21. var old_type = ""
  22. var old_parser = ""
  23. var old_nsdef = ""
  24. var saved_xpath = ""
  25. var saved_xsl = ""
  26. var saved_xsd = ""
  27. var saved_xdr = ""
  28. var xmldoc; 
  29. var xinputdoc; 
  30. var schemas; 
  31.  
  32. function init() 
  33.   xml = document.getElementById("xml"); 
  34.   xinputdocument.getElementById("xinput"); 
  35.   output = document.getElementById("output"); 
  36.   htmloutput = document.getElementById("htmloutput"); 
  37.   nsdef =  document.getElementById("nsdef") 
  38.   document.getElementById("default_xpath").style.visibility = 'hidden'
  39.   document.getElementById("default_xsl").style.visibility = 'hidden'
  40.   document.getElementById("default_xsd").style.visibility = 'hidden'
  41.   document.getElementById("default_xdr").style.visibility = 'hidden'
  42.   saved_xpath = document.getElementById("default_xpath").value; 
  43.   saved_xsl = document.getElementById("default_xsl").value; 
  44.   saved_xsd = document.getElementById("default_xsd").value; 
  45.   saved_xdr = document.getElementById("default_xdr").value; 
  46.   eval_xml(); 
  47.  
  48. function get_type() 
  49.   for (var i=0; i < document.main_form.type.length; i++) 
  50.   { 
  51.     if (document.main_form.type[i].checked) 
  52.       { 
  53.         return document.main_form.type[i].value; 
  54.       } 
  55.   } 
  56.   return "XPath"; 
  57.  
  58. function get_parser() 
  59.   for (var i=0; i < document.main_form.parser.length; i++) 
  60.   { 
  61.     if (document.main_form.parser[i].checked) 
  62.       { 
  63.         return document.main_form.parser[i].value; 
  64.       } 
  65.   } 
  66.   return "MSXML6"; 
  67.  
  68. function loadComponents() 
  69.   var parser = get_parser(); 
  70.   switch(parser) 
  71.   { 
  72.   case "MSXML3": 
  73.     xmldoc = new ActiveXObject("MSXML2.DOMDocument.3.0"); 
  74.     xinputdoc = new ActiveXObject("MSXML2.DOMDocument.3.0"); 
  75.     xmldoc.schemas = new ActiveXObject("MSXML2.XMLSchemaCache.3.0"); 
  76.     break; 
  77.   case "MSXML4": 
  78.     xmldoc = new ActiveXObject("MSXML2.DOMDocument.4.0"); 
  79.     xinputdoc = new ActiveXObject("MSXML2.DOMDocument.4.0"); 
  80.     xmldoc.schemas = new ActiveXObject("MSXML2.XMLSchemaCache.4.0"); 
  81.     break; 
  82.   default: 
  83.     xmldoc = new ActiveXObject("MSXML2.DOMDocument.6.0"); 
  84.     xinputdoc = new ActiveXObject("MSXML2.DOMDocument.6.0"); 
  85.     xmldoc.schemas = new ActiveXObject("MSXML2.XMLSchemaCache.6.0"); 
  86.   } 
  87.  
  88. function eval_xml() 
  89.   if (old_xml == xml.value && old_xinput == xinput.value && old_type == get_type() && old_parser == get_parser() && old_nsdef == nsdef.value) 
  90.   { 
  91.     return; // do not evaluate if both xml and xsl are not changed at all 
  92.   } 
  93.  
  94.   loadComponents(); 
  95.   switch(old_type) 
  96.   { 
  97.   case "XPath": 
  98.       saved_xpath = xinput.value; 
  99.       break; 
  100.   case "XSLT": 
  101.       saved_xsl = xinput.value; 
  102.       break; 
  103.   case "Schema": 
  104.       saved_xsd = xinput.value; 
  105.       break; 
  106.   case "XDR": 
  107.       saved_xdr = xinput.value; 
  108.       break; 
  109.   } 
  110.  
  111.   old_xml = xml.value; 
  112.   old_xinput = xinput.value; 
  113.   old_parser = get_parser(); 
  114.  
  115.   if(old_type != get_type()) 
  116.   { 
  117.     old_type = get_type(); 
  118.     switch(old_type) 
  119.     { 
  120.     case "XPath": 
  121.       xinput.value = saved_xpath
  122.       break; 
  123.     case "XSLT": 
  124.       xinput.value = saved_xsl
  125.       break; 
  126.     case "Schema": 
  127.       xinput.value = saved_xsd
  128.       break; 
  129.     case "XDR": 
  130.       xinput.value = saved_xdr
  131.       break; 
  132.     } 
  133.   } 
  134.   if(old_type == "Schema" && old_parser == "MSXML3") 
  135.   { 
  136.     output.style.color = "#000000"
  137.     output.value = ""
  138.     htmloutput.style.color = "#FF0000" 
  139.     htmloutput.innerHTML = "(error):MSXML3 doesn't support Schema, use XDR instead."
  140.     return; 
  141.   } else if(old_type == "XDR" && old_parser != "MSXML3") { 
  142.     output.style.color = "#000000"
  143.     output.value = ""
  144.     htmloutput.style.color = "#FF0000" 
  145.     htmloutput.innerHTML = "(error):XDR is only supported in MSXML3."
  146.     return; 
  147.   } 
  148.  
  149.   htmloutput.innerHTML = "Processing " + old_type + " on " + old_parser + " "; 
  150.  
  151.   xmldoc.validateOnParse=false
  152.   xmldoc.setProperty("ProhibitDTD",false); 
  153.   xmldoc.loadXML(xml.value); 
  154.   if (xmldoc.parseError != 0) 
  155.   { 
  156.     xml.style.color = "#FF0000"
  157.     output.value = ""
  158.     htmloutput.style.color = "#FF0000" 
  159.     htmloutput.innerHTML = "(xml error):" + xmldoc.parseError.reason; 
  160.     return; 
  161.   } 
  162.  
  163.   if(nsdef.value != "") 
  164.   { 
  165.     xmldoc.setProperty("SelectionNamespaces", nsdef.value.replace("\n", " ")); 
  166. //  xmldoc.setProperty("SelectionLanguage", "XPath"); //break MSXML3 
  167.   } 
  168.  
  169.  
  170.   xml.style.color = "#000000"
  171.   xinput.style.color = "#000000"
  172.   output.style.color = "#000000"
  173.   htmloutput.style.color = "#000000"
  174.  
  175.   switch(old_type) 
  176.   { 
  177.   case "XPath": 
  178.     evaluate_xpath(xmldoc, xinput, output, htmloutput); 
  179.     break; 
  180.   case "XSLT": 
  181.     evaluate_xsl(xmldoc, xinput, output, htmloutput); 
  182.     break; 
  183.   case "Schema": 
  184.   case "XDR": 
  185.     evaluate_xsd(xml, xinput, output, htmloutput); 
  186.     break; 
  187.   } 
  188.  
  189. function evaluate_xpath(doc, x, o, h) 
  190.   try 
  191.   { 
  192.     o.value = ""
  193.     var elements = doc.selectNodes(x.value); 
  194.     if (elements.length>0) 
  195.     { 
  196.       h.style.color = "#000000"
  197.       var s = ""
  198.       for (var i=0; i<elements.length; i++) 
  199.       { 
  200.         o.value += get_node_value(elements.item(i)) + "\n"; 
  201.         ss = s + "<p class='output'>" + get_node_path(elements.item(i)) + "</p>"; 
  202.       } 
  203.       h.innerHTML = s
  204.     } 
  205.     else 
  206.     { 
  207.       o.value = ""
  208.       h.style.color = "#C0C0C0" 
  209.       h.innerHTML = "(no ouput)"
  210.     } 
  211.   } 
  212.   catch (e) 
  213.   { 
  214.     o.value = ""
  215.     h.style.color = "#FF0000"
  216.     h.innerHTML = "(xpath error):" + e.message; 
  217.   } 
  218.  
  219. function evaluate_xsl(doc, x, o, h) 
  220.   xinputdoc.loadXML(x.value); 
  221.   o.value = ""
  222.   if (xinputdoc.parseError != 0) 
  223.   { 
  224.     x.style.color = "#FF0000"
  225.     h.style.color = "#FF0000" 
  226.     h.innerHTML = "(xsl error):" + xinputdoc.parseError.reason; 
  227.     return; 
  228.   } 
  229.  
  230.   try 
  231.   { 
  232.     var outstr = doc.transformNode(xinputdoc); 
  233.     if (outstr.length>0) 
  234.     { 
  235.       o.value = outstr
  236.       h.innerHTML = outstr
  237.     } 
  238.     else 
  239.     { 
  240.       o.value = ""
  241.       h.style.color = "#C0C0C0"
  242.       h.innerHTML = "(no output)"
  243.     } 
  244.   } 
  245.   catch (e) 
  246.   { 
  247.     o.value = ""
  248.     h.style.color = "#FF0000"
  249.     h.innerHTML = "(xslt error):" + e.message;; 
  250.   } 
  251.  
  252. function evaluate_xsd(xml, x, o, h) 
  253.   o.value=""
  254.  
  255.   xinputdoc.setProperty("ProhibitDTD",false); 
  256.   xinputdoc.resolveExternals = true
  257.   xinputdoc.loadXML(x.value); 
  258.   if (xinputdoc.parseError != 0) 
  259.   { 
  260.     x.style.color = "#FF0000"
  261.     h.style.color = "#FF0000"
  262.     h.innerHTML = "(xsd error):" + xinputdoc.parseError.reason; 
  263.     return; 
  264.   } 
  265.  
  266.   try{ 
  267.     var targetNS = xmldoc.childNodes[0].namespaceURI; 
  268.     xmldoc.schemas.add(targetNS, xinputdoc); 
  269.   }catch(se){ 
  270.     h.style.color = "#FF0000"
  271.     h.innerHTML = "(schema error):" + se.description; 
  272.     return; 
  273.   } 
  274.  
  275.   var vError = xmldoc.validate(); 
  276.   if (vError.reason == "") 
  277.   { 
  278.     h.style.color = "#000000"
  279.     h.innerHTML = "Validation success"
  280.   } 
  281.   else 
  282.   { 
  283.     h.style.color = "#FF0000"
  284.     h.innerHTML = "(validation error):" + vError.reason; 
  285.   } 
  286.  
  287. function get_node_path(n) 
  288.   var s = "/"
  289.   var pn = n.parentNode; 
  290.  
  291.   while (pn!=null && pn.nodeName!="#document") 
  292.   { 
  293.     s = "/" + pn.nodeName + s; 
  294.     pnpn = pn.parentNode; 
  295.   } 
  296.   s = "<b>" + n.nodeTypeString + "</b>: " + s + "<b>" + get_node_string(n) + " = " + get_node_value(n) + "</b>"; 
  297.   return s; 
  298.  
  299. function get_node_string(n) 
  300.   var t = n.nodeType; 
  301.   if (t == 2) 
  302.     return "@" + n.nodeName; 
  303.   else 
  304.     return n.nodeName; 
  305.  
  306.  
  307. function get_node_value(n) 
  308.   var t = n.nodeType; 
  309.   var s; 
  310.  
  311.   if (t == 1) 
  312.   { 
  313.     s = n.text; 
  314.   } 
  315.   else 
  316.   { 
  317.     s = n.nodeValue; 
  318.   } 
  319.   if (s==null || s=="") 
  320.     s = "[empty]"
  321.  
  322.   return s; 
  323.  
  324. function enable_area_timer(o) 
  325.   timer_enabled = true
  326.   setTimeout("timer_handler()", 500); 
  327.  
  328. function disable_area_timer(o) 
  329.   timer_enabled = false
  330.  
  331. function timer_handler() 
  332.   if (timer_enabled) 
  333.   { 
  334.     eval_xml(); 
  335.     setTimeout("timer_handler()", 500); 
  336.   } 
  337.  
  338. </SCRIPT> 
  339. </HEAD> 
  340. <BODY onload=init()> 
  341. <FORM name="main_form" action=#> 
  342. <TABLE border=0 cellSpacing=0 cellPadding=2 cols=3 rows="4"> 
  343.   <TBODY> 
  344.   <TR> 
  345.   <TD> 
  346.     <H2>MSXML Evaluator</H2> 
  347.   </TD> 
  348.   <TD valign="top">Functinality 
  349.       <input type="radio" name="type" value="XPath" onclick="javascript:eval_xml()" checked>XPath 1.0</input> 
  350.       <input type="radio" name="type" value="XSLT" onclick="javascript:eval_xml()">XSLT 1.0</input> 
  351.       <input type="radio" name="type" value="Schema" onclick="javascript:eval_xml()">Schema 1.0</input> 
  352.       <input type="radio" name="type" value="XDR" onclick="javascript:eval_xml()">XDR</input> 
  353.       <BR/>Component 
  354.       <input type="radio" name="parser" value="MSXML3" onclick="javascript:eval_xml()">MSXML 3.0</input> 
  355.       <input type="radio" name="parser" value="MSXML4" onclick="javascript:eval_xml()">MSXML 4.0</input> 
  356.       <input type="radio" name="parser" value="MSXML6" checked onclick="javascript:eval_xml()">MSXML 6.0</input> 
  357.   </TD> 
  358.   <TD valign="top"> 
  359.     <B>Namespaces for XPath</B><BR/><TEXTAREA style="COLOR: #000000" id="nsdef" class=xml_source onfocus=enable_area_timer() onblur=disable_area_timer() onchange=eval_xml() rows=3 cols=40 name="ns_def"> 
  360. xmlns:ms='http://www.microsoft.com'</TEXTAREA> 
  361.   </TD> 
  362.   </TR> 
  363.   <TR> 
  364.     <TD vAlign=top width="25%"> 
  365.       <H3>XML Content</H3><TEXTAREA onblur=disable_area_timer() style="COLOR: #000000" id=xml class=xml_source onfocus=enable_area_timer() rows=45 onchange=eval_xml() cols=40 name=xml_data> 
  366. &lt;library xmlns="http://www.microsoft.com"&gt; 
  367.   &lt;book&gt; 
  368.     &lt;chapter&gt;&lt;/chapter&gt; 
  369.     &lt;chapter&gt; 
  370.       &lt;section&gt; 
  371.         &lt;paragraph a="b"&gt;1&lt;/paragraph&gt; 
  372.         &lt;paragraph a="b"&gt;2&lt;/paragraph&gt; 
  373.       &lt;/section&gt; 
  374.     &lt;/chapter&gt; 
  375.   &lt;/book&gt; 
  376. &lt;/library&gt;</TEXTAREA> 
  377.       <BR/> 
  378.       Used for all functions. 
  379.     </TD> 
  380.     <TD vAlign=top width="30%" align=left> 
  381.         <H3>XPath/XSL/Schema</H3> 
  382.         <TEXTAREA onblur=disable_area_timer() style="COLOR: #000000" id=xinput class=xml_source onfocus=enable_area_timer() rows=45 onchange=eval_xml() cols=50 name=x_data></TEXTAREA> 
  383.         <BR/> 
  384.         Change will be remembered in memory when you switch to another function. 
  385.     </TD> 
  386.     <TD vAlign=top width="30%" align=left> 
  387.     <table> 
  388.     <tr><td> 
  389.         <H3>Result</H3> 
  390.         <TEXTAREA onblur=disable_area_timer() style="COLOR: #000000" id=output class=xml_source onfocus=enable_area_timer() rows=25 onchange=eval_xml() cols=40 name=output_data> 
  391.         </TEXTAREA> 
  392.         </td></tr> 
  393.         <tr><td> 
  394.   <h3>HTML Output</h3> 
  395.   <div id="htmloutput"></div> 
  396.         </td></tr> 
  397.     </table> 
  398.     </TD> 
  399.   </TR> 
  400. </TBODY></TABLE></FORM> 
  401. <TEXTAREA id="default_xpath"> 
  402. /ms:library/ms:book/ms:chapter/ms:section/ms:paragraph/@a</TEXTAREA> 
  403.  
  404. <TEXTAREA id="default_xsl"> 
  405. &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:m="http://www.microsoft.com"&gt; 
  406.  
  407. &lt;xsl:template match="/"&gt; 
  408. &lt;html&gt;&lt;body&gt;&lt;table&gt; 
  409.     &lt;tr&gt; 
  410.     &lt;td&gt;Attribute&lt;/td&gt; 
  411.     &lt;td&gt;Value&lt;/td&gt; 
  412.     &lt;/tr&gt; 
  413.     &lt;xsl:for-each select="m:library/m:book/m:chapter/m:section/m:paragraph"&gt; 
  414.     &lt;tr&gt; 
  415.     &lt;td&gt;&lt;xsl:value-of select="@a"/&gt;&lt;/td&gt; 
  416.     &lt;td&gt;&lt;xsl:value-of select="."/&gt;&lt;/td&gt; 
  417.     &lt;/tr&gt; 
  418.     &lt;/xsl:for-each&gt; 
  419. &lt;/table&gt;&lt;/body&gt;&lt;/html&gt; 
  420. &lt;/xsl:template&gt; 
  421.  
  422. &lt;/xsl:stylesheet&gt;</TEXTAREA> 
  423.  
  424. <TEXTAREA id="default_xsd"> 
  425. &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.microsoft.com" elementFormDefault="qualified"&gt; 
  426.  
  427. &lt;xs:element name="library"&gt; 
  428. &lt;xs:complexType&gt; 
  429. &lt;xs:sequence maxOccurs="unbounded"&gt; 
  430. &lt;xs:element name="book"&gt; 
  431.   &lt;xs:complexType&gt; 
  432.   &lt;xs:sequence maxOccurs="unbounded"&gt; 
  433.   &lt;xs:element name="chapter"&gt; 
  434.     &lt;xs:complexType&gt; 
  435.     &lt;xs:sequence minOccurs="0" maxOccurs="unbounded"&gt; 
  436.     &lt;xs:element name="section"&gt; 
  437.       &lt;xs:complexType&gt; 
  438.       &lt;xs:sequence maxOccurs="unbounded"&gt; 
  439.       &lt;xs:element name="paragraph"&gt; 
  440.         &lt;xs:complexType&gt; 
  441.         &lt;xs:simpleContent&gt; 
  442.         &lt;xs:extension base="xs:integer"&gt; 
  443.           &lt;xs:attribute name="a" type="xs:string" /&gt; 
  444.         &lt;/xs:extension&gt; 
  445.         &lt;/xs:simpleContent&gt; 
  446.         &lt;/xs:complexType&gt; 
  447.       &lt;/xs:element&gt; 
  448.       &lt;/xs:sequence&gt; 
  449.       &lt;/xs:complexType&gt; 
  450.     &lt;/xs:element&gt; 
  451.     &lt;/xs:sequence&gt; 
  452.     &lt;/xs:complexType&gt; 
  453.   &lt;/xs:element&gt; 
  454.   &lt;/xs:sequence&gt; 
  455.   &lt;/xs:complexType&gt; 
  456. &lt;/xs:element&gt; 
  457. &lt;/xs:sequence&gt; 
  458. &lt;/xs:complexType&gt; 
  459. &lt;/xs:element&gt; 
  460.  
  461. &lt;/xs:schema&gt;</TEXTAREA> 
  462.  
  463. <TEXTAREA id="default_xdr"> 
  464. &lt;Schema xmlns="urn:schemas-microsoft-com:xml-data"&gt; 
  465.   &lt;!-- AttributeType name="xmlns" --&gt; 
  466.   &lt;AttributeType name="a"/&gt; 
  467.  
  468.   &lt;ElementType name="paragraph"&gt; 
  469.     &lt;attribute type="a"/&gt; 
  470.   &lt;/ElementType&gt; 
  471.  
  472.   &lt;ElementType name="section" model="closed"&gt; 
  473.     &lt;element type="paragraph"/&gt; 
  474.   &lt;/ElementType&gt; 
  475.  
  476.   &lt;ElementType name="chapter" model="closed"&gt; 
  477.     &lt;element type="section"/&gt; 
  478.   &lt;/ElementType&gt; 
  479.  
  480.   &lt;ElementType name="book" model="closed"&gt; 
  481.     &lt;element type="chapter"/&gt; 
  482.   &lt;/ElementType&gt; 
  483.  
  484.   &lt;ElementType name="library" model="closed"&gt; 
  485.     &lt;element type="book"/&gt; 
  486.   &lt;/ElementType&gt; 
  487. &lt;/Schema&gt;</TEXTAREA> 
  488. <TABLE align="center"> 
  489. <TR> 
  490. <TD> 
  491.     <B>Created by: MSXML Team. All rights reserved. © 2009 Microsoft. </B> 
  492. </TD> 
  493. </TR> 
  494. </TABLE> 
  495. </BODY></HTML> 
本站部份资源来于互联网,只供学习之用,不得用于商业,如有侵犯版权请联系告知,本站将第一时间删除!
站长QQ:373638128 邮箱:navy1015@126.com
copyright © 2008 .Net中文社区 ASPXCS.NET™.All Rights Reserved 滇ICP备08102132号