繁体   English   中英

XML元素出现

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM