Providing Options in RELAX NG - Giving the user choices, continued
(Page 2 of 4 )
It's time to go ahead and give the person element some child elements. One piece of information that we might want an instance document to represent is the person's location. But how do we represent a person's location? One way to do this would be to store the person's zip code. Another way to do this would be to store the person's city and state. We could choose one of these ways and incorporate it into the schema, but there's another option out there.
We could have the author of the document choose one method or the other. This is entirely possible. Just as it's possible to make one element optional, it's possible to allow a choice between one element and another.
So, we need to offer the user a choice between declaring a zipCode element, which represents the person's zip code, or declaring a city element and a state element, which represent the person's city and state.
In the XML schema, this is done using the choice element. Each of the choice element's children represents a choice for the user. The user will have to select only one. However, it's possible to create a choice with multiple elements by using the group element, which groups its children together. This way, the city and state elements can be grouped together as a single choice.
With this in mind, the choice element needs to have two children. One needs to be an element representing the zipCode element, and the other needs to be a group element with elements representing the city and state elements. The additions to the XML schema (made in the element representing person, of course) would look like this:
<choice>
<element name="zipCode">
<text/>
</element>
<group>
<element name="city">
<text/>
</element>
<element name="state">
<text/>
</element>
</group>
</choice>
Using the compact syntax, choices are expressed differently. In the compact syntax, choices are made using the pipe symbol (“|”) which functions and looks just like a logical OR operator in some programming languages. The entire choice section is placed in a set of parentheses, and elements are also grouped using parentheses. This may sound a bit complicated at first, but it really isn't. The resulting compact schema would look like this:
element people {
element person {
attribute date { text }?,
(element zipCode { text }
| (element city { text },
element state { text }))
}*
}
The following instance document will now compile. It contains two person elements, one which specifies location using the zipCode element, and one which specifies location using city and state elements:
<people>
<person date="2008-06-30">
<city>Topeka</city>
<state>Kansas</state>
</person>
<person>
<zipCode>29555</zipCode>
</person>
</people>
In the above example, we gave the user a choice between elements. However, it's also possible to give the user a choice between attributes, or it's possible to mix attributes and elements together in the same choice.
Interestingly enough, the optional element is actually converted into a choice element by your implementation. One of the choices is the optional element or attribute, and the other choice is simply an empty element. So, when we made the date attribute optional, we could have actually done this:
<choice>
<attribute name="date"/>
<empty/>
</choice>
The exact same result would have been achieved.
Next: Enumerations with the value element >>
More XML Articles
More By Peyton McCullough