I want to model a situation where you can have multiple children of an element, and the children can be different types and in any order. A simple example of what I mean is at [1]. I am trying to make a schema to define this document, my attempted schema at [2].

If I understand the format correctly I need to make the parent element (building) a complex type, and this must have an order indicator to contain other elements. None of the 3 order indicators (All, Choice, Sequence) fit my model.

How do I do this? It seems I am missing something fundamental, or I am trying to model data that cannot be fit to xml.

Thank you for any help.

[1] 
<building name="house1">
  <window state="open"/>
  <door state="open"/>
  <door state="closed"/>
  <window state="closed"/>
</building>

[2]
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="building">
    <xs:annotation>
      <xs:documentation>a building</xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:sequence> <!-- This tag is the problem -->
        <xs:element name="window" maxOccurs="unbounded">
          <xs:complexType>
            <xs:attribute name="state" type="xs:string" use="required"/>
          </xs:complexType>
        </xs:element>
        <xs:element name="door" maxOccurs="unbounded">
          <xs:complexType>
            <xs:attribute name="state" type="xs:string" use="required"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Recommended Answers

All 5 Replies

replace <sequence> with <choice>, and use the attributes 'minOccurs="0" maxOccurs="unbounded"'. Take out the maxOccurs on each of the potential children in the choice. Something like this:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="building">
    <xs:annotation>
      <xs:documentation>a building</xs:documentation>
    </xs:annotation>
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="window" >
          <xs:complexType>
            <xs:attribute name="state" type="xs:string" use="required"/>
          </xs:complexType>
        </xs:element>
        <xs:element name="door" >
          <xs:complexType>
            <xs:attribute name="state" type="xs:string" use="required"/>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

What does this mean? it means that the author of a document may choose only one of the potential children, but do so as many times as they like; i.e. they can pick children in any order and any number of times. It's a nasty workaround to, what I feel is a nasty shortcoming in the W3C schema language. It's a nasty workaround, because you lose any level of evaluation of the actual provided sequence of children; all you can say is that there's some number of them.

Please, please don't assume that if you can't describe something, or can't well define it with W3C schema that the data "shouldn't" be modelled in XML!! there are other schema languages, RelaxNG/Schematron for eg. W3C schema are near-awful for anything where structure is more important than datatyping.

replace <sequence> with <choice>, and use the attributes 'minOccurs="0" maxOccurs="unbounded"'. Take out the maxOccurs on each of the potential children in the choice. Something like this:
...

Thanks, that does exactly what I want. It does not seems to do what w3schools says it does:

from http://www.w3schools.com/schema/schema_complex_indicators.asp

The <choice> indicator specifies that either one child element or another can occur:

Is there a more accurate description of what these tags really mean?

Thank you for the help.

It does mean that only one of the elements within the choice can be chosen - but putting maxOccurs = unbounded means that you can make that choice an unbounded number of times at that point... It's similar to how one would express the same constraint in DTD language:

i.e.

<!ELEMENT house ( door | window | roof ) * >

That means, house may contain ( ANY NUMBER OF [ * ] ( door OR window OR roof ) ).. so, first element is a door, ok that meets ( door OR window OR roof ), second is a door, ok that meets ( door OR window OR roof ), third is a roof.. and so on. Since your allowed an unbounded number of choices, you can pick any of the elements allowed by the choice, any number of times, in any order.

Hm, the best way to learn how to use w3c schema is to actually look at the working schema for some XML languages, that and consulting the actual specification for schema http://www.w3.org/XML/Schema#dev ...although, w3cschools isn't that helpful since it only details each element's functionality singularly, not really as they interact with each other.

Thank you very much. I understand much better now.

I shall have a look at the specification but it is not a short document.

Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.