0

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.

2 Answers 2

3

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>
2
  • Unfortunately I don't have a processor that understands 1.1 (oops! Thought I did.) Also NMTOKEN is too restrictive regarding start-character. I think I would have to make an itemList type with the xs:list, and then another itemSet type with the xs:assert restriction, correct? Jan 9, 2013 at 10:22
  • Yes, of course you can substitute your own user-defined list type for xs:NMTOKENS in this example. Jan 9, 2013 at 10:53
1

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.

2
  • Requiring a specific order is too restrictive for my particular case, sorry, but it might help other people to know about this option. Jan 9, 2013 at 10:23
  • This solution relies on the list being constrained to a small enumeration of strings - which was true of the example given, but not of the general problem. Jan 9, 2013 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.