[英]XML element occurrence
I am writing a schema for the following XML. 我正在为以下XML编写架构。 I want to limit the occurrence of Element_E, Element_F and Element_G to '1' and Element_D can occur n number of times.
我想将Element_E,Element_F和Element_G的出现限制为“ 1”,并且Element_D可以出现n次。 I tried to use
xs:sequence
but it enforces the element order, xs:choice
does not check the max occurrences of above elements. 我尝试使用
xs:sequence
但是它强制执行元素顺序, xs:choice
不会检查上述元素的最大出现次数。
Is it possible to validate the occurrence of each element in a group through XML Schema? 是否可以通过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>
With XSD 1.0 , you'll have to check such a constraint in code. 使用XSD 1.0时 ,您必须在代码中检查这种约束。
With XSD 1.1 , you can use xs:assert
to state count invariants: 借助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>
Have you looked into maxOccurs ? 您是否研究过maxOccurs ?
<xsd:element ref="Element_E" maxOccurs="1"/>
http://www.w3schools.com/schema/schema_complex_indicators.asp http://www.w3schools.com/schema/schema_complex_indicators.asp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.