Using XML Schemas in Internet Explorer

The XML support in Internet Explorer is built into the Microsoft XML (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-1. XML Schema Support by MSXML Version
Version Support
MSXML No support for schemas
MSXML 2.0 Support for XDR schemas
MSXML 2.6 Support for XDR schemas
MSXML 3.0 Support for XDR schemas
MSXML 4.0 Support for XSD and XDR schemas

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 http://msdn.microsoft.com/library/default.asp?url=/nhp/Default.asp?contentid=28000438. The actual download site for MSXML 4.0 is http://msdn.microsoft.com/downloads/default.asp?url=/downloads/sample.asp?url=/msdnfiles/027/001/766/msdncompositedoc.xml currently (or if you don't feel like typing all that in, use the links from http://msdn.microsoft.com/downloads/xml).

Here's an example, 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("http://starpowder", "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, ch05_02.xsdnote the namespace, xs , used by the schema elements, which corresponds to "http://www.w3.org/2001/XMLSchema" :

Listing ch05_02.xsd
 <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"     targetNamespace = "http://starpowder"     xmlns:ch05 = "http://starpowder"     elementFormDefault = "qualified">    <xs:element name="document"></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 = 'http://starpowder'>     <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.

graphics/05fig01.gif

If you look closely at 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 (for details on how JavaScript works, see the next chapter):

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:xsi='http://www.w3.org/2001/XMLSchema-instance'   xmlns:ch05 = 'http://starpowder'   xsi:schemaLocation='http://starpowder ch05_02.xsd'>  <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, "Essential XML," Visual Basic.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.

graphics/05fig02.gif

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?



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net