【问题标题】:XML Schema - Allow multiple occurrences of some elements and require others [duplicate]XML Schema - 允许某些元素多次出现并需要其他元素[重复]
【发布时间】:2021-03-21 10:27:50
【问题描述】:

在一个 xml 文档中(我不能更改)我有这样的结构:

<rootMeta> // all children can occur in any order
   <requiredExample1 value="someValue" /> // Should only be allowed to occur once. REQUIRED
   <nonRequiredExample2 value="someValue" /> // Should only be allowed to occur once. NOT REQUIRED
   <nonRequiredExample3 value="someValue" /> // All these are NOT REQUIRED but can occur many times.
   <nonRequiredExample3 value="someValue" />
   <nonRequiredExample3 value="someValue" />
</rootMeta>

我尝试了“全部”和“选择”,但还没有找到一种方法来满足所有强制要求(第一次尝试构建模式)。我试图寻找重复的问题,但只能找到需要我更改 xml 的解决方案。我当前的迭代如下所示:

<xs:element name="rootMeta">
        <xs:complexType>
            <xs:choice maxOccurs="unbounded">
                <xs:element name="requiredExample1" minOccurs="1" maxOccurs="1">
                    <xs:complexType>
                        <xs:attribute name="value" type="xs:string" use="required" />
                    </xs:complexType>
                </xs:element>
                <xs:element name="nonRequiredExample2" maxOccurs="1">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:maxLength value="200" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="nonRequiredExample3">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:maxLength value="200" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
          <xs:choice>
     <xs:complexType>
</xs:element>

【问题讨论】:

    标签: xml xsd schema


    【解决方案1】:

    在 XSD 1.1 中,您可以允许任何顺序,并使用 xs:all 定义基数约束。

    在 XSD 1.0 中,如果您想限制出现次数,那么您还必须限制顺序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-30
      • 1970-01-01
      • 2011-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多