tags:

views:

103

answers:

2

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.

A: 

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.

ZZ Coder
This is pretty much what I went with. It's a pity they didn't use a sub tag for each item in the list, but this interface was already implemented in a embedded device.
Rickard von Essen
A: 

I can write a schema file if they follow some sequence .. like {foo,bar,goo,doo,woo}
but in your case, you say that, they can appear in ANY SEQUENCE..
so (5P5+5P4+5P3+5P2+5P1+1) = 326 patterns .. !!!

If there was some sequence as I mentioned .. then the number of patterns would have been .. 32 .. bearable..

infant programmer
yes you are right .. :)
infant programmer