2010-01-26 99 views
0

只是要簡明扼要我想獲得這樣的事:如何在XML Schema中定義具有相同名稱但不同類型的元素序列?

<root> 
    <field value="..." text="...">fixed_value1</field> 
    <field value="..." text="...">fixed_value2</field> 
    <field value="..." text="...">fixed_value3</field> 
    <!-- in some cases we can choose the «fixed_value» among different ones --> 
    ... 
    <field value="..." text="...">fixed_valueN</field> 
</root> 

我嘗試不同的方法,但它似乎很不可能才達到,因爲XML模式不允許設置的元素列表名稱相同,但不同的類型(簡單或複雜並不重要...)。這樣對嗎?沒有其他方法來定義像上面那樣的結構?

編輯:也許我必須解釋它好一點。在元素«field»的打開和關閉標記之間必須有一個由XML Schema定義的值(換句話說,對於用戶來說,不可能寫入與fixed_value不同的東西)。又如:

<root> 
    <field value="Ferrari" text="company">Car</field> 
    <!-- but it could be Van or Motorcycle or Plane --> 
    <field value="12300000" text="euro">Cost</field> 
    <!-- here instead it's only possible to choose «Cost» --> 
    <field value="Red" text="">Color</field> 
    <!-- same as above --> 
</root> 

這可能嗎?提前致謝!

回答

1

請嘗試以下XSD:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema id="root" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="root"> 
    <xs:complexType> 
     <xs:choice minOccurs="0" maxOccurs="unbounded"> 
     <xs:element name="field" nillable="true"> 
      <xs:complexType> 
      <xs:simpleContent> 
       <xs:extension base="xs:string"> 
       <xs:attribute name="value" type="xs:string" /> 
       <xs:attribute name="text" type="xs:string" /> 
       </xs:extension> 
      </xs:simpleContent> 
      </xs:complexType> 
     </xs:element> 
     </xs:choice> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 
相關問題