Browse DevX
Sign up for e-mail newsletters from DevX


Analyze Schemas with the XML Schema Infoset Model : Page 2

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.

Analyzing Your Schemas
The first thing you'll want to do is check your schema for possibly failing to specify restrictions on integer-derived types. This ensures that all order quantities in purchase orders have been bounded. Here, the schemas must be very specific, so you want to require that all simple types deriving from integers include both min/maxInclusive or min/maxExclusive facets. However, if the min/maxInclusive or min/maxExclusive facets are inherited from and your simple types derive from the same type, that is sufficient.

While you can use XSLT or XPath to query a schema's concrete representation in an .xsd file or inside some other .xml content, it is much more difficult to discover the type derivations and interrelationships that schema components actually have. Since the XML Schema Infoset Model library models both the concrete representation and the abstract concept of the schema, it can easily be used to collect details about its components, even when the schema has deep type hierarchies or is defined in multiple schema files.

In this simple schema, some types meet the criteria of having max/min facets, and some do not. The full sample schema called FindTypesMissingFacets.xsd is included in the zip file. The following code excerpt illustrates how to write according to schema specs:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"

<!-- SimpleType missing both max/min facets -->
<xsd:simpleType name="integer-noFacets">
    <xsd:restriction base="xsd:integer"/>

<!-- Derived type has inherited min facet but missing max facet -->
<xsd:simpleType name="positiveInteger-inheritedMinFacet">
    <xsd:restriction base="xsd:positiveInteger"/>

<!-- Derived type with both effective max/min facets -->
<xsd:simpleType name="positiveInteger-bothFacets">
    <xsd:restriction base="positiveInteger-inheritedMinFacet">
        <xsd:maxExclusive value="100"/>
<!-- etc... -->

Loading Schemas into the XML Schema Infoset Model
The library can read and write schema objects from a variety of sources In the code below, the org.eclipse.emf.ResourceSet framework easily loads sets of schemas; you can also build and emit schemas directly from or to a DOM object that you manage yourself. The library provides a custom XSDResourceSet implementation that can intelligently and automatically load sets of schemas related by includes, imports, and redefines. The abstract relationship between related schemas is also modeled in the library. Take a look at the code excerpt below to see how to load a schema.

// String variable schemaURL is "FindTypesMissingFacets.xsd" or 
// the URL to your schema Create a resource set and load the 
// main schema file into it.
ResourceSet resourceSet = new ResourceSetImpl();
XSDResourceImpl xsdSchemaResource = 
    URI.createDeviceURI(schemaURL), true);

// getResources() returns an iterator over all the resources, 
// therefore, the main resource and those that have been included, 
// imported, or redefined.
for (Iterator resources = resourceSet.getResources().iterator(); 
    resources.hasNext(); /* no-op */)
    // Return the first schema object found, which is the main schema 
    //   loaded from the provided schemaURL
    Resource resource = (Resource)resources.next();
    if (resource instanceof XSDResourceImpl)
        XSDResourceImpl xsdResource = (XSDResourceImpl)resource;
        // This returns an org.eclipse.xsd.XSDSchema object
        return xsdResource.getSchema();

Thanks for your registration, follow us on our social networks to keep up-to-date