Microsoft KB Archive/309616

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

Article ID: 309616

Article Last Modified on 4/24/2003

-

APPLIES TO


 * Microsoft XML Core Services 4.0

-



This article was previously published under Q309616





IN THIS TASK

 * SUMMARY
 * Create the Sample XSD Schema Document
 * Create the Sample XML Document
 * Create the Visual Basic Application
 * Run the Visual Basic 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 XSD schema document. This step-by-step article describes how to program the MSXML 4.0 SOM in a Visual Basic 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

Create the Visual Basic Application
<ol> <li>In Visual Basic, create a new Standard EXE project. Form1 is created by default.</li> <li>Add a project reference to Microsoft XML version 4.0.</li> <li>Drag 2 text boxes and a command button onto Form1.</li> <li>Set the Name property of one of the text boxes to txtXPath, set the Name property of the other text box to txtResults, and set the Name property of the command button to cmdGetTypeInfo. The txtXPath text box specifies an XPath expression that is run to locate the element node in the XML document whose schema declaration information is to be extracted from the XSD schema. The txtResults text box displays the type information of the specified element as extracted from the schema.

</li> <li>Set the MultiLine and Scrollbars properties of the txtResults text box to True and Both. Resize txtResults so that it can display up to 10 lines of text with approximately 30 columns in each line of text.</li> <li> Paste the following code in the Click event procedure of the cmdGetTypeInfo command button. See the inline comments for a description of how the code functions: On Error Resume Next 'Declare a Schema cache object to cache Books.xsd. Dim sc As New MSXML2.XMLSchemaCache40 'Declare a DOMDocument object to load Books.xml. Dim doc As New MSXML2.DOMDocument40 'Add books.xsd to the schema cache. sc.Add &quot;urn:books&quot;, &quot;c:\books.xsd&quot; 'Configure the DOMDocument object to load and validate 'Books.xml against Books.xsd. Set doc.schemas = sc doc.async = False doc.setProperty &quot;SelectionNamespaces&quot;, &quot;xmlns:x='urn:books'&quot; doc.Load &quot;c:\books.xml&quot; 'Call the selectSingleNode method of the DOM object to 'locate the first node in Books.xml that matches the XPath 'expression that is specified in the txtXPath text box. Dim node As MSXML2.IXMLDOMNode Set node = doc.selectSingleNode(txtXPath.Text) 'Check to verify that the specified XPath expression 'did not generate an error. If Err.Number <> 0 Then MsgBox &quot;Invalid XPath expression&quot;,, &quot;Error&quot; Exit Sub End If 'Declare and use a SchemaCache object to hold a reference 'to the list of namespaces that are referenced by the DOMDocument. Dim docNamespaces As New MSXML2.XMLSchemaCache40 Set docNamespaces = doc.namespaces 'Run the getDeclaration method of the SchemaCache object 'to obtain the schema declaration of the node that is selected by running 'the specified XPath expression. Assign the return value to an ISchemaElement object. Dim oDecl As MSXML2.ISchemaElement Set oDecl = docNamespaces.getDeclaration(node) 'Blank out the txtResults text box. This text box is 'used to display the type information for the element that is  'referenced by the node that is returned on running the specified 'XPath expression. txtResults.Text = &quot;&quot; 'Check the type property of the ISchema element that is returned by 'running getDeclaration to determine whether it is a  'ComplexType or a simpleType element. If TypeName(oDecl.Type) = &quot;ISchemaComplexType&quot; Then 'If the element is a complexType element, obtain a      'reference to its SchemaModelGroup by using its contentModel property. Dim otype As MSXML2.ISchemaComplexType Set otype = oDecl.Type Dim scModelGroup As MSXML2.ISchemaModelGroup Set scModelGroup = otype.contentModel 'Display the complexType name of the element. txtResults.Text = &quot;Type of &quot; & txtXPath.Text & &quot; is &quot; & oDecl.Type.Name & vbCrLf & vbCrLf 'Loop through the particles of the element's SchemaModelGroup 'to display the names and type names of its subelements. txtResults.Text = txtResults.Text & &quot;Sub Elements of &quot; & txtXPath.Text & vbCrLf Dim subElem As MSXML2.ISchemaElement For Each subElem In scModelGroup.particles Set stype = subElem.Type txtResults.Text = txtResults.Text & vbTab & subElem.Name & &quot; : &quot; & subElem.Type.Name & vbCrLf Next 'Loop through the attributes of the element (if any) and 'display the names and type information of each attribute. txtResults.Text = txtResults.Text & vbCrLf & &quot;Attributes of &quot; & txtXPath.Text & vbCrLf Dim att As MSXML2.ISchemaAttribute For Each att In otype.Attributes txtResults.Text = txtResults.Text & vbTab & att.Name & &quot; : &quot; & att.Type.Name & vbCrLf Next End If 'If the element is a simpleType element, only display 'its name and type information. If TypeName(oDecl.Type) = &quot;ISchemaType&quot; Then txtResults.Text = &quot;Data type of &quot; & txtXPath.Text & &quot; is &quot; & oDecl.Type.Name End If </li></ol>

Back to the top

Run the Visual Basic Application
<ol> <li>Save and run the Visual Basic project.</li> <li> To extract and display the schema declaration information for the book complexType element, type the following XPath expression in the txtXPath text box and click the cmdGetTypeInfo command button: /x:catalog/book The following output is displayed in the txtResults text box. The output displays the data type of the book element (custom type bookdata), the names and data types of its subelements (title, price, publish_date), and the name and data type of its single attribute (id): Type of /x:catalog/book is bookdata

Sub Elements of /x:catalog/book title : string price : float publish_date : date

Attributes of /x:catalog/book id : string </li> <li> To extract and display the schema declaration information for the price simpleType element, specify the following XPath expression in the txtXPath text box and click the cmdGetTypeInfo command button: /x:catalog/book/price The following output is displayed in the txtResults text box, indicating the data type of the price simpleType element: Data type of /x:catalog/book/price is float </li></ol>

Back to the top

<div class="references_section">