Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML schema restriction pattern for not allowing empty strings

Tags:

regex

xsd

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?

like image 601
jlp Avatar asked May 16 '11 07:05

jlp


People also ask

Which types can be used to restrict the length of string XML?

To limit the length of a value in an element, we would use the length, maxLength, and minLength constraints.

Which defines the type of XML data and restrictions?

An XSD defines the structure of an XML document. It specifies the elements and attributes that can appear in an XML document and the type of data these elements and attributes can contain. This information is used to verify that each element or attribute in an XML document adheres to its description.

What is enumeration in XML?

Enumerations are a base simple type in the XSD specification containing a list of possible values. Single-valued enumerations are shown as restrictions of the base simple type xs:token , as illustrated below: ? < xs:simpleType name=”GraduationPlanTypeMapType”>


2 Answers

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)

like image 193
Frederik Avatar answered Sep 28 '22 11:09

Frederik


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.
like image 29
Richard Avatar answered Sep 28 '22 09:09

Richard