这是一个摆动'Match a string of characters between 0 and 100 long with no more than 1 hyphen in it'
加上一些额外的限制:
考虑到 XSD 正则表达式支持的语法,我认为您不能在模式中完成最大长度;但是,很容易将它与 maxLength 方面结合起来。
这是一个 XSD:
<?xml version="1.0" encoding="utf-8" ?>
<!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)-->
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="last-name">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="100"/>
<xsd:pattern value="[a-zA-Z ]+\-?[a-zA-Z ]+"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:schema>
该模式可以进一步细化以禁止仅由空格包围的连字符等。
有效的 XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<last-name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">last - name</last-name>
无效的 XML(连字符太多)和消息:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<last-name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">l-ast - name</last-name>
验证错误:
Error occurred while loading [], line 3 position 121
The 'http://tempuri.org/XMLSchema.xsd:last-name' element is invalid - The value 'l-ast - name' is invalid according to its datatype 'String' - The Pattern constraint failed.
无效的 XML(长于最大值,对于我使用 maxLength=14 的测试)和消息:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<last-name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">last - name that is longer</last-name>
验证错误:
Error occurred while loading [], line 3 position 135
The 'http://tempuri.org/XMLSchema.xsd:last-name' element is invalid - The value 'last - name that is longer' is invalid according to its datatype 'String' - The actual length is greater than the MaxLength value.