Partager via


po2.vbs Step-by-Step

 

[This topic discusses features that were only implemented in MSXML 6.0.]

This topic walks you through the Locate Declarations application. The code is interspersed with textual comments that describe each step of the application.

Click here for the Uncommented Code for the Locate Declarations Application.

The Application

The code begins with some constant declarations.

' Item types:
SOMITEM_SCHEMA                    = 4*1024
SOMITEM_ATTRIBUTE                 = SOMITEM_SCHEMA + 1
SOMITEM_ATTRIBUTEGROUP            = SOMITEM_SCHEMA + 2
SOMITEM_NOTATION                  = SOMITEM_SCHEMA + 3

SOMITEM_ANYTYPE                   = 8*1024
SOMITEM_DATATYPE                  = SOMITEM_ANYTYPE+256
SOMITEM_SIMPLETYPE                = SOMITEM_DATATYPE+256
SOMITEM_COMPLEXTYPE               = 9*1024

SOMITEM_PARTICLE                  = 16*1024
SOMITEM_ANY                       = SOMITEM_PARTICLE+1
SOMITEM_ANYATTRIBUTE              = SOMITEM_PARTICLE+2
SOMITEM_ELEMENT                   = SOMITEM_PARTICLE+3
SOMITEM_GROUP                     = SOMITEM_PARTICLE+256

SOMITEM_ALL                       = SOMITEM_GROUP+1
SOMITEM_CHOICE                    = SOMITEM_GROUP+2
SOMITEM_SEQUENCE                  = SOMITEM_GROUP+3
SOMITEM_EMPTYPARTICLE             = SOMITEM_GROUP+4

SCHEMAUSE_OPTIONAL   = 0
SCHEMAUSE_PROHIBITED = 1
SCHEMAUSE_REQUIRED   = 2

SCHEMACONTENTTYPE_EMPTY        = 0
SCHEMACONTENTTYPE_TEXTONLY     = 1
SCHEMACONTENTTYPE_ELEMENTONLY  = 2
SCHEMACONTENTTYPE_MIXED        = 3

remarks = 0

Create a schema cache object. This object will be used later to contain the XML Schema document, po2.xsd.

Set oSchemaCache = CreateObject("Msxml2.XMLSchemaCache.6.0")

Add the XML Schema document to the schema cache, using its add method. A SOM schema object is returned. The SOM interfaces will now be used to explore the schema object.

nsTarget="http://www.example.microsoft.com/po"
oSchemaCache.add nsTarget, "po2.xsd"

The following block of code sets properties on a DOMDocument. For more information about DOMDocuments, see the IXMLDOMDocument/DOMDocument interface, and the setProperty method of DOMDocument2.

Set oDoc = CreateObject("Msxml2.DOMDocument.6.0")
oDoc.async = false
oDoc.validateOnParse = false

The schema cache is stored in the schemas property of the DomDocument.

set oDoc.schemas = oSchemaCache
oDoc.load "po2.xml"
oDoc.setProperty "SelectionLanguage", "XPath"
oDoc.setProperty "SelectionNamespaces", "xmlns:po='http://www.example.microsoft.com/po'"
oDoc.setProperty "SelectionNamespaces", "xmlns:po='http://www.example.microsoft.com/po'"

result = ""

For the next three groups of code, set the node object oNo to the DOM node for the specified section of the XML document.

Set oNo = oDoc.selectSingleNode("//po:purchaseOrder/shipTo/@country")
result = result + printDecl(oNo) + vbNewLine

Set oNo = oDoc.selectSingleNode("//po:purchaseOrder/items/item/quantity")
result = result + printDecl(oNo) + vbNewLine

Set oNo = oDoc.selectSingleNode("//po:purchaseOrder/items/item/@partNum")
result = result + printDecl(oNo) + vbNewLine

WScript.Echo result

Create a function to create a declaration object. This function will examine the itemType to determine whether the declaration is an element or an attribute.

Function printDecl(oNode)

Use the getDeclaration method that is implemented by the namespaces method of the DOMDocument2 object to retrieve a declaration object for the node object that is passed to this function.

    Set oDecl = oDoc.namespaces.getDeclaration(oNode)

Check the itemType of the declaration object. If the type is an element, pass the declaration object to a function that will examine the properties of the object.

    If oDecl.itemType = SOMITEM_ELEMENT Then
        printDecl = printElement(oDecl, 1)
    End If

If the type is an attribute, send the declaration object to a function that will examine the properties of the attribute object.

    If oDecl.itemType = SOMITEM_ATTRIBUTE Then
       printDecl = printAttr(oDecl, 1)

Send a type object to a function that will extract its properties. The type object is the type specified in the type attribute of the current attribute.

       printDecl = printDecl + processType(oDecl.Type, 1)
    End If
End Function