Reputation: 10358
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
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
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
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
Reference: W3C whiteSpace
Upvotes: 1
Reputation: 109080
The pattern:
.*
(.
matches any character).\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.Upvotes: 7