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



The information in this article applies to:

  • Microsoft XML 4.0

This article was previously published under Q312554

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:
  1. In Notepad, create an XSD document named Po.xsd that contains the following code:
    <xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'
                xmlns:po = "http://www.example.org/po"
                targetNamespace = "http://www.example.org/po">
    
     <xsd:annotation>
      <xsd:documentation>
       Purchase order schema for Example.com.
       Copyright 2000 Example.com. All rights reserved.
      </xsd:documentation>
      <xsd:appinfo>
      </xsd:appinfo>
     </xsd:annotation>
    
     <xsd:element name='comment' type='xsd:string'/>
    
     <xsd:element name='purchaseOrder' type='po:PurchaseOrderType'/>
    
     <xsd:complexType name='USAddress'>
      <xsd:sequence>
       <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>
    					
  2. Save Books.xsd in the root folder of drive C.
back to the top

Create the Sample XML Document

  1. In Notepad, create an XML document named Po.xml that contains the following code:
    <po:purchaseOrder orderDate="1999-10-20" xmlns:po="http://www.example.org/po" confirmDate="2001-08-27">
      <shipTo country="US">
        <name>Kim Yoshida</name>
        <street>123 Maple Street</street>
        <city>Mill Valley</city>
        <state>CA</state>
        <zip>90952</zip>
      </shipTo>
      <billTo country="US">
        <name>Neil Smith</name>
        <street>8 Oak Avenue</street>
        <city>Old Town</city>
        <state>PA</state>
        <zip>95819</zip>
      </billTo>
    
      <items>
        <item partNum="872-AA">
          <productName>Lawnmower</productName>
          <quantity>1</quantity>
          <USPrice>148.95</USPrice>
          <po:comment>Confirm that this is gas-powered.</po:comment>
        </item>
        <item partNum="926-AA">
          <productName>Baby Monitor</productName>
          <quantity>1</quantity>
          <USPrice>39.98</USPrice>
          <po:comment>Confirm that this is electric.</po:comment>
          <shipDate>1999-05-21</shipDate>
        </item>
      </items>
    </po:purchaseOrder>
  2. Save the Po.xml file in the root folder of drive C.
back to the top

Download the Visual Basic Sample

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

File nameSize
Schemahandler.cls11KB
Sombysax.vbp1KB
Form1.frm5KB
  1. Double-click the Sombysax.vbp file to open the project. You must use Visual Basic 6.0 to compile and run the project.
  2. On the Project menu, click References. Add a reference to Microsoft XML, v4.0.
  3. In Project Explorer, double-click the MySchemaDeclHandler class to open the Myschemadeclhandler.cls file.
  4. 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
    					
  5. Replace the code in the Command Click event of cmdLocate with the following:
    ' Clear the results window.
    txtResults = ""
    
    ' 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 "schema-validation", True
    oReader.putProperty "schemas", oSC
    ' schemaDecl handler
    oReader.putProperty "schema-declaration-handler", oValidator
    ' Query one element.
    oValidator.queryElement = comboElement.Text
    ' Parse and validate the file.
    oReader.parseURL txtInputFile
    
    txtResults.Text = oValidator.results
    					
back to the top

Run the Visual Basic Application

  1. Run the Visual Basic project.
  2. 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'/>
    					
  3. In the drop-down list box, change the element name to Quantity, and then run the program again.
back to the top

REFERENCES

For comprehensive samples that demonstrate how to program the Microsoft XML SOM to implement common application requirements, see the SOM Developer's Guide in the Microsoft XML 4.0 Software Development Kit (SDK) documentation.

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 KB312554 kbAudDeveloper