Note that there are some explanatory texts on larger screens.

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

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload