0
votes

I am trying to create an XSD, and trying to write the definition with the following requirement:

Allow child elements specified to appear only once, but one elements to appear multiple times
Allow child elements to be in any order

Example:

<parent>
<child1/>
<child2/>
<child3/>
<child3/>
</parent>

The child1 and child2 elements must be able to exchange the order but should not be repeated.

2
Welcome to Stack Overflow! Please edit your question to show the code you have so far. You should include at least an outline (but preferably a minimal reproducible example) of the code that you are having problems with, then we can try to help with the specific problem. You should also read How to Ask.Toby Speight

2 Answers

0
votes

It's not possible in general in XSD 1.0, except by listing all the possible permutations as @kjhughes has done, which becomes unmanageable if you have more than say 4 permitted element children.

In XSD 1.1 you can write

<xs:all>
  <xs:element name="child1"/>
  <xs:element name="child2"/>
  <xs:element name="child3" minOccurs="0" maxOccurs="unbounded"/>
</xs:all>
0
votes

This simple, v1.0, XSD will require a parent with exactly one child1 and one child2, in any order, among any number of child3:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="parent">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="child3" minOccurs="0" maxOccurs="unbounded"/>
        <xs:choice>
          <xs:sequence>
            <xs:element name="child1"/>
            <xs:element name="child3" minOccurs="0" maxOccurs="unbounded"/>
            <xs:element name="child2"/>
            <xs:element name="child3" minOccurs="0" maxOccurs="unbounded"/>
          </xs:sequence>
          <xs:sequence>
            <xs:element name="child2"/>
            <xs:element name="child3" minOccurs="0" maxOccurs="unbounded"/>
            <xs:element name="child1"/>
            <xs:element name="child3" minOccurs="0" maxOccurs="unbounded"/>
          </xs:sequence>
        </xs:choice>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>