问题描述
|
我想在XML模式中定义一个元素,该元素将允许使用空字符串或某些特定模式,例如:
<Code/>
<Code></Code>
<Code> </Code>
<Code>11111</Code>
<Code>111111</Code> - INVALID
<Code>AAAAA</Code> - INVALID
如何修改现有限制?
<xs:element name=\"Code\">
<xs:simpleType>
<xs:restriction base=\"xs:string\">
<xs:pattern value=\"[0-9]{5}\" />
</xs:restriction>
</xs:simpleType>
</xs:element>
解决方法
在您的正则表达式中添加“ 2”作为另一个选择,以允许使用空格字符[#x20 \\ t \\ n \\ r](即:\“ regular \”空格,制表符,换行符,回车符。不间断空格不是包括在内。)
<xs:simpleType>
<xs:restriction base=\"xs:string\">
<xs:pattern value=\"\\s*|[0-9]{5}\" />
</xs:restriction>
</xs:simpleType>
,使用\"^$|pattern\"
,因为^$
仅匹配空值。