2012-03-28 48 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.

相關問題