xml schema check that restriction enumeration value only occrus once
I am creating a xsd schema开发者_运维知识库 for validation of some xml
I would like to restrict the xml so it's not posible to input the same item twice:
<branches>
<branche>Bank</branche>
<branche>Bank</branche>
</branches>
But it must be posible to use 2 different items:
<branches>
<branche>Bank</branche>
<branche>Insurance</branche>
</branches>
So i have the following code:
<!-- definition of simple elements -->
<xs:simpleType name="branche">
<xs:restriction base="xs:string">
<xs:enumeration value="Bank" maxOccurs="1"/>
<xs:enumeration value="Insurance" maxOccurs="1"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="branches" minOccurs="0"> <!-- minOccurs becouse i want it to be posible to leave out the whole <branches> tag -->
<xs:complexType>
<xs:sequence>
<xs:element name="branche" type="branche" minOccurs="0" maxOccurs="2" />
</xs:sequence>
</xs:complexType>
</xs:element>
using the maxOccurs="1"
does not restrict it to only one value because the 'branche' tag can occur twice.
I want the value (<branche>value</branche>
) to be unique.
thnx!
See examples on identity constraints here. Something like:
<xs:element name="branches" ...>
<xs:unique name="...">
<xs:selector xpath="branche"/>
<xs:field xpath="."/>
</xs:key>
</xs:element>
Not quite sure about the syntax, but you get the idea.
Fixed it by using the following code:
<xs:element name="branches" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="branche" type="branche" minOccurs="0" maxOccurs="2" />
</xs:sequence>
</xs:complexType>
<xs:unique name="brancheUnique">
<xs:selector xpath="branche"/>
<xs:field xpath="."/>
</xs:unique>
</xs:element>
thnx lexicore for pointing me in the right direction
精彩评论