XSD pattern to match a priority order of comma separated words

design-patterns, xsd

Solution

I don't think you can express all the rules in a regular expression. Especially, it will be tough to enforce "maximum once". This is the closest I come up with,

<xs:simpleType name="order">
    <xs:annotation>
      <xs:documentation>
      Comma-separated list of anything
      </xs:documentation>
    </xs:annotation>
    <xs:restriction base="xs:string">
      <xs:pattern value="[^,]+(,\s*[^,]+)*"/>
    </xs:restriction>
</xs:simpleType>

You might want try to use space as separator. That's more common in XML files. XML Schema has a builtin type "`list`" defined for space-separated list.

Problem

I have a tag like this ``` <order>foo,bar,goo,doo,woo</order> ``` that I need to validate with an xsd. How do I write a regexp pattern that matches the string that contains: - List item any of {foo,bar,goo,doo,woo} maximum once - or is empty. Valid examples: ``` <order>foo,bar,goo,doo,woo</order> <order>foo,bar,goo</order> <order>foo,doo,goo,woo</order> <order>woo,foo,goo,doo,bar</order> <order></order> ``` Invalid: ``` <order>foo,foo</order> <order>,</order> <order>fo</order> <order>foobar</order> ``` This have to work in different XML/XSD parsers.

Original source