MSXML是微软非托管代码栈中最为核心的XML服务集合,不但适合基于COM的开发应用,更是微软AJAX解决方案和客户端XSLT解决方案的核心组件。
安装后会在c:\windows\system32\目录下,如:msxml6.dll
测试代码:
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
- <HTML><HEAD><META content="IE=5.0000" http-equiv="X-UA-Compatible">
- <TITLE>XML Evaluator</TITLE>
- <META content="text/html; charset=utf-8" http-equiv=Content-Type>
- <STYLE type=text/css>.xml_source {
- FONT-FAMILY: "Lucida Console"; FONT-WEIGHT: bold
- }
- .output {
- BACKGROUND-COLOR: #ffffff; FONT-FAMILY: "Lucida Console", font-weight: bold; COLOR: #000000; FONT-SIZE: 16px
- }
- </STYLE>
- <SCRIPT language=JavaScript>
- var area_obj;
- var timer_enabled = false;
- var xml;
- var xinput;
- var output;
- var htmloutput;
- var old_xml = "";
- var old_xinput = "";
- var old_type = "";
- var old_parser = "";
- var old_nsdef = "";
- var saved_xpath = "";
- var saved_xsl = "";
- var saved_xsd = "";
- var saved_xdr = "";
- var xmldoc;
- var xinputdoc;
- var schemas;
- function init()
- {
- xml = document.getElementById("xml");
- xinput= document.getElementById("xinput");
- output = document.getElementById("output");
- htmloutput = document.getElementById("htmloutput");
- nsdef = document.getElementById("nsdef")
- document.getElementById("default_xpath").style.visibility = 'hidden';
- document.getElementById("default_xsl").style.visibility = 'hidden';
- document.getElementById("default_xsd").style.visibility = 'hidden';
- document.getElementById("default_xdr").style.visibility = 'hidden';
- saved_xpath = document.getElementById("default_xpath").value;
- saved_xsl = document.getElementById("default_xsl").value;
- saved_xsd = document.getElementById("default_xsd").value;
- saved_xdr = document.getElementById("default_xdr").value;
- eval_xml();
- }
- function get_type()
- {
- for (var i=0; i < document.main_form.type.length; i++)
- {
- if (document.main_form.type[i].checked)
- {
- return document.main_form.type[i].value;
- }
- }
- return "XPath";
- }
- function get_parser()
- {
- for (var i=0; i < document.main_form.parser.length; i++)
- {
- if (document.main_form.parser[i].checked)
- {
- return document.main_form.parser[i].value;
- }
- }
- return "MSXML6";
- }
- function loadComponents()
- {
- var parser = get_parser();
- switch(parser)
- {
- case "MSXML3":
- xmldoc = new ActiveXObject("MSXML2.DOMDocument.3.0");
- xinputdoc = new ActiveXObject("MSXML2.DOMDocument.3.0");
- xmldoc.schemas = new ActiveXObject("MSXML2.XMLSchemaCache.3.0");
- break;
- case "MSXML4":
- xmldoc = new ActiveXObject("MSXML2.DOMDocument.4.0");
- xinputdoc = new ActiveXObject("MSXML2.DOMDocument.4.0");
- xmldoc.schemas = new ActiveXObject("MSXML2.XMLSchemaCache.4.0");
- break;
- default:
- xmldoc = new ActiveXObject("MSXML2.DOMDocument.6.0");
- xinputdoc = new ActiveXObject("MSXML2.DOMDocument.6.0");
- xmldoc.schemas = new ActiveXObject("MSXML2.XMLSchemaCache.6.0");
- }
- }
- function eval_xml()
- {
- if (old_xml == xml.value && old_xinput == xinput.value && old_type == get_type() && old_parser == get_parser() && old_nsdef == nsdef.value)
- {
- return; // do not evaluate if both xml and xsl are not changed at all
- }
- loadComponents();
- switch(old_type)
- {
- case "XPath":
- saved_xpath = xinput.value;
- break;
- case "XSLT":
- saved_xsl = xinput.value;
- break;
- case "Schema":
- saved_xsd = xinput.value;
- break;
- case "XDR":
- saved_xdr = xinput.value;
- break;
- }
- old_xml = xml.value;
- old_xinput = xinput.value;
- old_parser = get_parser();
- if(old_type != get_type())
- {
- old_type = get_type();
- switch(old_type)
- {
- case "XPath":
- xinput.value = saved_xpath;
- break;
- case "XSLT":
- xinput.value = saved_xsl;
- break;
- case "Schema":
- xinput.value = saved_xsd;
- break;
- case "XDR":
- xinput.value = saved_xdr;
- break;
- }
- }
- if(old_type == "Schema" && old_parser == "MSXML3")
- {
- output.style.color = "#000000";
- output.value = "";
- htmloutput.style.color = "#FF0000"
- htmloutput.innerHTML = "(error):MSXML3 doesn't support Schema, use XDR instead.";
- return;
- } else if(old_type == "XDR" && old_parser != "MSXML3") {
- output.style.color = "#000000";
- output.value = "";
- htmloutput.style.color = "#FF0000"
- htmloutput.innerHTML = "(error):XDR is only supported in MSXML3.";
- return;
- }
- htmloutput.innerHTML = "Processing " + old_type + " on " + old_parser + " ";
- xmldoc.validateOnParse=false;
- xmldoc.setProperty("ProhibitDTD",false);
- xmldoc.loadXML(xml.value);
- if (xmldoc.parseError != 0)
- {
- xml.style.color = "#FF0000";
- output.value = "";
- htmloutput.style.color = "#FF0000"
- htmloutput.innerHTML = "(xml error):" + xmldoc.parseError.reason;
- return;
- }
- if(nsdef.value != "")
- {
- xmldoc.setProperty("SelectionNamespaces", nsdef.value.replace("\n", " "));
- // xmldoc.setProperty("SelectionLanguage", "XPath"); //break MSXML3
- }
- xml.style.color = "#000000";
- xinput.style.color = "#000000";
- output.style.color = "#000000";
- htmloutput.style.color = "#000000";
- switch(old_type)
- {
- case "XPath":
- evaluate_xpath(xmldoc, xinput, output, htmloutput);
- break;
- case "XSLT":
- evaluate_xsl(xmldoc, xinput, output, htmloutput);
- break;
- case "Schema":
- case "XDR":
- evaluate_xsd(xml, xinput, output, htmloutput);
- break;
- }
- }
- function evaluate_xpath(doc, x, o, h)
- {
- try
- {
- o.value = "";
- var elements = doc.selectNodes(x.value);
- if (elements.length>0)
- {
- h.style.color = "#000000";
- var s = "";
- for (var i=0; i<elements.length; i++)
- {
- o.value += get_node_value(elements.item(i)) + "\n";
- ss = s + "<p class='output'>" + get_node_path(elements.item(i)) + "</p>";
- }
- h.innerHTML = s;
- }
- else
- {
- o.value = "";
- h.style.color = "#C0C0C0"
- h.innerHTML = "(no ouput)";
- }
- }
- catch (e)
- {
- o.value = "";
- h.style.color = "#FF0000";
- h.innerHTML = "(xpath error):" + e.message;
- }
- }
- function evaluate_xsl(doc, x, o, h)
- {
- xinputdoc.loadXML(x.value);
- o.value = "";
- if (xinputdoc.parseError != 0)
- {
- x.style.color = "#FF0000";
- h.style.color = "#FF0000"
- h.innerHTML = "(xsl error):" + xinputdoc.parseError.reason;
- return;
- }
- try
- {
- var outstr = doc.transformNode(xinputdoc);
- if (outstr.length>0)
- {
- o.value = outstr;
- h.innerHTML = outstr;
- }
- else
- {
- o.value = "";
- h.style.color = "#C0C0C0";
- h.innerHTML = "(no output)";
- }
- }
- catch (e)
- {
- o.value = "";
- h.style.color = "#FF0000";
- h.innerHTML = "(xslt error):" + e.message;;
- }
- }
- function evaluate_xsd(xml, x, o, h)
- {
- o.value="";
- xinputdoc.setProperty("ProhibitDTD",false);
- xinputdoc.resolveExternals = true;
- xinputdoc.loadXML(x.value);
- if (xinputdoc.parseError != 0)
- {
- x.style.color = "#FF0000";
- h.style.color = "#FF0000";
- h.innerHTML = "(xsd error):" + xinputdoc.parseError.reason;
- return;
- }
- try{
- var targetNS = xmldoc.childNodes[0].namespaceURI;
- xmldoc.schemas.add(targetNS, xinputdoc);
- }catch(se){
- h.style.color = "#FF0000";
- h.innerHTML = "(schema error):" + se.description;
- return;
- }
- var vError = xmldoc.validate();
- if (vError.reason == "")
- {
- h.style.color = "#000000";
- h.innerHTML = "Validation success";
- }
- else
- {
- h.style.color = "#FF0000";
- h.innerHTML = "(validation error):" + vError.reason;
- }
- }
- function get_node_path(n)
- {
- var s = "/";
- var pn = n.parentNode;
- while (pn!=null && pn.nodeName!="#document")
- {
- s = "/" + pn.nodeName + s;
- pnpn = pn.parentNode;
- }
- s = "<b>" + n.nodeTypeString + "</b>: " + s + "<b>" + get_node_string(n) + " = " + get_node_value(n) + "</b>";
- return s;
- }
- function get_node_string(n)
- {
- var t = n.nodeType;
- if (t == 2)
- return "@" + n.nodeName;
- else
- return n.nodeName;
- }
- function get_node_value(n)
- {
- var t = n.nodeType;
- var s;
- if (t == 1)
- {
- s = n.text;
- }
- else
- {
- s = n.nodeValue;
- }
- if (s==null || s=="")
- s = "[empty]";
- return s;
- }
- function enable_area_timer(o)
- {
- timer_enabled = true;
- setTimeout("timer_handler()", 500);
- }
- function disable_area_timer(o)
- {
- timer_enabled = false;
- }
- function timer_handler()
- {
- if (timer_enabled)
- {
- eval_xml();
- setTimeout("timer_handler()", 500);
- }
- }
- </SCRIPT>
- </HEAD>
- <BODY onload=init()>
- <FORM name="main_form" action=#>
- <TABLE border=0 cellSpacing=0 cellPadding=2 cols=3 rows="4">
- <TBODY>
- <TR>
- <TD>
- <H2>MSXML Evaluator</H2>
- </TD>
- <TD valign="top">Functinality
- <input type="radio" name="type" value="XPath" onclick="javascript:eval_xml()" checked>XPath 1.0</input>
- <input type="radio" name="type" value="XSLT" onclick="javascript:eval_xml()">XSLT 1.0</input>
- <input type="radio" name="type" value="Schema" onclick="javascript:eval_xml()">Schema 1.0</input>
- <input type="radio" name="type" value="XDR" onclick="javascript:eval_xml()">XDR</input>
- <BR/>Component
- <input type="radio" name="parser" value="MSXML3" onclick="javascript:eval_xml()">MSXML 3.0</input>
- <input type="radio" name="parser" value="MSXML4" onclick="javascript:eval_xml()">MSXML 4.0</input>
- <input type="radio" name="parser" value="MSXML6" checked onclick="javascript:eval_xml()">MSXML 6.0</input>
- </TD>
- <TD valign="top">
- <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">
- xmlns:ms='http://www.microsoft.com'</TEXTAREA>
- </TD>
- </TR>
- <TR>
- <TD vAlign=top width="25%">
- <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>
- <library xmlns="http://www.microsoft.com">
- <book>
- <chapter></chapter>
- <chapter>
- <section>
- <paragraph a="b">1</paragraph>
- <paragraph a="b">2</paragraph>
- </section>
- </chapter>
- </book>
- </library></TEXTAREA>
- <BR/>
- Used for all functions.
- </TD>
- <TD vAlign=top width="30%" align=left>
- <H3>XPath/XSL/Schema</H3>
- <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>
- <BR/>
- Change will be remembered in memory when you switch to another function.
- </TD>
- <TD vAlign=top width="30%" align=left>
- <table>
- <tr><td>
- <H3>Result</H3>
- <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>
- </TEXTAREA>
- </td></tr>
- <tr><td>
- <h3>HTML Output</h3>
- <div id="htmloutput"></div>
- </td></tr>
- </table>
- </TD>
- </TR>
- </TBODY></TABLE></FORM>
- <TEXTAREA id="default_xpath">
- /ms:library/ms:book/ms:chapter/ms:section/ms:paragraph/@a</TEXTAREA>
- <TEXTAREA id="default_xsl">
- <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:m="http://www.microsoft.com">
- <xsl:template match="/">
- <html><body><table>
- <tr>
- <td>Attribute</td>
- <td>Value</td>
- </tr>
- <xsl:for-each select="m:library/m:book/m:chapter/m:section/m:paragraph">
- <tr>
- <td><xsl:value-of select="@a"/></td>
- <td><xsl:value-of select="."/></td>
- </tr>
- </xsl:for-each>
- </table></body></html>
- </xsl:template>
- </xsl:stylesheet></TEXTAREA>
- <TEXTAREA id="default_xsd">
- <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.microsoft.com" elementFormDefault="qualified">
- <xs:element name="library">
- <xs:complexType>
- <xs:sequence maxOccurs="unbounded">
- <xs:element name="book">
- <xs:complexType>
- <xs:sequence maxOccurs="unbounded">
- <xs:element name="chapter">
- <xs:complexType>
- <xs:sequence minOccurs="0" maxOccurs="unbounded">
- <xs:element name="section">
- <xs:complexType>
- <xs:sequence maxOccurs="unbounded">
- <xs:element name="paragraph">
- <xs:complexType>
- <xs:simpleContent>
- <xs:extension base="xs:integer">
- <xs:attribute name="a" type="xs:string" />
- </xs:extension>
- </xs:simpleContent>
- </xs:complexType>
- </xs:element>
- </xs:sequence>
- </xs:complexType>
- </xs:element>
- </xs:sequence>
- </xs:complexType>
- </xs:element>
- </xs:sequence>
- </xs:complexType>
- </xs:element>
- </xs:sequence>
- </xs:complexType>
- </xs:element>
- </xs:schema></TEXTAREA>
- <TEXTAREA id="default_xdr">
- <Schema xmlns="urn:schemas-microsoft-com:xml-data">
- <!-- AttributeType name="xmlns" -->
- <AttributeType name="a"/>
- <ElementType name="paragraph">
- <attribute type="a"/>
- </ElementType>
- <ElementType name="section" model="closed">
- <element type="paragraph"/>
- </ElementType>
- <ElementType name="chapter" model="closed">
- <element type="section"/>
- </ElementType>
- <ElementType name="book" model="closed">
- <element type="chapter"/>
- </ElementType>
- <ElementType name="library" model="closed">
- <element type="book"/>
- </ElementType>
- </Schema></TEXTAREA>
- <TABLE align="center">
- <TR>
- <TD>
- <B>Created by: MSXML Team. All rights reserved. © 2009 Microsoft. </B>
- </TD>
- </TR>
- </TABLE>
- </BODY></HTML>