在XSD 1.1 中,您可以使用断言
<xs:element name="FirstName" nillable="true" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="28"/>
<xs:whiteSpace value="collapse"/>
<xs:assertion test="not(matches($value, '\d{4}'))"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
但是你甚至可以在 XSD 1.0 中使用xs:pattern
:
<xs:element name="FirstName" nillable="true" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="28"/>
<xs:whiteSpace value="collapse"/>
<xs:pattern value="\d{0,3}(\D+\d{0,3})*|(\d{0,3}\D+)+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
或者,如果您愿意,可以将模式分开:
<xs:element name="FirstName" nillable="true" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="28"/>
<xs:whiteSpace value="collapse"/>
<!-- Matches every strings (optionally starting with 0 to 3 digits) and optionally followed by [(non digits) + (0 to 3 digits)] n times -->
<xs:pattern value="\d{0,3}(\D+\d{0,3})*"/>
<!-- Matches every strings ending with a non-digit and not containing more than 3 continuous digits -->
<xs:pattern value="(\d{0,3}\D+)+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>