簡體   English   中英

XML和XSD-子元素僅在選擇某些父屬性時才有效?

[英]XML and XSD - Sub-Elements only valid when certain Parent Attribute is selected?

是否可以在XSD中聲明子元素僅在父元素選擇了特定屬性時才有效?

因此,例如在xml中:

<dropdown style="radiobuttons">
<button>my first choice</button>
<button>my second choice</button>
<button>my third choice</button>
</dropdown>

<dropdown style="checkboxes">
<checkbox>my first choice</checkbox>
<checkbox>my second choice</checkbox>
<checkbox>my third choice</checkbox>
</dropdown>

但以下內容將是無效的/架構將不支持它,因為button元素只能與style =“ radiobutton”一起使用

<dropdown style="checkboxes">
<button>my first choice</button>
<button>my second choice</button>
<checkbox>my third choice</checkbox>
</dropdown>

我知道選擇將需要在xsd中由xs:enumeration控制

XSD 1.0不支持這種條件類型分配,但是XSD 1.1支持:

XSD 1.1解決方案

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
  vc:minVersion="1.1">

  <xs:element name="dropdown">
    <xs:alternative test="@style eq 'radiobuttons'" type="RadioButtonsType" />
    <xs:alternative test="@style eq 'checkboxes'" type="CheckBoxesType" />
  </xs:element>

  <xs:complexType name="RadioButtonsType">
    <xs:sequence>
      <xs:element name="button" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="CheckBoxesType">
    <xs:sequence>
      <xs:element name="checkbox" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>

</xs:schema>

但是,請注意,您可以很自然地避免對XSD 1.1和條件類型賦值的需要,只需在button元素和checkbox元素中使用不同的容器元素,而不是對兩者都使用dropdown

暫無
暫無

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

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