I'm trying to make an XSD that specifies that an <a>
element must have 4 child <b>
elements, which contain the c
attributes 1 through 4, as follows:
Valid:
<a>
<b c="1" d="valueof1" />
<b c="2" d="valueof2" />
<b c="3" d="valueof3" />
<b c="4" d="valueof4" />
</a>
Not valid:
&开发者_C百科lt;a>
<b c="1" d="valueof1" />
<b c="1" d="valueof1_other" />
<b c="3" d="valueof3" />
<b c="4" d="valueof4" />
</a>
Not valid:
<a>
<b c="1" d="valueof1" />
<b c="2" d="valueof2" />
<b c="3" d="valueof3" />
<b c="4" d="valueof4" />
<b c="5" d="valueof5" />
</a>
Is this possible? The closest thing I could find is the all element, but that only appears to work for specifying unique elements, not attribute values.
You can enforce the first rule by declaring element b
with minOccurs=4
and maxOccurs=4
, like so:
<xs:element name="a">
<xs:complexType>
<xs:sequence>
<xs:element name="b" minOccurs="4" maxOccurs="4"/>
</xs:sequence>
</xs:complexType>
</xs:element>
However, you can not use XSD to enforce the second rule. If you really want to do that, you can, for example, replace the 4 b
elements with elements b1
, b2
, b3
, and b4
, each of which would implicitly represent the c
attributes 1 through 4, respectively.
精彩评论