2013-01-11 95 views
15

我想我已經搜索了很多關於這個,但仍然沒有去。XSD屬性限制

將不勝感激任何幫助。

我想限制空元素的屬性。 「顏色」應該有一個限制,只能保持3位或minLength = 3和maxLength = 3。它不應該有任何內容。

<?xml version="1.0" encoding="utf-8"?> 
    <items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation=""> 
    <product id="" name=""> 
    <article id="1001"> 
     <umbrella color="100"/> 
     <umbrella color="101"/> 
    </article> 
    <article id="1002"> 
     <umbrella color="110"/> 
    </article> 
    </product> 
</items> 

編輯:我知道如何做一個simpleType的XSD限制。但我不知道如何將它與ComplexType結合到一個實體中。

如果您可以提供更詳細的(或全部)解決方案,我會很高興。

順便說一句,「顏色」不限於xs:整數。它實際上是一個xs:字符串。

回答

23

您可以定義與以下類似的屬性。此示例使用模式來限制該值,但如果更合適,也可以使用min和max。

<xs:attribute name="color"> 
    <xs:simpleType> 
     <xs:restriction base="xs:integer"> 
      <xs:pattern value="[0-9][0-9][0-9]"/> 
     </xs:restriction> 
    </xs:simpleType> 
</xs:attribute> 

然後在你的元素定義,你只需要使用一個ref引用定義的屬性:

<xs:attribute ref="color"/> 

UPDATE(響應從OP評論):

這裏是整個模式可能看起來像:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

    <xs:attribute name="color"> 
     <xs:simpleType> 
      <xs:restriction base="xs:integer"> 
       <xs:pattern value="[0-9][0-9][0-9]"/> 
      </xs:restriction> 
     </xs:simpleType> 
    </xs:attribute> 

    <xs:attribute name="id"> 
     <xs:simpleType> 
      <xs:restriction base="xs:integer"> 
       <xs:pattern value="[0-9][0-9][0-9][0-9]"/> 
      </xs:restriction> 
     </xs:simpleType> 
    </xs:attribute> 

    <xs:attribute name="name" type="xs:string"/> 

    <xs:complexType name="article_type"> 
     <xs:attribute ref="color" use="required"/> 
    </xs:complexType> 

    <xs:element name="article"> 
     <xs:complexType> 
      <xs:choice maxOccurs="unbounded" minOccurs="0"> 
       <xs:element name="umbrella" type="article_type"/> 
      </xs:choice> 
      <xs:attribute ref="id" use="required"/> 
     </xs:complexType> 
    </xs:element> 

    <xs:element name="product"> 
     <xs:complexType> 
      <xs:choice maxOccurs="unbounded" minOccurs="0"> 
       <xs:element ref="article"/> 
      </xs:choice> 
      <xs:attribute ref="id" use="required"/> 
      <xs:attribute ref="name"/> 
     </xs:complexType> 
    </xs:element> 

    <xs:element name="items"> 
     <xs:complexType> 
      <xs:choice maxOccurs="unbounded" minOccurs="0"> 
       <xs:element ref="product"/> 
      </xs:choice> 
     </xs:complexType> 
    </xs:element> 

</xs:schema> 
+1

點點短:'[0-9] {3}' – 13ren

+0

謝謝您的輸入。上面我的編輯。我知道如何做一個xs:限制,但我不知道如何將它們合併爲一個整體。請爲我的示例提供更多或完整的內容。如果我理解正確,則帶有SimpleType屬性的複雜類型帶有限制。 – ZiggyStardust

+0

@ZiggyStardust查看更新。 – David

1

以下應該工作

<element name="umbrella" nillable="true" type="umbrellaType"> 

<complexType name="umbrellaType"> 
    <attribute name="color"> 
    <simpleType> 
     <restriction base="int"> 
     <minExclusive value="99"></minExclusive> 
     <maxInclusive value="999"></maxInclusive> 
     </restriction> 
    </simpleType> 
    </attribute> 
</complexType> 
+0

感謝您的意見。上面我的編輯。我知道如何做一個xs:限制,但我不知道如何將它們合併爲一個整體。請爲我的示例提供更多或完整的內容。如果我理解正確,則帶有SimpleType屬性的複雜類型帶有限制。 – ZiggyStardust