Take the 2-minute tour ×

In my XML schema I have element of type string that I don't want to be empty (if it contains white-spaces etc I also consider it empty)

I applied restrinction I found at http://blogs.msdn.com/b/neerajag/archive/2005/08/12/450723.aspx

<xsd:restriction base = "xsd:string">
  <xs:minLength value="1" />
  <xs:pattern value=".*[^\s].*" />

What exactly does that pattern do and will do what I expect?

share|improve this question

4 Answers 4

The pattern:

  • zero or more characters .* (. matches any character).
  • matches something not in the listed set of characters. \s is whitespace, so [^\s] is "match something that isn't a whitespace. The initial ^ in the match negates the normal match any one of these characters.
  • zero or more characters.
share|improve this answer
It rejects text with carriage returns char. Why? –  jlp May 26 '11 at 10:25
@jlp: ". matches any character" is the quick version. The full version includes a note that depending on the regex engine it might not match newlines by default. Try replacing .* with [.\s]* as \s matches includes both newline and carriage return. –  Richard May 27 '11 at 7:58
Could you please write pattern that meets requirements from question? –  jlp May 27 '11 at 15:25
@jlp: What requirement? The question is "what does this pattern mean?". –  Richard May 27 '11 at 15:29
string that is not empty (only whitespaces and newlines means is empty too) –  jlp Oct 13 '11 at 8:51

doesn't this do exactly what you want?

 <xs:restriction base="xs:token">
  <xs:minLength value="1"/>

If the string contains only whitespace (line feeds, carriage returns, tabs, leading and trailing spaces), the processor will remove them so validation will fail; if there's anything else, validation will succeed. (note though: internal sequences of two or more spaces will be removed - make sure you're ok with that)

share|improve this answer
Works! Much more readable than regular expressions! –  Rudolf Adamkovic Aug 2 at 15:24

I don't know if still useful but I found a better pattern than the first posted. Here it is:

<xs:simpleType name="nonEmptyString">
    <xs:restriction base="xs:string">
        <xs:pattern value="(\s*[^\s]\s*)+"></xs:pattern>

Using Eclipse, seems to work fine.

share|improve this answer
I just wrote 5 unit tests testing a) empty string b) single space c) d) multiple spaces containing a new line e) multiple spaces containing multiple new lines. It works for all those cases, so I would suggest that this is a good solution to anyone needing it! :) well done. –  Tod Thomson Sep 12 '13 at 23:28
This is the only solution out of all the answers that works for my use case +1 –  Chris L Oct 1 at 14:17

Looking at subject of the post "pattern for not allowing empty strings" which is still unanswered. You can do that using <xsd:whiteSpace value="collapse" /> tag to disallow spaces

whiteSpace constraint set to "collapse", it will do the following

  1. removes all white space characters including line feeds, tabs, spaces, carriage returns
  2. leading and trailing spaces are removed
  3. multiple spaces are reduced to a single space

Reference: W3C whiteSpace

share|improve this answer
Your description of the 'collapse' value seems incorrect; especially number 1. The only part of number 1 that seems correct is the part that is duplicated in number 2. But considering your source (w3schools), I can see why you might be confused; W3Schools is a pretty low-quality source for many things. (see w3fools.com) –  Andrew Barber Dec 3 '12 at 12:59
@Andrew Thanks for pointing. I have also referred to W3C for reference W3C whiteSpace - w3.org/TR/xmlschema-2/#rf-whiteSpace Point 2,3 seems to be perfectly correct. Although no mentioning of point 1 but it is also working Updated the Reference –  M. Atif Riaz Dec 4 '12 at 7:35
Excellent update on the reference. I'm still a tiny bit not sure on #1 (#2, 3 are definitely correct, as you note). On #1, does it not convert those types of whitespace to spaces, rather than simply removing them? (and then, subsequently, collapses all spaces to a single space...) –  Andrew Barber Dec 4 '12 at 14:33

Your Answer


