An XML schema constraint that allows empty elements or a specific template

I want to define an element in an XML schema that will contain an empty string or some specific template, for example:

<Code/> <Code></Code> <Code> </Code> <Code>11111</Code> <Code>111111</Code> - INVALID <Code>AAAAA</Code> - INVALID 

How can I change an existing restriction?

 <xs:element name="Code"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[0-9]{5}" /> </xs:restriction> </xs:simpleType> </xs:element> 
+4
source share
2 answers

Add \s as another choice for your regular expression to allow whitespace [# x20 \ t \ n \ r] (That is: "regular" space, tab, feed, carriage return. Is included.)

 <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="\s*|[0-9]{5}" /> </xs:restriction> </xs:simpleType> 
+5
source

Use "^$|pattern" it should work, since ^$ matches only null values.

0
source

All Articles