Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSD pattern to match a priority order of comma separated words

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:

  1. List item any of {foo,bar,goo,doo,woo} maximum once
  2. 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.

like image 851
Rickard von Essen Avatar asked Oct 25 '22 23:10

Rickard von Essen


1 Answers

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.

like image 103
ZZ Coder Avatar answered Oct 28 '22 12:10

ZZ Coder