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



The information in this article applies to:

  • Microsoft XML 4.0

This article was previously published under Q312182

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:
  1. 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> 
    					
  2. Save Books.xsd in the root folder of drive C.
Back to the top

Create the Sample XML Document

  1. 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>
  2. Save Books.xml in the root folder of drive C.
Back to the top

Download the Visual C++ Sample

The following file is available for download from the Microsoft Download Center:
The Vcsomlocate.exe file contains the following files:

File nameSize
Helper.cpp12KB
Helper.h0.7KB
Resource.h1KB
Stdafx.cpp0.2KB
Stdafx.h1KB
Vcsomlocate.cpp2KB
Vcsomlocate.dsp4KB
Vcsomlocate.h1KB
Vcsomlocate.rc6KB
Vcsomlocatedlg.cpp10KB
Vcsomlocatedlg.h1KB
res\vcsomlocate.ico2KB
res\vcsomlocate.rc22KB


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:
  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

  1. 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.
  2. 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"/>
    					
  3. 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 top

REFERENCES

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

Modification Type:MinorLast Reviewed:8/9/2004
Keywords:kbdownload kbhowto kbHOWTOmaster KB312182 kbAudDeveloper