Using XML Schemas in Internet Explorer
- Using XML Schemas in Internet Explorer
- About This Article
The XML support in Internet Explorer is built into the MSXML package. This package was called the Microsoft XML Parser until version 4.0, when it was named the Microsoft XML Core Services. Version 4.0 is the version that supports full schemas, also called XML Schema Definition Language (XSD) schemas. Before version 4.0, the MSXML package supported a smaller and different version of XML schemas, which Microsoft calls XML-Data Reduced (XDR) schemas. You can see the support for XML schemas by MSXML version in Table 5.1.
Table 5.1XML Schema Support by MSXML Version
If you're running Windows, how do you know what version of MSXML you have? Take a look in the directory where Windows stores your system dynamic link library (DLL) fileseither system or system32 under the main Windows directory. If you see msxml3.dll and no later version, you have MSXML 3.0. If you see msxml4.dll and no later version, you have version 4.0.
To work with full schemas, you'll need MSXML version 4.0 or later. If you don't already have it installed, you can download it from Microsoft for free from the Microsoft XML site, currently at. The actual download site for MSXML 4.0 is currently (or if you don't feel like typing all that in, use the links from).
Here's an example, Listing ch05_01.html, that puts MSXML 4.0 to work. This example uses JavaScript, which you'll see in the next chapter, to load an XML schema in and use it to verify an XML document:
Listing ch05_01.html
<HTML> <HEAD> <TITLE> Using XML Schemas </TITLE> </HEAD> <BODY> <CENTER> <H1>Using XML Schemas</H1> </CENTER> <SCRIPT LANGUAGE="JavaScript"> var schemaCache = new ActiveXObject("MSXML2.XMLSchemaCache.4.0"); schemaCache.add("", "ch05_02.xsd"); var doc = new ActiveXObject("MSXML2.DOMDocument.4.0"); doc.schemas = schemaCache; doc.validateOnParse = true; if (doc.load("ch05_03.xml")) { document.write("ch05_03.xml is valid."); } else { if (doc.parseError.errorCode != 0) { document.write("Error: " + doc.parseError.reason); } } </SCRIPT> </BODY> </HTML>
Here's the XML schema, Listing ch05_02.xsdnote the namespace, xs, used by the schema elements, which corresponds to "":
Listing ch05_02.xsd
<?xml version="1.0"?> <xs:schema xmlns: <xs:element</xs:element> </xs:schema>
And here's the XML document that we want to try to verify, ch05_03.xml:
Listing ch05_03.xml
<?xml version="1.0"?> <ch05:document xmlns: <ch05:data/> </ch05:document>
If you take a close look at the XML schema here, you can probably figure out what's going on: It's declaring an element named document. If you look in the XML document, on the other hand, you can see that element in use, along with a namespace that corresponds to this chapter, <ch05:document>. Note, however, that the XML document also contains an element named <ch05:data/>, which is not in the schema. That's an error, and that's exactly what this example reports, as you see in Figure 5.1. (Note that to run this example, ch05_01.html, ch05_02.xsd, and ch05_03.xml should all be in the same directory.)
Figure 5.1 Using XML schemas in Internet Explorer.
If you look closely at Listing ch05_01.html, you'll see that the code in that example loads both the XML document and the XML schema. In fact, MSXML 4.0 lets you specify the location of the schema in the XML document, without having to specify it in your code. You can indicate where the schema is with the schemaLocation attribute in the XML document's root element; if you use that attribute, you don't have to load the schema separately, as you see in this new version of this example's HTML page:
Listing ch05_04.html
<HTML> <HEAD> <TITLE> Using XML Schemas </TITLE> </HEAD> <BODY> <CENTER> <H1>Using XML Schemas</H1> </CENTER> <SCRIPT LANGUAGE="JavaScript"> var doc = new ActiveXObject("MSXML2.DOMDocument.4.0"); doc.validateOnParse = true; if (doc.load("ch05_05.xml")) { document.write("ch05_05.xml is valid."); } else { if (doc.parseError.errorCode != 0) { document.write("Error: " + doc.parseError.reason); } } </SCRIPT> </BODY> </HTML>
Here's the new XML document that this HTML page uses. Note the use of the schemaLocation attribute to indicate both the namespace (ch05) and the location of the schema (ch05_02.xsd):
Listing ch05_05.xml
<?xml version="1.0"?> <ch05:document xmlns: <ch05:data/> </ch05:document>
When you load this new HTML page, ch05_04.html, into Internet Explorer, you'll get the same results as you see in Figure 5.1 (to run this example, ch05_02.xsd, ch05_04.html, and ch05_05.xml should all be in the same directory). As you can see, Internet Explorer does indeed offer support for XML schemas.
Here's another exampleas discussed in Chapter 1, VB .NET uses XML to transfer data. And it uses XML schema to validate that data. In fact, you can take a look at such schema directly.
To do that, you need a data set from a database. To take a look at the XML schema Visual Basic .NET uses for a particular dataset, use the Data | View Dataset Schema menu item. This opens the data set's schema, in a Visual Basic designer window, as you see in Figure 5.2.
Figure 5.2 Using XML schemas in Visual Basic .NET.
As you can see here and in many other applications, software support for XML schemas is now real. The next question is: Just how do you write a schema in the first place?