jlp
jlp

Reputation: 10358

XML schema restriction pattern for not allowing empty strings

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].*" />
</xsd:restriction>

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

Upvotes: 13

Views: 25623

Answers (4)

Frederik
Frederik

Reputation: 81

doesn't this do exactly what you want?

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

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)

Upvotes: 8

maurizeio
maurizeio

Reputation: 286

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>
    </xs:restriction>
</xs:simpleType>

Using Eclipse, seems to work fine.

Upvotes: 6

M. Atif Riaz
M. Atif Riaz

Reputation: 502

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

Upvotes: 1

Richard
Richard

Reputation: 109080

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.

Upvotes: 7

Related Questions