连续数字的XSD验证

时间:2018-02-08 01:07:44

标签: xsd-validation xsd-1.1

我有下面的元素,我不希望在字符串中的任何位置继续超过3位数。我怎么能用图案或任何其他方式做到这一点。感谢。

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

... | Export-Csv \\server\share($strTimestamp + "_" + $group + ".csv") -NoType

1 个答案:

答案 0 :(得分: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>

但即使在使用xs:pattern 的XSD 1.0中,您也可以执行此操作:

<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>