2012-03-28 62 views
1

我不知道如何做到这一点,但我正在处理XSD的数据类型,我试图做的事情之一是扩展它以允许以姓氏连字符。所以这应该匹配SmithFrySafran-Foer。此外,我想限制被检查字符串的长度不超过100(或101)个字符。我正则表达式的渊源是:在RegEx/XSD中匹配较长的有限字符串中的一个字符

<xsd:pattern value="[a-zA-Z ]{0,100}"/> 

现在我知道我能做到,我打破这个并随意允许50个字符像两侧的东西:

<xsd:pattern value="[a-zA-Z ]{0,50}(\-)?[a-zA-Z ]{0,50}"/> 

,但似乎不适度。有没有办法做到的线沿线的东西:

<xsd:pattern value="[a-zA-Z (\-)?]{0,100}"/> 

问什么,我正在寻找的是“匹配0和100之间的长字符的字符串,在它不超过1个连字符的另一种方式”。

谢谢!

+1

为了增加伤害,'''失败当hyph en位于记录的字符51上。 – 2012-03-28 01:17:13

回答

2

这是在'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(大于最大更长,测试我使用最大长度= 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.

相关问题