[英]XML element occurrence
我正在為以下XML編寫架構。 我想將Element_E,Element_F和Element_G的出現限制為“ 1”,並且Element_D可以出現n次。 我嘗試使用xs:sequence
但是它強制執行元素順序, xs:choice
不會檢查上述元素的最大出現次數。
是否可以通過XML Schema驗證組中每個元素的出現?
<?xml version="1.0" encoding="utf-8"?>
<Element_A> <!-- One time occurrence -->
<Element_B> <!-- One time occurrence -->
<Element_C> <!-- One time occurrence -->
<Element_D /> <!-- n time occurrence -->
<Element_D />
<Element_D />
<Element_E /> <!-- One time occurrence -->
<Element_F /> <!-- One time occurrence -->
<Element_G /> <!-- One time occurrence -->
</Element_C>
</Element_B>
</Element_A>
使用XSD 1.0時 ,您必須在代碼中檢查這種約束。
借助XSD 1.1 ,您可以使用xs:assert
來聲明計數不變式:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.1"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">
<xs:element name="Element_A">
<xs:complexType>
<xs:sequence>
<xs:element name="Element_B">
<xs:complexType>
<xs:sequence>
<xs:element name="Element_C">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Element_D"/>
<xs:element name="Element_E"/>
<xs:element name="Element_F"/>
<xs:element name="Element_G"/>
</xs:choice>
<xs:assert test=" count(Element_E) = 1
and count(Element_F) = 1
and count(Element_G) = 1"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
您是否研究過maxOccurs ?
<xsd:element ref="Element_E" maxOccurs="1"/>
http://www.w3schools.com/schema/schema_complex_indicators.asp
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.