Flylib.com

Books Software

 
 
 

XML Schema Tools


XML Schema Tools

A number of tools are at your disposal to create and validate XML Schema documents. You can find a large list of such tools at http://www.w3.org/XML/Schema#Tools. Although a large list is available, I will review only the two that are commonly used by developers-Microsoft's Visual Studio 2005 and Altova's XMLSpy.

Microsoft's Visual Studio 2005

Microsoft made strong efforts to incorporate XML development into its latest development environment. Microsoft's Visual Studio 2005 can do quite a bit more than XML development, but it allows you to create XML-based documents of many kinds easily, including XML Schema documents.

A XSD document type in Visual Studio enables you to visual create schema documents. You can also create hem directly in the code view as well. Figure 6-4 shows Visual Studio 2005 open with the design surface for the schema document.

image from book
Figure 6-4

From the available Toolbox, you can drag and drop elements directly onto the design surface. The tool creates all the required code on your behalf . Figure 6-5 shows the screen when elements have been created on this design surface.

image from book
Figure 6-5

As you can see from the figure, you can select the datatypes from a drop-down list directly in the designer. Next, this chapter reviews Altova's XMLSpy.

Altova's XMLSpy

Altova's XMLSpy is a powerful tool recommended for any serious XML programmer. You will find that the XSD capabilities built into the tool allow you to perform pretty much any XSD task including creating schemas from scratch, from other XML documents, and more. Figure 6-6 shows how XMLSpy allows you to code the schema document directly.

image from book
Figure 6-6

You can also design the schema visually and let XMLSpy create the schema code on your behalf. This is presented in Figure 6-7.

image from book
Figure 6-7



Summary

Just as you could probably see the power and reasoning behind DTDs, I hope that you can appreciate that XML Schema documents are that much more powerful. Their power comes in their incredible flexibility. This chapter took a look at the principles you need to build XML Schema documents and focused on building elements, attributes, and more. You will find that schema documents are a big part of most XML technologies-whether you are working with Web services, RSS feeds, or others.

The next chapter looks at another schema technology that is quite flexible as well, but is fairly new on the scene-RELAX-NG.



Chapter 7: RELAX NG

The previous two chapters looked at two of the more commonly used formats for defining XML vocabularies. This chapter continues this survey of schema languages by looking at RELAX NG. This is a relatively new XML schema definition language from the Organization for the Advancement of Structured Information Standards (OASIS). OASIS is a standards body-similar to the W3C-that was originally created as an improved version of DTDs, and can leverage some of the functionality of XML Schema. To further these goals, RELAX NG was designed to provide a highly readable format that could be used to define complex vocabularies.

This chapter will look at the two forms of the RELAX NG specification: the standard form and the compact form. It will show the way you use these two forms to define XML vocabularies, and compare it with DTDs and XML Schemas. Along the way, you will see how RELAX NG integrates with XML tools and programming languages for validating XML documents and creating objects that map to RELAX NG schemas.

Why Another Schema Language?

At first glance, there seems to be little need for yet another schema language. DTDs have been around since SGML, so most people are aware of them, and most applications can work with them. XML Schema is also commonly available, integrated with many XML tools, and ratified by the W3C. Still, there are great reasons why you should consider using RELAX NG.

The primary reason to use RELAX NG is simplicity. Anyone who has read the W3C XML Schema specification (all three parts ) knows that they are not simple. RELAX NG was designed from the outset to be simple to understand and implement. Listing 7-1 shows a RELAX NG schema. Even without knowing the structure of a RELAX NG schema or reading the specification, it is fairly easy to understand the intent of each of the elements.

Listing 7-1: RELAX NG Schema

image from book
<?xml version="1.0" encoding="UTF-8"?> <grammar xmlns="http://

relaxng

.org/ns/structure/1.0" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"> <start> <element name="productCatalog"> <oneOrMore> <element name="product"> <ref name="productDefinition"></ref> </element> </oneOrMore> </element> </start> <define name="productDefinition"> <attribute name="id"><data type="integer"/></attribute> <element name="shortName"><text /></element> <element name="fullName"><text /></element> <element name="description"><text /></element> <element name="

components

"> <ref

name

="productDefinition" /> </element> </define> </grammar>
image from book

In the preceding schema, you see that it defines a grammar (the root node). Within that grammar, the root element is a productCatalog . This productCatalog includes one or more products. Each product, in turn , includes a number of sub-elements. The name of each pattern in a RELAX NG schema almost literally guides the reader to describe the structure, and this is one of the main benefits in its use.

In addition to simplicity, RELAX NG was defined to be much more modular and composable than DTDs or XML Schema. Features such as W3C Schema data types can be integrated with RELAX NG schemas. In addition, RELAX NG makes it easy to break your schema into multiple files. This enables you to define standardized schema elements (such as a standard address schema) and combine them into a larger schema.

Finally, RELAX NG provides two forms: a normal syntax that uses XML, and a compact syntax. This enables you to define your XML vocabularies using either the well- formed normal syntax, or the more DTD-like compact syntax.