RELAX NG Compact


In addition to the regular RELAX NG syntax, there is a compact form of the syntax that does not use XML as a format. Therefore, it can be viewed as a more capable, modernized version of the DTD. The format is similar to the regular form of the syntax. However, the elements are not expressed using XML syntax. Listing 7-22 shows a RELAX NG form of the schema defined in Listing 7-1.

Listing 7-22: RELAX NG compact schema

image from book
      start =        element productCatalog {          element product { productDefinition }+        }      productDefinition =        attribute id { xsd:integer },        element shortName { text },        element fullName { text },        element description { text },        element components { productDefinition }* 
image from book

Each of the patterns of the regular RELAX NG syntax has an analog in the compact syntax. The choice of form has more to do with the desire or need to use XML syntax when defining your schema, rather than capabilities of the language. If you need to manipulate the schema using other XML tools, then the regular syntax is most appropriate. If you want to avoid "the angle bracket tax" that XML is sometimes called, look to the compact syntax.

Most of the elements in the XML form of RELAX NG have corresponding items in the compact syntax, but some of the structures do bear describing. Most of these relate to the way you use the compact syntax to define zeroOrMore, oneOrMore, optional, and similar patterns.

If an element can occur zero or more times in a schema, you use the asterisk (*) to mark the pattern as shown previously in Listing 7-22. If the element must occur at least once (oneOrMore), you replace the asterisk with a plus sign (+) as shown in the following code:

Note 

Optional elements use the question mark (?) as shown in the qty element.

      element order {        element orderDate { text },        element orderItems {          element sku { text },                element price { text },          element qty { text }?        }+      } 

In the XML form of RELAX NG, you use the choice element to enable one of multiple items. In the compact syntax, you do this with the pipe (|) character. If one or more of the items must occur together (as in the previous group pattern), you wrap them in parentheses like this:

      element contact {        (element fullName { text } |          (element givenName { text }, element surname { text } )),        element email { text }      } 

In this schema, either fullName or the combination of givenName and surname must be used. As the givenName and surname must occur together, they are included within a second set of parentheses.

These patterns work the same way for attributes as they do for elements. For example, if an attribute needed a value to be one of a set of options, you use the same notation:

      element item {        attribute partNum { text },        attribute color { "red" | "blue" | "green" | "black" }      } 

This specifies that the color attribute can have a value of red, blue, green or black.

Just as with the longer format, you can create named definitions using the compact syntax. For example:

      element order {        element shipAddress { address },        element orderItems { item+ }      }      address =        element street { text },        element city { text },        element state { text },        element zip { text }      item =        attribute id { text },        element name { text } 

If you combine these definitions, you use |= (for combine=“choice”) or &= (for combine=ℌinterleave”) to describe how you want the definitions to be included within the main schema.

The compact version of the RELAX NG syntax takes a bit longer to get used to-keeping the various symbols straight can take a while. However, when you are comfortable with these symbols, they can save a great deal of space. For example, the Atom 1.0 specification written using the XML form of RELAX NG is approximately 16KB in size. The equivalent RELAX NG compact syntax form is approximately 7KB. If you must transfer your schema many times, or if the connections are slower, it may make sense to take advantage of this briefer format.




Professional XML
Professional XML (Programmer to Programmer)
ISBN: 0471777773
EAN: 2147483647
Year: 2004
Pages: 215

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net