Home > Articles > Web Services > XML

This chapter is from the book

This chapter is from the book

18.5 -Schema Object Model (SOM)

So far, this chapter has focused on the generic approach that MSXML provides for accessing XML schemas. This consists of accessing the XML schema documents directly as ordinary XML documents. In this manner, you can work with them programmatically, using the same DOM you use for any other document. This approach is limited, however, because you are working with the least common denominator. XML schemas have a higher structure that has already been defined for us, so it makes sense to use a higher level API to access them if one is available.

Beginning with version 4.0 of MSXML, this higher level API is available and is called the "Schema Object Model" (SOM). The SOM is a set of components and interfaces that represent the compiled structure of an XML schema rather than just the structure of its XML.

18.5.1 SOM Fundamentals

The SOM can be used to load an XML schema document, and that document is then loaded into a tree structure that closely models the relationships of an XML schema document. Rather than a DOM-based tree that treats the elements of the schema document as normal XML, the SOM understands the Schema-based relationships between the parts of a schema document and models those relationships in the SOM structure. For example, XML schema document element elements are represented by the ISchemaElement, which has a type property. That property corresponds to an XML Schema document simpleType or complexType element, and that is represented in the SOM by the ISchemaType interface. This compiled model makes the SOM much more attractive than the DOM for developers that need to examine XML schemas. Table 18.3 describes how each interface in the model relates to an XML schema, but it does not illustrate how some of the interfaces relate.

TABLE 18.3 Schema Object Model Interfaces

Interface

Description

ISchema

schema element

ISchemaAny

any element

ISchemaAttribute

attribute element

ISchemaAttributeGroup

attributeGroup element

ISchemaComplexType

complexType element

ISchemaElement

element element

ISchemaIdentityConstraint

complexType element

ISchemaItem

Base interface

ISchemaItemCollection

Collection of SOM objects

ISchemaModelGroup

modelGroup element

ISchemaNotation

notation element

ISchemaParticle

Piece of a modelGroup element

ISchemaStringCollection

Collection of strings in the SOM

ISchemaType

simpleType element or complexType element


Many of the SOM interfaces inherit from other SOM interfaces. Figure 18.3 shows where that inheritance occurs.

To help explain the SOM, we can take a look at some of the principal interfaces that make up the SOM. The first of those interfaces is ISchemaItem, the foundation of all the SOM interfaces.

This chapter does not provide a complete reference to all the properties and methods of the Schema Object Model. For a complete list, see the documentation provided with the MSXML 4.0 SDK.

18.5.2 The ISchemaItem Interface

In most object models, you will find some root class or interface from which all the objects in the system inherit. In Java, this root is the Object class. In the .NET Framework, the same is true: A root class called "Object" exists for all object classes. Within the framework of the SOM, all interfaces inherit from the ISchemaItem interface. Through the common properties of the ISchemaItem interface, you can learn a great deal about any of the component instances in the model. Table 18.4 lists the properties of the ISchemaItem interface.

Figure 18.3 FIGURE 18.3 SOM diagram.


TABLE 18.4 Property Summary of the ISchemaItem Interface

Property

Description

Id

Value of id attribute of the element

itemType

Constant defining the type of the object.

Name

Item name.

namespaceURI

URI of associated namespace.

Schema

ISchema of this model.

writeAnnotation

Writes top-level annotations into an output document.

unhandledAttributes

-Any attributes not defined in the XML Schema Recommendation.


The ISchemaItem interface provides two properties you might find yourself relying upon. The first is the Schema property, which returns the ISchema interface for the XML schema of which this particular item is a part. This means that from any item in the SOM, you can reach the root. This is not true, however, for built-in types and the ISchema interface itself (to avoid a circular reference problem).

The second property is the itemType property, which returns one of a list of constants provided in the SOM. These constants define the type of item this instance of ISchemaItem represents (for example, built-in type, complex type, attribute, notation). These constants are contained in the SOMITEM_TYPE enumeration.

Some of the SOMITEM_TYPE constants are used alone to indicate the type of the object in question, whereas others are combined to create new values that determine the type. For example, SOMITEM_SCHEMA indicates that an item is a schema and therefore is also an ISchema interface. On the other hand, SOMITEM_DATATYPE and SOMITEM_DATATYPE_BOOLEAN are combined in a bit-mask to indicate a built-in Boolean datatype. With one or more of these constants, all the types can be expressed.

18.5.3 -The ISchema Interface

While the ISchemaItem is the foundation of the SOM, the ISchema interface is at the root of the schema model itself. The ISchema interface of the Schema Object Model directly corresponds to the schema element of the XML schema. Through the ISchema interface, you have access to all the other elements that make up the schema.

To get an ISchema interface from an XML document, you use a utility component included in MSXML, XMLSchemaCache40. XMLSchemaCache40 functions as a map of target namespaces to valid XML schemas. When the add method of XMLSchemaCache40 is called, as shown in Listing 18.8, the XML schema document is read to make sure it conforms to the XML Schema Recommendation. If it does, it is loaded into the cache. If it does not, an error is thrown by the add method.

LISTING 18.8 Getting the ISchema Interface

Dim schemaCache As XMLSchemaCache40
Set schemaCache = New XMLSchemaCache40

schemaCache.Add "http://www.XMLSchemaReference.com/examples/theme/addr", _
 "c:\temp\address.xsd"

Dim schema As ISchema
Set schema = schemaCache.getSchema(_
 "http://www.XMLSchemaReference.com/examples/theme/addr")

The add method in the preceding code takes two parameters: the namespace to associate with the XML schema and the XML schema itself (either as a URL or as a DOMDocument). Typically, the namespace you would associate with the XML schema would be the targetNamespace of the XML schema document, but the SOM does not enforce this relationship. Once we have an ISchema interface, we have access to all the parts that make up an XML Schema. These are provided as properties of the ISchema interface, as listed in Table 18.5.

Some of the properties listed in Table 18.5 have values that are nothing more than strings that provide access to information such as the version or namespace. However, properties that allow programmatic access to the remainder of the XML schema's components are more complex than that. All the other seven properties represent collections of objects, such as the attributes or structure types. Because the SOM is implemented as a COM programming interface, these are COM collections that are easily traversed by using just a few lines of Visual Basic code. Each of these collections is represented as an ISchemaItemCollection interface.

Listing 18.9 illustrates how to traverse one of the collections provided by the ISchema interface. In this instance, a For . . . Each loop cycles through all the elements in the XML schema and prints the output to the debugger.

LISTING 18.9 Walking the SOM Elements Collection

Dim elem As ISchemaElement
For Each elem In schema.elements
    Debug.Print elem.Name
Next elem

TABLE 18.5 Property Summary of the ISchema Interface

Property

Description

attributeGroups

Collection of ISchemaAttributeGroup

attributes

Collection of ISchemaAttribute

elements

Collection of ISchemaElement

modelGroups

Collection of ISchemaModelGroup

notations

Collection of ISchemaNotation

schemaLocations

Location of linked schemas

targetNamespace

String, target namespace for the XML schema

types

Collection of ISchemaType

version

String version of the XML schema


18.5.4 -DOM versus SOM

We have worked with XML documents by using both the DOM and the Schema Object Model (SOM), and each model has limitations. Because of their tight integration in the MSXML components, separating the core MSXML components from SOM components and deciding which approach is needed for a particular problem is difficult.

Obviously, the SOM is designed to read XML schema documents—and only XML schema documents. The DOM, on the other hand, works with any well-formed XML document. In cases where an application must examine an XML schema document, it makes sense to use the SOM, the higher-level API, to determine the components of the schema.

This advantage of the SOM, however, applies only to the reading of an XML schema. As mentioned earlier, the SOM is a read-only representation of an XML schema, loaded from a document. The SOM cannot be used to create or modify an XML schema document, only to examine it. If your application must create an XML schema document or modify an existing XML schema document, the application should use the DOM or SAX, or perhaps even a transformation for that purpose.

18.5.5 -Creating XML Schemas

From an application, you are most likely going to programmatically access an XML schema to read its contents. XML schema documents are typically written to function as a binding contract; as such, their creation typically involves collaboration between developers and designers and even outside parties. That creating or modifying a schema programmatically is the exception—and reading a schema is the rule—is evident in that the SOM provides no mechanism for performing either creation or modification. The SOM is only a tool for reading XML schemas and using them to validate an XML instance.

Of course, we can still create an XML schema document with MSXML. There are cases where creating or modifying an XML schema might be necessary. For example, an application that exports a database schema to an XML schema document needs to create an XML schema document. In those cases, developers must return to the standard APIs for XML. The DOMDocument40 component provides not only the capability to read an XML document and traverse the tree, but also to create and modify documents.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020