簡體   English   中英

如何在XML Schema中定義具有相同名稱但不同類型的元素序列?

[英]How to define a sequence of elements with same name but different types in 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 Schema不允許設置名稱相同但類型不同的元素列表(簡單或復雜都沒有關系...)。 這樣對嗎? 有沒有其他方法可以定義上述結構?

編輯:也許我必須解釋得更好一點。 在元素“ field”的打開和關閉標簽之間,必須有一個由XML模式定義的值(換句話說,對於用戶而言,不可能編寫與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>

那可能嗎? 提前致謝!

嘗試以下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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM