Вопрос

What I'm trying to do is, declare an parent element called "data", which are having 10 sub element of these one element are conditional.

My XSD is:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" elementFormDefault="qualified" attributeFormDefault="unqualified" vc:minVersion="1.1">
    <xs:element name="data" >
        <xs:complexType>
            <xs:sequence>
                <xs:element name="sub_data" minOccurs="0" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:all >
                            <xs:element ref="A"/>
                            <xs:element ref="B" minOccurs="0" maxOccurs="1"/>
                            <xs:element ref="C"/>
                            <xs:element ref="D"/>
                            <xs:element ref="E"/>
                            <xs:element ref="F"/>
                            <xs:element ref="G"/>
                            <xs:element ref="H"/>
                            <xs:element ref="I"/>
                            <xs:element ref="J"/>
                            <xs:element ref="K"/>
                            <xs:element ref="L"/>
                            <xs:element ref="M"/>
                            <xs:element ref="N"/>
                            <xs:element ref="element_group"/>
                        </xs:all>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
            <xs:attribute name="status"/>
        </xs:complexType>
    </xs:element>

    <xs:element name="O" type="xs:string" substitutionGroup="element_group">
    <xs:element name="P" type="xs:string" substitutionGroup="element_group">
    <xs:element name="Q" type="xs:string" substitutionGroup="element_group">

</xs:schema>

Requirement is:

  1. All element from A to N are appearing in any order.
  2. Element P,Q and R is also part of data but only one element appear from among 3. Order is also any.
  3. More important I have cover 1st and 2nd point but I want one more restriction is that only and only four element will be become the part of <data> that means element count from <A> to <Q> is exact four,

Combination can any of them from <A> to <Q> but final count is only four, please help me.

Now currently i am unable to set maxOccures in <all> , it not compiling the xsd after setting maxOccures.

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

Решение

The simplest way to handle this is probably to use XSD 1.1 and use an assertion on the parent to specify that there must be exactly (or at most) four children. You will also need to make each child of the all-group optional, since thirteen of them will not appear.

The best way might be to redesign your XML to work better with your schema language instead of fighting it. It's hard to give advice on that, though, since your example is abstract enough to make it unclear why you are imposing the requirements you mention.

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