Browse DevX
Sign up for e-mail newsletters from DevX


Analyze Schemas with the XML Schema Infoset Model : Page 3

IBM's new XML Schema Infoset Model provides a complete modeling of schemas themselves, including the concrete representations as well as the abstract relationships within a schema or a set of schemas. Learn how to use this powerful library to perform complex queries on your own schemas.

Convenient Schema Querying
Now that you have an XSDSchema object, query it to find any types that are missing max/min facets. The code below uses some of the available library methods to quickly find all of its simpleTypeDefinitions that derive from the built-in integer type. Since the library provides a complete model of the abstract meaning of a schema, this turns out to be very straightforward. You can query the XSDSchema for its getTypeDefinitions() listing, and then filter for XSDSimpleTypeDefinitions that actually inherit from the base integer type:
// A handy convenience method quickly gets all 
//   typeDefinitions within the schema
List allTypes = schema.getTypeDefinitions();
ArrayList allIntegerTypes = new ArrayList();

for (Iterator iter = allTypes.iterator(); 
        iter.hasNext(); /* no-op */)
    XSDTypeDefinition typedef = (XSDTypeDefinition)iter.next();
    // Filter out for only simpleTypes...
    if ((typedef instanceof XSDSimpleTypeDefinition) 
        // ... and filter for built-in integer types
        // Use a worker method in the very handy sample 
        //  program org.eclipse.xsd.util. XSDSchemaQueryTools
        && XSDSchemaQueryTools.isTypeDerivedFrom(typedef, 
                schema.getSchemaForSchemaNamespace(), "integer"))
        // The filter found one; save it and continue.

The Schema Components Model
Every component defined in the W3C schema specifications is modeled in detail in the library. When you have a list of all the XSDSimpleTypeDefinitions that derive from an integer, you can query this list for ones that are missing either their max or min facets, and produce a report. Note that the library can conveniently group the effective max/minExclusive or max/minInclusive facets together for quick searching. It also provides detailed access to each type—including the actual lexical values if needed. Listing 1 shows an example of how to query XSDSimpleType components.

Your Report: Types Missing max/min Facets

As you can see, with just a little bit of code, you can discover some fairly detailed information about the schema. Download the sample code and run it against the provided schema file. The following code will result:

Schema missing max/min facet report on: 
Schema named component: 
  is missing these required facets:  
  XSDMaxFacet (either inclusive or exclusive)

Schema named component: 
  is missing these required facets:  
  XSDMaxFacet (either inclusive or exclusive)
  XSDMinFacet (either inclusive or exclusive)

Schema named component:
  is missing these required facets:  XSDMaxFacet 
  (either inclusive or exclusive)
Schemas Made Easier
Although this is a contrived example, it does show how the XML Schema Infoset Model's detailed representation of a schema makes it easy to find exactly the parts of a schema you need. The library provides setter methods for the properties of schema components, so it is easy to update your sample to automatically fix any found types by adding any missing facets. And since the library models the concrete representation of the schema as well, you can write your updated schema back out to an .xsd file.

Shane Curcuru has been a developer and quality engineer at Lotus and IBM for 12 years and is a member of the Apache Software Foundation. He has worked on such diverse projects as Lotus 1-2-3, Lotus eSuite, Apache's Xalan-J XSLT processor, and a variety of XML Schema tools. Reach him via e-mail at shane_curcuru@us.ibm.com.
Thanks for your registration, follow us on our social networks to keep up-to-date