Microsoft KB Archive/312554

= HOW TO: Use the MSXML 4.0 SOM in a Visual Basic Program to Locate Element Declarations in an XSD Schema with SAX Reader =

Article ID: 312554

Article Last Modified on 8/9/2004

-

APPLIES TO


 * Microsoft XML Core Services 4.0

-



This article was previously published under Q312554



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

REFERENCES



SUMMARY
This step-by-step article describes how to program the Microsoft XML Schema Object Model (SOM) in a Visual Basic application to locate the schema declaration and to display the corresponding type information of a specified element.

You can use the SOM that is implemented in Microsoft XML version 4.0 to programmatically locate schema information that pertains to XML element declarations in an XML Schema Definition (XSD) schema document.

The sample in this article uses the Simple API for XML (SAX) reader.

back to the top

Create the Sample XSD Schema Document
To create an XSD schema that defines the structure of the PurchaseOrders XML document, follow these steps:   In Notepad, create an XSD document named Po.xsd that contains the following code: 

  Purchase order schema for Example.com. Copyright 2000 Example.com. All rights reserved.    





  <xsd:element name='name'  type='xsd:string'/> <xsd:element name='street' type='xsd:string'/> <xsd:element name='city'  type='xsd:string'/> <xsd:element name='state' type='xsd:string'/> <xsd:element name='zip'   type='xsd:decimal'/> </xsd:sequence> <xsd:attribute name='country' type='xsd:NMTOKEN' fixed='US'/> </xsd:complexType>

<xsd:simpleType name='SKU'> <xsd:restriction base='xsd:string'> <xsd:pattern value='\d{3}-[A-Z]{2}'/> </xsd:restriction> </xsd:simpleType> <xsd:complexType name='Items'> <xsd:sequence> <xsd:element name='item' minOccurs='0' maxOccurs='unbounded'> <xsd:complexType> <xsd:sequence> <xsd:element name='productName' type='xsd:string'/> <xsd:element name='quantity'> <xsd:simpleType> <xsd:restriction base='xsd:positiveInteger'> <xsd:minInclusive value='1'/> <xsd:maxExclusive value='100'/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name='USPrice' type='xsd:decimal'/> <xsd:element ref='po:comment' minOccurs='1'/> <xsd:element name='shipDate' type='xsd:date' minOccurs='0'/> </xsd:sequence> <xsd:attribute name='partNum' type='po:SKU'/> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType>

<xsd:complexType name='PurchaseOrderType'> <xsd:sequence> <xsd:element name='shipTo' type='po:USAddress'/> <xsd:element name='billTo' type='po:USAddress'/> <xsd:element ref='po:comment' minOccurs='0'/> <xsd:element name='items' type='po:Items'/> </xsd:sequence> <xsd:attribute name='orderDate' type='xsd:date'/> <xsd:attribute name='confirmDate' type='xsd:date' use='required'/> </xsd:complexType> </xsd: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> In Notepad, create an XML document named Po.xml that contains the following code: <po:purchaseOrder orderDate=&quot;1999-10-20&quot; xmlns:po=&quot;http://www.example.org/po&quot; confirmDate=&quot;2001-08-27&quot;> <shipTo country=&quot;US&quot;> Kim Yoshida 123 Maple Street Mill Valley CA    90952 </shipTo> <billTo country=&quot;US&quot;> Neil Smith 8 Oak Avenue Old Town PA    95819 </billTo>

<item partNum=&quot;872-AA&quot;> <productName>Lawnmower</productName> 1      <USPrice>148.95</USPrice> <po:comment>Confirm that this is gas-powered.</po:comment> <item partNum=&quot;926-AA&quot;> <productName>Baby Monitor</productName> 1      <USPrice>39.98</USPrice> <po:comment>Confirm that this is electric.</po:comment> <shipDate>1999-05-21</shipDate> </po:purchaseOrder> </li> <li>Save the Po.xml file in the root folder of drive C.</li></ol>

back to the top

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

Vbsaxbysomstudent.exe

The Vbsaxbysomstudent.exe file contains the following files:

<ol> <li>Double-click the Sombysax.vbp file to open the project. You must use Visual Basic 6.0 to compile and run the project.</li> <li>On the Project menu, click References. Add a reference to Microsoft XML, v4.0.</li> <li>In Project Explorer, double-click the MySchemaDeclHandler class to open the Myschemadeclhandler.cls file.</li> <li> Implement the schemaElementDecl event handler to output the element declaration based on user selection, as follows: Private Sub IMXSchemaDeclHandler_schemaElementDecl(ByVal oSchemaElement As MSXML2.ISchemaElement)

If oSchemaElement.Name = queryElement Then results = printElement(oSchemaElement, 1) End If End Sub </li> <li> Replace the code in the Command Click event of cmdLocate with the following: ' Clear the results window. txtResults = &quot;&quot;

' Create a SAX reader. Dim oReader As New MSXML2.SAXXMLReader40

' Create an XML schema cache. Dim oSC As New MSXML2.XMLSchemaCache40

' Create a class module for implementing the error handler. Dim oValidator As New MySchemaDeclHandler

' Add the schema file to the schema cache. oSC.Add txtNamespace, txtSchemaFile

' Configure the SAX reader. oReader.putFeature &quot;schema-validation&quot;, True oReader.putProperty &quot;schemas&quot;, oSC ' schemaDecl handler oReader.putProperty &quot;schema-declaration-handler&quot;, oValidator ' Query one element. oValidator.queryElement = comboElement.Text ' Parse and validate the file. oReader.parseURL txtInputFile

txtResults.Text = oValidator.results </li></ol>

back to the top

Run the Visual Basic Application
<ol> <li>Run the Visual Basic project.</li> <li> Click Locate, and then note the declaration in the txtResults box. The following output appears in the XSD declaration text box: <xsd:element name='purchaseOrder' abstract='False' id='' type='PurchaseOrderType'/> </li> <li>In the drop-down list box, change the element name to Quantity, and then run the program again.</li></ol>

back to the top

<div class="references_section">