Search code examples
xmlxsdxsd-validation

Restriction of xsd:list with enumeration


is there a way to restrict xsd:list with enumerated strings? In my example I want a b (or a a b, b a, ..) to be valid, but not a b c. However, a b also seems not to be valid.

Xsd schema:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    
    <xsd:element name="example">
        <xsd:complexType>
            <xsd:all>
                <xsd:element name="stringList" type="restrictedStringList"/>
            </xsd:all>
        </xsd:complexType>
    </xsd:element>

    <xsd:simpleType name="restrictedStringList">
        <xsd:restriction base="stringListType">
            <xsd:enumeration value="a"/>
            <xsd:enumeration value="b"/>
        </xsd:restriction>
    </xsd:simpleType>

    <xsd:simpleType name="stringListType">
        <xsd:list itemType="xsd:string"/>
    </xsd:simpleType>

</xsd:schema>

XML instance:

<?xml version="1.0" encoding="UTF-8"?>
<example xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:noNamespaceSchemaLocation="example.xsd">
    <stringList>a b</stringList>
</example>

The above example fails to validate:

Value 'a b' is not in the enumeration list. It must be one of the following:

  • a
  • b

Solution

  • I would think you need e.g.

    <xsd:simpleType name="enumerationList">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="a"/>
            <xsd:enumeration value="b"/>
        </xsd:restriction>
    </xsd:simpleType>
    
    
    <xsd:simpleType name="stringListType">
        <xsd:list itemType="enumerationList"/>
    </xsd:simpleType>
    
    <xsd:element name="stringList" type="stringListType"/>