ZVON > Tutorials > XML Schema and Relax NG Tutorial
Index
Contents > Simple tasks

Simple tasks

Empty element

We want to have the root element to be named "AAA", from null namespace and empty. The empty element is defined as a "complexType" with a "complexContent" which is a restriction of "anyType", but without any elements.

To specify, that element must be empty, use the "empty" element.

...
Element with text only

We want to have the root element to be named "AAA", from null namespace and containing text only.

To specify, that element contains text, use the "text" element.

...
Element with mixed content (text and only one element)

We want to have the root element to be named "AAA", from null namespace and contains text and an element "BBB". You will need to set the attribute "mixed" to "true" - to allow mixed content.

Use the "interleave" pattern.

...
Element which contains elements only (order does not matter)

We want to have the root element to be named "AAA", from null namespace and contains one "BBB" and one "CCC" element. Their order is not important.

Use the "interleave" pattern.

...
Element which contains elements only (order is important)

We want to have the root element to be named "AAA", from null namespace and contains one "BBB" element, followed by one "CCC" element. Use the "sequence" pattern to specify exact order of the elements. The attributes "minOccurs" and "maxOccurs" are not necessary, because their default value is 1.

You need not to use any pattern, because the order is implicitly important - it's quite intuitive: if you do not specify otherwise, you want the elements to appear in the same order as you put them in your schema.

...
Element which contains mixture of two elements (can appear more times)

We want to have the root element to be named "AAA", from null namespace and contains a mixture of any number (even zero) of "BBB" and "CCC" elements. You need to use the trick below - use "sequence" element with "minOccurs" attribute set to 0 and "maxOccurs" set to "unbounded", and the attribute "minOccurs" of the "element" elements must be set to 0 too.

We will use the "interleave" and "zeroOrMore" patterns.

...
Element which contains one of two elements

We want to have the root element to be named "AAA", from null namespace and contains either "BBB" or "CCC" elements (but not both). Use the "choice" element.

We will use the "choice" patterns.

...
Element which contains two "patterns" (sequences), in any order

We want to have the root element to be named "AAA", from null namespace and contains two patterns in any order. The first pattern is a sequence of "BBB" and "CCC" elements, the second one is a sequence of "XXX" and "YYY" element. The element "choice" allows one of the cases: either the sequence "myFirstSequence"-"mySecondSequence" or "mySecondSequence"-"myFirstSequence".

The element "choice" allows one of the cases: either the sequence "myFirstSequence"-"mySecondSequence" or "mySecondSequence"-"myFirstSequence".

...
Element contains text only and one attribute

This should be a simple task - we want the element "root" to contain text only and have just one attribute, named "xx".

...