An Overview of MSXML 4.0
by Steven Livingstone
|
Pages: 1, 2
The ValidateXML() function of the HTML code used to dynamically
validate our XML instance against the sample Schema documents is shown below
(sample5.htm).
function ValidateXML(i)
{
//Load XML Schema Documents
var xmlXSDDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
xmlXSDDoc.async = false;
xmlXSDDoc.load("SampleSchema.xsd");
var xmlXSDDoc2 = new ActiveXObject("Msxml2.DOMDocument.4.0");
xmlXSDDoc2.async = false;
xmlXSDDoc2.load("SampleSchema2.xsd");
var cache = new ActiveXObject("Msxml2.XMLSchemaCache.4.0");
cache.add("http://deltabis.com/products",xmlXSDDoc);
cache.add("http://deltabis.com/itinerary", xmlXSDDoc2);
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
xmlDoc.async = false;
xmlDoc.schemas = cache;
if (i==0)
{
var strXML = document.all("XMLfile").value;
bol = xmlDoc.load(strXML);
}
else
{
var strXML = document.all("XML").value
bol = xmlDoc.loadXML(strXML);
}
if (xmlDoc.parseError.errorCode != 0)
{
alert(xmlDoc.parseError.reason + "\n" +
xmlDoc.parseError.srcText);
}
else {
document.all("XML").value=xmlDoc.xml
alert("File is valid.");
}
}
So, if we modify the value of one of the sold elements from a numeric to a non-numeric value, we will get a validation error message as shown in Figure 2 below:

The ability to validate XML instances against XSDL is the most significant improvement to the MSXML parser.
MSXML 4.0 also contains the Schema Object Model (SOM), which could become very popular. When used with the DOM, SOM gives you access to an XSDL document, allowing you to programmatically interrogate it. At its most basic level, this is a reflection technique, with which you can dynamically generate front end HTML screens with validation or create sub-schemas.
XPath & XSLT
Beyond implementing the standard XPath 1.0 W3C recommendation, MSXML 4.0 also adds extension functions to support XSDL as well as some other miscellaneous functions.
The XSD-related extension functions include the following.
ms:type-is(URI, local-name)This allows you to compare the data type of the current node against the XSD data type.
For example, the following will return true for a node that is an XSD decimal type.
ms:type-is("www.w3.org/2001/XMLSchema","decimal")ms:type-local-name([node-set])This returns the nonqualified name of the XSD type of the current node of the first node of a node-set argument.
ms:type-namespace-uri([node-set])The returns the namespace URI associated with the current node or first node of the node-set argument.
ms:schema-info-available()This function will return true if the XSD Schema is available for the current node.
The assorted miscellaneous functions include the following.
ms:string-compare(string1,string2,language,options)This functions compares
string1andstring2lexicographically (dictionary order) based on the language parameter and case sensitivity defined in the options parameter. It returns-1ifstring1<string2,0ifstring1=string2and1ifstring1>string2.For example:
ms:string-compare("a", "A", "en-US") returns -1.ms:utc(string)Converts data and time values into coordinated universal time (UTC).
ms:namespace-uri(string)Takes a qualified string and returns the URI of the prefix.
ms:local-name(string)This function returns the non-qualified name of the XSD type - that is the name without prefix or namespace qualification.
ms:number(string)Takes an XSD number and converts it to an XPath number.
For example:
ms:number("5.9e5")returns5.9e5ms:format-date(datatime, format, locale)Takes and XSD date and converts it to the date format specified by the format parameter in the specified locale. The format is based on the Win32 API
GetDateFormat()method.ms:format-time(datatime, format, locale)Takes and XSD time and converts it to the time format specified by the format parameter in the specified locale. The format is based on the Win32 API
GetTimeFormat()method.
If you've worked with MSXML, then you'll be well aware of the XSL
implementation (based on the December 1998 W3C Working Draft) available with
previous parser releases, identified by the namespace
http://www.w3.org/TR/WD-xsl. As of MSXML 4.0, this XSL namespace
has been completely dropped, which is of critical importance if you plan to use
MSXML and have not upgraded your XSL documents to work with the W3C XSLT 1.0
specification.
We can use the extended XPath functions to get information about the XSDL
types defined on our XML instance document. This uses the
ms:type-is() function within an XSLT document (sample.xsl) and just
outputs the number of elements that have been declared an XSDL
"int" type within the XML instance document.
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ms="urn:schemas-microsoft-com:xslt">
<xsl:template match="/">
There are <xsl:value-of
select="count(//*[ms:type-is('http://www.w3.org/2001/XMLSchema','int')])" />
integer element types in the instance.
</xsl:template>
</xsl:transform>
In our case, the result is "There are 6 integer element types in the
instance.". We can create powerful dynamic front ends and web services
with fully featured validation simply by interrogating the SOM for a given XML
Schema document.
That's the end of the overview. If you want to learn more, I suggest you download the MSXML 4.0 parser and documentation. You might also be interested in my book, XML Application Development with MSXML 4.0.