2012年7月25日 星期三

在xsd complexType 加入 simpleType 做條件限制

由於之前寫了一個WebService method 名為searchActiveDepts,我一開始預設是使用String[],由於 java已預設會將String的xsd(XML Schema) maxOccurs 設為 unbound,若企圖改寫xsd並加入simpleType的條件限制,以下為成功的範例碼。


<xs:complexType name="searchActiveDepts">
<xs:sequence>
<xs:element name="keywords" minOccurs="0" maxOccurs="3">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:maxLength value="10"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>
<xs:element name="account" type="tns:userAccount" minOccurs="0"/>
</xs:sequence>
</xs:complexType>


若直接用瀏覽器查看xsd,亦會看到如下

-<xs:complexType name="searchActiveDepts">
  -<xs:sequence>
    -<xs:element name="keywords" minOccurs="0" maxOccurs="3">
      -<xs:simpleType>
        -<xs:restriction base="xs:string">
          <xs:maxLength value="10"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
    <xs:element name="account" type="tns:userAccount" minOccurs="0"/>
  </xs:sequence>
</xs:complexType>

關鍵在於在尚未加入simpleType時,本來是
<xs:element name="keywords" type="xs:string" minOccurs="0" maxOccurs="unbound">

而我為了加入simpleType必須將紅色的部分拿掉
且我的目的是 使用此element(keyword)最多只能一次使用3組,所以設定maxOccurs="3"

並且利用simpleType,將每組keyword的長度不可超出10 (ex:"123456",長度為6)


因此現在我的關鍵字查詢功能 可以設定成至多同時輸入 3組查詢,且每組不超過10的長度


另外 simpleType可以加入許多不同的條件限制,詳見
http://www.w3schools.com/schema/schema_facets.asp





沒有留言:

張貼留言