ecently, “Paul” wrote to the xml.general group asking:
I have the following XML element:
HelloJohn, my nameis Paul. I would like to tell you something.
element can contain any number of and elements in any order?but I cannot seem to determine how to construct the element within the Schema file. I currently have in the schema something like this:
element allows for any ordering, but only allows to have a max of 1 and 1 element. I need to have an unbounded number of and elements in any order.
There are two problems here. First, the
When you’re working with regular structured data, such as data extracted from a relational database table, it’s relatively easy to see how you can write an XML schema, to describe the data, because:
- The database defines the data types for each column
- Any row holds one and only one data value for each column.
For example, here’s a simple customer record with three columns:
CustomerID CustomerLName CustomerFName 25 Foo John
A schema definition for this might look like:
10 Doe John
Working It Through
Sometimes, you need to ensure that a set of elements appears, but you don’t care about the order. For example, does it really matter if the CustomerID element always appears first in a document? If you’re simply scanning through an XML file, perhaps reading all
In that case, you can use the
Now, you can validate either the customer XML document shown earlier or a customer XML document with a different sub-element sequence, for example:
Doe John 10
However, when the data in XML documents is less structured, as in Paul’s question, the schema structure is less clear. One answer received from Anthony Jones states that you can solve the problem of the unknown number of child elements by adding a maxOccurs attribute with a value of “unbounded.” Unbounded means an element may appear any number of times.
But that still doesn’t solve the problem; as Paul says, the
But, that’s not a complete solution, because the
A Working Solution
Rather than xs:all, use xs:choice, with the minOccurs attribute set to 0 and the maxOccurs attribute set to unbounded. Here’s the fixed schema.
The preceding schema lets the
Schema such as this are critical when you need to be able to validate unstructured data, where it’s impossible to know the order or number of elements in advance.
Go the the xml.general group now to participate or ask your own question.