XML schema restriction pattern for not allowing specific string

OK, the OP has persuaded me that while the other question mentioned has an overlapping topic, the fact that the forbidden string is forbidden at all locations, not just as a prefix, complicates things enough to require a separate answer, at least for the XSD 1.0 case. (I started to add this answer as an addendum to my answer to the other question, and it grew too large.)

There are two approaches one can use here.

First, in XSD 1.1, a simple assertion of the form

not(matches($v, 'FILENAME'))

ought to do the job.

Second, if one is forced to work with an XSD 1.0 processor, one needs a pattern that will match all and only strings that don’t contain the forbidden substring (here ‘FILENAME’).

One way to do this is to ensure that the character ‘F’ never occurs in the input. That’s too drastic, but it does do the job: strings not containing the first character of the forbidden string do not contain the forbidden string.

But what of strings that do contain an occurrence of ‘F’? They are fine, as long as no ‘F’ is followed by the string ‘ILENAME’.

Putting that last point more abstractly, we can say that any acceptable string (any string that doesn’t contain the string ‘FILENAME’) can be divided into two parts:

  1. a prefix which contains no occurrences of the character ‘F’
  2. zero or more occurrences of ‘F’ followed by a string that doesn’t match ‘ILENAME’ and doesn’t contain any ‘F’.

The prefix is easy to match: [^F]*.

The strings that start with F but don’t match ‘FILENAME’ are a bit more complicated; just as we don’t want to outlaw all occurrences of ‘F’, we also don’t want to outlaw ‘FI’, ‘FIL’, etc. — but each occurrence of such a dangerous string must be followed either by the end of the string, or by a letter that doesn’t match the next letter of the forbidden string, or by another ‘F’ which begins another region we need to test. So for each proper prefix of the forbidden string, we create a regular expression of the form

$prefix || '([^F' || next-character-in-forbidden-string || ']' 
    || '[^F]*'

Then we join all of those regular expressions with or-bars.

The end result in this case is something like the following (I have inserted newlines here and there, to make it easier to read; before use, they will need to be taken back out):

[^F]*
((F([^FI][^F]*)?)
|(FI([^FL][^F]*)?)
|(FIL([^FE][^F]*)?)
|(FILE([^FN][^F]*)?)
|(FILEN([^FA][^F]*)?)
|(FILENA([^FM][^F]*)?)
|(FILENAM([^FE][^F]*)?))*

Two points to bear in mind:

  • XSD regular expressions are implicitly anchored; testing this with a non-anchored regular expression evaluator will not produce the correct results.
  • It may not be obvious at first why the alternatives in the choice all end with [^F]* instead of .*. Thinking about the string ‘FEEFIFILENAME’ may help. We have to check every occurrence of ‘F’ to make sure it’s not followed by ‘ILENAME’.

Leave a Comment