thomas.schuerger
thomas.schuerger

Reputation: 121

How can an XML schema allow simple content or a complex type for an element?

In an XML 1.0 or 1.1 schema, how can I define an element that can either be simple content (e.g. an integer) or a complex type, e.g. a tag with attributes?

For example, I want to allow a "number" tag to allow either specifying an integer directly or details how to random-generate it:

Both

<number>12345</number>

and

<number><random min="10000" max="15000"/></number>

should be accepted. How can this be done?

Upvotes: 0

Views: 155

Answers (1)

kimbert
kimbert

Reputation: 2422

You cannot. In XML Schema, the content of a tag is described in one of two ways:

a) a tag with simple content, possibly including the attributes in the open tag (a simple type with complex content)

b) a tag with complex content. In this case, all of the character data in between all of the child tags is called 'mixed content'. It might be scattered throughout the tag's content in various pieces in between the child tags. Mixed content cannot be assigned a type, and it is normal for applications to ignore it. Its most common purpose is for 'pretty printing' an XML document by adding newlines and indentation to show the structure.

Best practice would be to model the two variants using a choice of two different child tags (number and randomNumber).

Upvotes: 3

Related Questions