f you've built a library of schemas, you might want to reuse them for new applications. If you already have a data model for an internal purchase order, as you move towards Web services, you may need to update it for use with SOAP. SOAP allows you to transport an XML message across a network and the XML body can also be constrained with a schema. However, a SOAP message typically uses element data for its XML body, not attribute data. We'll explore a program that can automatically update an existing schema document to convert any attribute declaration into roughly "equivalent" element declarations.
Overview: How We're Going to Do It
Given the complexity of XML Schemas, we certainly don't want to use Notepad to edit the .xsd files. A good XML editor is not much of a step up: while it may organize our elements and attributes nicely, it can't show the many abstract Infoset relationships that are defined in the Schema specification. That's where the Schema Infoset Model comes in: it expresses both the concrete DOM representation of a set of schema documents, and the full abstract Infoset model of a schema. Both of these representations are shown through the programmatic API of the Model as well as in the Model's built-in sample editor for schemas.
|This article assumes a basic understanding of schemas. The sample code included in our zip file works as a standalone or in an Eclipse workbench.|
If you've installed the XSD Schema Infoset Model and EMF Eclipse Modeling Framework plug-ins into Eclipse, you can see the sample editor at work in your Workbench. (Note: Exploring the editor is not required to follow this article). Simply right-click on a schema.xsd file in the Navigator, and select Open With... Sample XML Schema Editor. You will get a standard Eclipse editor that shows the usual Source viewthis is the concrete DOM representation of the .xsd file that you just opened.
At the bottom of the editor there are two more tabs after the Source viewSemantics and Syntax. These two graphical tree views show the various abstract Infoset relationships between schema components. For example in the Semantics view, you will see a top-level item for Typesthis is all of the types (simple and complex) declared anywhere in the schema itself, not just at the top level, and not just in this document (this becomes more obvious when the schema document you opened uses includes and imports).
|Figure 1: Changing Attributes into Elements.|
For the purposes of our example, we're going to simplify our problem slightly. MakeSoapCompatible.java is a program that will attempt to take most attributeDeclarations
in a schema and turn them into roughly-equivalent elementDeclarations
, with a few caveats.
First, we obviously can't convert an attribute into an element if there already is an element with that name in the particles of the complexType
: we will detect name conflicts first, and refuse to change these attributes. To keep our example simpler, we also arbitrarily declare some other conditions that make a schema incompatible for our program. We won't change schemas that have wildcards, since we would have to do some complex name checking to ensure that any changed attributes wouldn't conflict with elements. We also won't change any groups that use #all
as a compositor since this could change the meaning of the group in ways we can't easily predict.