1

我有以下元素,我不想在字符串中的任何位置延续超过 3 位数字。我怎样才能使用模式或任何其他方式做到这一点。谢谢。

例如 John - 有效,123 John - 有效,123John - 有效,1230John - 无效,Jo1284hn - 无效,John1734 - 无效

<xs:element name="FirstName" nillable="true" minOccurs="0">
<xs:simpleType>
    <xs:restriction base="xs:string">           
        <xs:maxLength value="28"/>
        <xs:whiteSpace value="collapse"/>
    </xs:restriction>
</xs:simpleType>

4

1 回答 1

1

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>
于 2018-02-08T22:20:26.357 回答