Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML schema restriction that allows empty elements or specific pattern

Tags:

xsd

I want to define an element in XML schema that will allow for an empty string or some specific pattern, e.g.,:

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

How can I modify my 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>
like image 473
jlp Avatar asked May 25 '11 13:05

jlp


Video Answer


1 Answers

Add \s as another choice to your regex to allow whitespace characters [#x20\t\n\r] (That is: "regular" space, tab, line feed, carriage return. Non-breaking space is not included.)

<xs:simpleType>
    <xs:restriction base="xs:string"> 
        <xs:pattern value="\s*|[0-9]{5}" />
    </xs:restriction>
</xs:simpleType>
like image 136
jasso Avatar answered Sep 28 '22 04:09

jasso