SUMMARY
The Schema Object Model (SOM) that is implemented in MSXML version 4.0 can be used to programmatically locate schema information that pertains to XML element declarations in an XML Schema Definition (XSD) schema document. This article provides a sample that demonstrates how to program the MSXML 4.0 SOM in a Microsoft Visual C++ application to locate the schema declaration and display the corresponding type information of a specified element.
Back to the topCreate the Sample XSD Schema Document
To create an XSD schema to define the structure of an XML document that is used to store data about a book catalog, follow these steps:
- Use Notepad to create an XSD document named Books.xsd that contains the following code:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:books" xmlns:b="urn:books">
<xs:element name="catalog" type="b:CatalogData"/>
<xs:complexType name="CatalogData">
<xs:sequence>
<xs:element name="book" type="b:bookdata" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="bookdata">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="price" type="xs:float"/>
<xs:element name="publish_date" type="xs:date"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string"/>
</xs:complexType>
</xs:schema>
- Save Books.xsd in the root folder of drive C.
Back to the topCreate the Sample XML Document
- Use Notepad to create an XML document named Books.xml that contains the following code:
<?xml version="1.0"?>
<x:catalog xmlns:x="urn:books">
<book>
<title>XML Step by Step</title>
<price>50.00</price>
<publish_date>2000-10-01</publish_date>
</book>
</x:catalog>
- Save Books.xml in the root folder of drive C.
Back to the topDownload the Visual C++ Sample
The following file is available for download from the Microsoft Download Center:
The Vcsomlocate.exe file contains the following files:
|
Helper.cpp | 12KB |
Helper.h | 0.7KB |
Resource.h | 1KB |
Stdafx.cpp | 0.2KB |
Stdafx.h | 1KB |
Vcsomlocate.cpp | 2KB |
Vcsomlocate.dsp | 4KB |
Vcsomlocate.h | 1KB |
Vcsomlocate.rc | 6KB |
Vcsomlocatedlg.cpp | 10KB |
Vcsomlocatedlg.h | 1KB |
res\vcsomlocate.ico | 2KB |
res\vcsomlocate.rc2 | 2KB |
In this Visual C++ project, you use
#import with Msxml4.dll. When you open the Stdafx.h file, you can see the
#import statement as follows:
#import <msxml4.dll>
using namespace MSXML2;
This project contains the following four main functions under the
CVCSOMLocateDlg class that locate the declaration of the element:
afx_msg void OnLoadXML();
afx_msg void OnLoadXSD();
afx_msg void OnLocate();
void printDecl(IXMLDOMNodePtr pNode);
Each method represents an important step in locating declarations, as follows:
- OnLoadXML loads the XML file.
- OnLoadXSD loads the XSD file, adds the XSD schema to SchemaCache object, and then calls validate on the XML document to retrieve all definitions.
- OnLocate selects the DOM node based on the user input and outputs the declaration with the printDecl function.
- printDecl(IXMLDOMNodePtr pNode) is a helper function. It retrieves the schema collection that is associated with the XML document, then calls getDeclaration to retrieve a declaration object for the DOM node.
To use the Visual C++ sample file, follow these steps:
- Download the Vcsomlocate.exe file.
- Double-click Vcsomlocate.dsw and open the project. You need Visual C++ version 6.0 to compile and run the project.
- Compile the project.
Back to the topRun the Visual C++ Application
- Run the Visual C++ project and click load XML. If the XML file encounters no errors, the load XSD button is enabled. Click load XSD to load the XSD file.
- To extract and display the schema declaration information for the book complexType element, select /x:catalog/book from the XPath for select node drop-down list, and then click Locate Declaration.
The following output is displayed in the XSD declaration text box:
<xsd:element name="book" minOccurs=" 0" maxOccurs="unbounded" abstract="false" type="x:bookdata"/>
- To extract and display the schema declaration information for the price simpleType element, select /x:catalog/book/price from the XPath for select node drop-down list, then click Locate Declaration.
The following output is displayed in the XSD declaration text box:
<xsd:element name="price" abstract="false" type="xsd:float"/>
Back to the topREFERENCES
For comprehensive samples that demonstrate how to program the MSXML SOM to implement common application requirements, see the SOM Developer's Guide in the MSXML 4.0 Software Development Kit (SDK) documentation. The SDK document ships with the MSXML 4.0 parser installation.
For additional information, click the article number below
to view the article in the Microsoft Knowledge Base:
309616 HOW TO: Use the MSXML 4.0 SOM in a Visual Basic Application to Locate Element Declarations in an XSD Schema
Back to the top