J. Jordan
J. Jordan

Reputation: 13

XSD to validate a list of 1 to 4 specific characters

I would like to validate that an attribute value in my XML contains at least one character and up to 4 characters in a set. The values I'm looking for are "T", "C", "S" and "R". They can be in any order and combination, must be at least one of these characters and may be all four of them.

I've created a long list with all of the possible combinations, below, using enumerations. I would like to simplify this with some pattern checking.

<xs:attribute name="valid-types" use="required">
  <xs:simpleType>
    <xs:restriction base="xs:string">   
        <xs:enumeration value="T"/>
        <xs:enumeration value="S"/>
        <xs:enumeration value="C"/>
        <xs:enumeration value="R"/>
        <xs:enumeration value="TS"/>
        <xs:enumeration value="TC"/>
        <xs:enumeration value="TR"/>
        <!-- and so on  -->
    </xs:restriction>
  </xs:simpleType>
</xs:attribute>

In the end, acceptable XML values would be "T", or "TC" or "TSC" or "TSCR" or any other combination of TSCR.

Upvotes: 1

Views: 208

Answers (1)

kjhughes
kjhughes

Reputation: 111601

Just use xs:pattern with a regex:

<xs:attribute name="valid-types" use="required">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:pattern value="[TCSR]{1,4}"/>
    </xs:restriction>
  </xs:simpleType>
</xs:attribute>

Upvotes: 2

Related Questions