【问题标题】:In XSD, either any or both elements should be present在 XSD 中,任何一个或两个元素都应该存在
【发布时间】:2017-01-22 21:26:46
【问题描述】:

我正在尝试编写一个 XSD,其中任何一个 EM 或 RUID 都应该存在,或者两者都存在。但是使用下面的 XSD,我收到以下错误:

错误:模型组中出现多个名称为“RUID”且类型不同的元素。

<xs:element name="ADCNT">
  <xs:complexType>
    <xs:choice minOccurs="1" maxOccurs="1">

      <xs:sequence>
        <xs:element name="EM" minOccurs="1" maxOccurs="1"></xs:element>
        <xs:element name="RUID" minOccurs="0"  maxOccurs="1"></xs:element>
      </xs:sequence>

      <xs:sequence>
        <xs:element name="RUID" minOccurs="1" maxOccurs="1">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="username" minOccurs="0" maxOccurs="1">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:minLength value="1" />
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>

    </xs:choice>
  </xs:complexType>
</xs:element>

【问题讨论】:

    标签: xml xsd xsd-validation xml-validation


    【解决方案1】:

    XSD 中的一个元素或两个元素

    此 XSD 将允许 ADCNT 包含 EMRUID 或两者:

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
      <xs:element name="EM" type="xs:string"/>
      <xs:element name="RUID" type="xs:string"/>
    
      <xs:element name="ADCNT">
        <xs:complexType>
          <xs:choice>
            <xs:element ref="RUID"/>
            <xs:sequence>
              <xs:element ref="EM"/>
              <xs:element ref="RUID" minOccurs="0"/>
            </xs:sequence>
          </xs:choice>
        </xs:complexType>
      </xs:element>
    
    </xs:schema>
    

    更新:我已根据 OP 的要求将 RUID 的类型更改为复杂,但原理保持不变:

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
      <xs:complexType name="RUIDType">
        <xs:sequence>
          <xs:element name="username" minOccurs="0" maxOccurs="1">
            <xs:simpleType>
              <xs:restriction base="xs:string">
                <xs:minLength value="1" />
              </xs:restriction>
            </xs:simpleType>
          </xs:element>
        </xs:sequence>
      </xs:complexType>
    
      <xs:element name="ADCNT">
        <xs:complexType>
          <xs:choice>
            <xs:element name="RUID" type="RUIDType"/>
            <xs:sequence>
              <xs:element name="EM" type="xs:string"/>
              <xs:element name="RUID" type="RUIDType" minOccurs="0"/>
            </xs:sequence>
          </xs:choice>
        </xs:complexType>
      </xs:element>
    
    </xs:schema>
    

    【讨论】:

    • 当我在元素中添加复杂类型时,我得到了错误,但是使用这个简单的 XS:String 它可以正常工作。那么,你能帮我处理一下复杂的类型吗?
    • 答案已更新以将RUID 的类型更改为复杂,但请注意,该原则仍然独立于RUIDEM 的类型。
    猜你喜欢
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    • 2018-06-01
    • 1970-01-01
    • 2019-07-07
    相关资源
    最近更新 更多