I want my XSD to validate the contents of a string. To be specific, I want to validate that a certain string does not occur.
Consider this rule, which will verify that my string occurs. Looking for all Link
elements starts with this particular string: /site/example.com
<xs:element name="Link" type="xs:normalizedString" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:token">
<xs:pattern value="(/site/example\.com).*"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
In other words, the expression above verifies that all Link
elements start with /site/example.com
. How do you invert the expression above, so that it **verifies that no Link
elements start with /site/example.com
?
I tried the following regexp with no luck: /[^(site/example\.com)].*
, so this is not working:
Not-working strategy 1 (negation of single character) I am aware that this probably would work for negating a single character, since this SO question does that: XML schema restriction pattern for not allowing empty strings
The suggested pattern in that question <xs:pattern value=".*[^\s].*" />
But negating only a single character does not work in this case, since it would correctly fail:
/site/example.com
but also it would incorrectly fail
/solutions
Not-working Strategy 2 (advanced regexp lookahead)
According to this SO question ( Regular expression to match a line that doesn't contain a word? ), you could solve this with negative lookahead (?!expr)
.
So this will work in ordinary regexp:
^((?!/site/example.com).)*$
Now, unfortunately xsd validations support only limited regexps. According to this site, no lookaheads are supported: regular-expressions.info -- xsd
This pretty much describes what i have tried until now.
My question is, how do i negate a regular expression in an XSD schema?