Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Taking XML Validation to the Next Level: XSD Schema vs. CAM

The generic-sounding Content Assembly Mechanism, or CAM, is an exciting step beyond XML Schema, but it's new and not well documented. This is the second in a series of articles representing "CAM: The Missing Manual."




Application Security Testing: An Integral Part of DevOps

n the first article in this series, you saw the rudiments of template-building and validation with CAM. This part of the series presents a much more detailed comparison between XML Schema and CAM.

Schema Validation

Because XML Schema is well-established, the tools for designing schema are considerably richer. CAM does not currently, for example, offer any graphical tools, whereas XML Schema has a wide variety. Probably the most well-known, XmlSpy, is an outstanding platform for XML development; however this article uses Liquid XML Studio for the examples and figures, because it's available in a free community edition. Figure 1 shows a graphical representation of the purchase order schema (see Listing 1) rendered by Liquid XML Studio. Note that it's far easier to grasp the structure of the schema from the graphical rendering than by reading the XSD file!

Figure 1. Purchase Order Schema: This graphical rendering provides a much more intuitive view of the schema file than viewing the file contents directly.

Note that both //shipTo/zip and //billTo/zip are specified as decimal types, which—as you saw in the CAM discussion in Part 1—is not adequate to ensure a proper zip code. The following example shows how to fix the schema and run the series of test cases to verify the fix.

Figure 2. Validating an XML File in Liquid XML Studio: To validate the XML file press the validate button (1). Press the schema selector (2). Press the add button (3) in the document-to-schema mappings. Press the add button (4) in the entry form to select your XSD file. Provide a name (5), close out of the dialogs, and your schema will be visible at (6).

Open the po.xml file in Liquid XML Studio—refer to Figure 2. Press F7 or the on-screen validate button (item 1 in Figure 2) to validate the file.

Author's Note: Remember that you must correct an error in the XML file—the closing bracket fix described in Part 1.

With the corrected file loaded, the application should report only that the document is well-formed. It does not validate against the schema, because you need to tell it to do so—item 6 in the figure is blank. Open the schema browser by pressing the schema selector button (item 2 in the figure). This opens the "XML Document to XML Schema mappings" window. Press the Add button (item 3) to open the Edit Schema Library Entry window where you will identify your schema file. You need to provide the file path and to give a name to the schema. Press the Add button (item 4) to open a standard file chooser and find the po.xsd file. That fills in the file path. Type a descriptive name of your choice (item 5), and then close the dialog boxes to return to the main window. The schema you selected should now appear in the properties pane (item 6). If not, re-open the schema browser and make sure the new schema has a check next to it.

Next, just as with the CAM template in Part 1, work through the test cases to see how well this schema can validate a zip code. With the zip defined as a simple decimal most of the test cases fail. Changing to an integer instead of a decimal improves the results a bit, but not much. To fully fix the schema you need to mirror the change made on the CAM side, which is to treat the zip code as a 5-character string containing all digits. The simplest way is to change the base data type to a string and set the pattern facet to "\d{5}", a regular expression indicating precisely five digits between 0 and 9. To do this in Liquid XML Studio, open the XSD file, select the zip element in the graphical view (see the arrow in Figure 3), and then adjust the type and the pattern in the properties pane as indicated in the figure.

Figure 3. Correcting the Zip Element: To properly validate a 5-digit zip code with XML Schema, change the datatype from decimal to string and use the pattern facet to restrict the string to 5 digits.

Making those adjustments automatically generates the schema code highlighted in the bottom pane, casting the zip as a subtype of string constrained by the specified facet. After this change is in place all the zip code tests should produce the correct results, giving you an XML Schema solution equivalent to the CAM solution from Part 1.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



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