Reputation: 31621
Is it possible express in XML Schema a simple type which is a list and which has the further restriction that each item in the list may only appear once? Said differently, is it possible to define a "set" simple type in XML Schema?
For example, using the following schema:
<xs:schema version="1.1" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="item">
<xs:restriction base="xs:token">
<xs:enumeration value="A"/>
<xs:enumeration value="B"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="itemSet">
<xs:list itemType="item"/>
</xs:simpleType>
<xs:element name="root">
<xs:complexType>
<xs:attribute name="set" type="itemSet"/>
</xs:complexType>
</xs:element>
</xs:schema>
This document validates:
<root set="A B"/>
However, so does this document, which I would like to fail validation because B
is repeated:
<root set="A B B"/>
I suspect this is not possible with XML Schema alone, but I don't know it very well so I am looking for confirmation.
Upvotes: 0
Views: 1934
Reputation: 839
It's possible through regular expression. It could be much easier if we enforce the attribute value to be ordered. If not we probably have to create 'N' pattern expression with all combination
<xs:simpleType name="itemSet1">
<xs:restriction base="itemSet">
<xs:pattern value="(A\s?)?(B\s?)?(C\s?)?"/>
</xs:restriction>
<xs:element name="root">
<xs:complexType>
<xs:attribute name="set" type="itemSet1">
</xs:attribute>
</xs:complexType>
</xs:element>
Now
<root set="A B A"/>
is invalid and so is
<root set="B A"/>
If you want the above one to be also valid we need to add another pattern to the restriction with this order.
Upvotes: 1
Reputation: 163262
It's not possible in XSD 1.0, but it's very easy in XSD 1.1:
<xs:simpleType name="uniqueList">
<xs:restriction base="xs:NMTOKENS">
<xs:assert test="count($value) = count(distinct-values($value))"/>
</xs:restriction>
</xs:simpleType>
Upvotes: 3