4

I have a XML Schema with a regex restriction that matches three different options and looks similar to this:

somestring|someotherstring|
  1. Some string, or
  2. Some other string, or
  3. Empty string

My problem is that xmllint does not like this regex. It gives the following error:

regexp error : failed to compile: expecting a branch after |

I am forced to comply with xmllint because of the environment.

My question is, what are other options to match empty string using regex in a XSD?

As I understand, negative look ahead is not available in a XSD regex, and I am at a loss for other options.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
d.breve
  • 127
  • 1
  • 12

2 Answers2

6

You may use an optional group:

(somestring|someotherstring)?
^                          ^^ 

It will match these 2 words or an empty string. In XSD, all patterns are anchored automatically, so, no additional operators are necessary.

See what the regex matches.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
3

I think Wiktor's answer is fine (+1), but you asked for more answers, so here are some alternatives:

Regex Solutions

Works with Xerces-J but not xmllint:

|somestring|someotherstring
somestring||someotherstring
somestring|someotherstring|

Works with Xerces-J and xmllint:

(somestring|someotherstring)?    <!-- Wiktor's answer -->
()|somestring|someotherstring
somestring|()|someotherstring
somestring|someotherstring|()

Non-regex Solution

Works with Xerces-J and xmllint:

 <xs:simpleType>
   <xs:restriction base="xs:string">
     <xs:enumeration value=""/>
     <xs:enumeration value="somestring"/>
     <xs:enumeration value="someotherstring"/>
   </xs:restriction>
 </xs:simpleType>
kjhughes
  • 106,133
  • 27
  • 181
  • 240