validate pipe-separated string in XSD

840 views Asked by At

I have a use case where I need to validate pipe-separated string in XSD attribute value.

Example: XML Attribute

<Fruits Names="Apple|Grapes|Banana">

I want to write a XSD pattern where Fruits attribute Name allows following and other valid combination from above 3 values.

Apple
Banana
Grapes
Apple|Banana
Grapes|Banana
Apple|Grapes
Banana|Grapes
Grapes|apple
Apple|Grapes|Banana

I currently wrote something like

    <xs:simpleType name="Fruits">
        <xs:restriction base="xs:string">
            <xs:pattern value="Apple*|Grapes*|Banana" ></xs:pattern>
        </xs:restriction>
    </xs:simpleType>

I want to use this in C#, so I guess I can only use XSD 1.0.

1

There are 1 answers

0
kjhughes On BEST ANSWER

I'd recommend that you give up the | separator and use space () instead.

I.e:

<Fruits Names="Apple Grapes Banana"/>

Then, the following XSD will meet your requirements:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Fruits">
    <xs:complexType>
      <xs:attribute name="Names">
        <xs:simpleType>
          <xs:list itemType="FruitTypes"/>
        </xs:simpleType>
      </xs:attribute>
    </xs:complexType>
  </xs:element>
  <xs:simpleType name="FruitTypes">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Apple"/>
      <xs:enumeration value="Grapes"/>
      <xs:enumeration value="BAnana"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>