Question

I had xsd as follows

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="College">
    <xs:complexType>
      <xs:sequence>
          <xs:element type="xs:string" name="Name" maxOccurs="unbounded" minOccurs="1"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

It must allow only 2 values Example Only ABC and CBS are allowed

<college>
<Name>ABC</Name>
<Name>ABC</Name>
<Name>CBS</Name>
</college>

which should not allowed

<college>
<Name>ABC</Name>
<Name>ABC</Name>
<Name>CBS</Name>
<Name>XYZ</Name>
</college>
Was it helpful?

Solution

Since you have a countable number of options you can use an enumeration to restrict them. Replace your xs:element declaration with this:

<xs:element name="Name" maxOccurs="unbounded" minOccurs="1">
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:enumeration value="ABC"/>
            <xs:enumeration value="CBS"/>
        </xs:restriction>
    </xs:simpleType>
</xs:element>

OTHER TIPS

Try pattern matching

<xsd:element name="elementName">
    <xsd:simpleType>
        <xsd:restriction base="xsd:string">
            <xsd:pattern value="/^[ABC|CBS]$/"></xsd:pattern>
        </xsd:restriction>
    </xsd:simpleType>
</xsd:element>   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top