Microsoft KB Archive/312182

= HOW TO: Use the MSXML 4.0 SOM in a Visual C++ Application to Locate Element Declarations in an XSD Schema =

Article ID: 312182

Article Last Modified on 8/9/2004

-

APPLIES TO


 * Microsoft XML Core Services 4.0

-



This article was previously published under Q312182





IN THIS TASK
SUMMARY
 * Create the Sample XSD Schema Document
 * Create the Sample XML Document
 * Download the Visual C++ sample
 * Run the Visual C++ Application

REFERENCES



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 top

Create 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:element name=&quot;price&quot; type=&quot;xs:float&quot;/> <xs:element name=&quot;publish_date&quot; type=&quot;xs:date&quot;/> </xs:sequence>

<xs:attribute name=&quot;id&quot; type=&quot;xs:string&quot;/>

</xs:complexType>

</xs:schema> </li> <li>Save Books.xsd in the root folder of drive C.</li></ol>

Back to the top

Create the Sample XML Document
<ol> <li> Use Notepad to create an XML document named Books.xml that contains the following code: <?xml version=&quot;1.0&quot;?> <x:catalog xmlns:x=&quot;urn:books&quot;> XML Step by Step 50.00      <publish_date>2000-10-01</publish_date> </x:catalog> </li> <li>Save Books.xml in the root folder of drive C.</li></ol>

Back to the top

Download the Visual C++ Sample
The following file is available for download from the Microsoft Download Center:

Vcsomlocate.exe

The Vcsomlocate.exe file contains the following files:

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: 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:
 * 1) import <msxml4.dll>
 * 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:
 * 1) Download the Vcsomlocate.exe file.
 * 2) Double-click Vcsomlocate.dsw and open the project. You need Visual C++ version 6.0 to compile and run the project.
 * 3) Compile the project.

Back to the top

Run the Visual C++ Application
<ol> <li>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.</li> <li> 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=&quot;book&quot; minOccurs=&quot; 0&quot; maxOccurs=&quot;unbounded&quot; abstract=&quot;false&quot; type=&quot;x:bookdata&quot;/> </li> <li> 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=&quot;price&quot; abstract=&quot;false&quot; type=&quot;xsd:float&quot;/> </li></ol>

Back to the top