如何定義只能在一個元素中設置爲「true」的布爾屬性。 以下片段必須無效。XML模式 - 只有一個元素應該將屬性設置爲true
<products>
<product featured="yes">Prod 1</product>
<product featured="yes">Prod 2</product>
</products>
如何定義只能在一個元素中設置爲「true」的布爾屬性。 以下片段必須無效。XML模式 - 只有一個元素應該將屬性設置爲true
<products>
<product featured="yes">Prod 1</product>
<product featured="yes">Prod 2</product>
</products>
你不能用XML模式來做到這一點。
您可以在元素上定義屬性,但不能將它們限制爲元素的一個實例。
你不能用XMLSchema做到這一點。如果您想在XML環境中指定這些約束,請嘗試Schematron(http://www.schematron.com/)。
我的回覆是這樣,因爲我還不能添加評論。
「您可以在products元素中添加一個屬性,以指示哪個產品具有特色。」
此解決方案導致另一個問題:檢查屬性是否指向現有元素。
<products featured_id="3">
<product id="1">Prod 1</product>
<product id="2">Prod 2</product>
</products>
你可以做以下...
<products>
<product featured="Yes">Prod 1</product>
<product>Prod 2</product>
</products>
然後使用一個獨特的元素來約束這樣的屬性...
<xs:unique name="UniqueFeaturedProduct">
<xs:selector xpath="product"/>
<xs:field xpath="@featured"/>
</xs:unique>
如果要限制「功能'屬性爲一個值的可選枚舉「是」,那麼只能有一個特色屬性。像這樣...
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="products">
<xs:complexType>
<xs:sequence>
<xs:element name="product" type="productType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="UniqueFeaturedProduct">
<xs:selector xpath="product"/>
<xs:field xpath="@featured"/>
</xs:unique>
</xs:element>
<xs:simpleType name="featuredType">
<xs:restriction base="xs:string">
<xs:enumeration value="Yes"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="productType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="featured" type="featuredType" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:schema>
我不相信這是可能的XML架構。 – skaffman 2009-12-18 08:19:41