Choice element in XML Schema with any order, any number and "one element restriction"?

StackOverflow https://stackoverflow.com/questions/23151671

  •  05-07-2023
  •  | 
  •  

Вопрос

What I'm trying to do is, declare an parent element called "data", which are having 6 sub element of these one element are conditional that means one element is appear only and only one time and other can appear any number of times with any order.

Example:

<data>
   <Child1>text1</Child1>
   <Child2>text1</Child2>
   <Child3>text1</Child3>
   <Child4>text1</Child4>
   <Child5>text1</Child5>
   <Child6>text1</Child6> <!-- Only one times-->


</data>

1 : All element can appear in any order and any number of times.

2 : Child6 is appear only one time.

XSD Code

<xs:element name="data">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="sequence" minOccurs="0" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:choice minOccurs="0" maxOccurs="unbounded" >
                            <xs:element ref="Child1" />
                            <xs:element ref="Child1" />
                            <xs:element ref="Child3" />
                            <xs:element ref="Child4" />                          
                            <xs:element ref="Child5" />
                            <xs:element ref="Child6" minOccurs="0" maxOccurs="1"/>

                        </xs:choice>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

Please suggest me how can i do it.?

Это было полезно?

Решение

The XSD below will validate according to these rules:

  • Allows Child1 to Child5 elements in any order, all optional, repeating any number of times.
  • Allows only one Child6 element which, if present, must be positioned after all the other elements.

<xs:element name="data">
    <xs:complexType>
        <xs:sequence>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
                <xs:element name="Child1" />
                <xs:element name="Child2" />
                <xs:element name="Child3" />
                <xs:element name="Child4" />                          
                <xs:element name="Child5" />
            </xs:choice>
            <xs:element name="Child6" minOccurs="0" maxOccurs="1"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

To allow Child6 to also appear anywhere, you can use xs:all but you'll need a XSD 1.1 compatible parser since XSD 1.0 parsers won't allow elements in xsd:all to have unbounded occurrences:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
    xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1"> 

    <xs:element name="data">
        <xs:complexType>
            <xs:all>
                <xs:element name="Child1" minOccurs="0" maxOccurs="unbounded" />
                <xs:element name="Child2" minOccurs="0" maxOccurs="unbounded" />
                <xs:element name="Child3" minOccurs="0" maxOccurs="unbounded" />
                <xs:element name="Child4" minOccurs="0" maxOccurs="unbounded" />                          
                <xs:element name="Child5" minOccurs="0" maxOccurs="unbounded" />
                <xs:element name="Child6" minOccurs="0" maxOccurs="1"/>
            </xs:all>
        </xs:complexType>
    </xs:element>

</xs:schema>

You can achieve something in between with XSD 1.0, but you'll have to decide if it is worth writing a more complex and less reusable schema to allow for the flexibility you want in placing your elements.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top