【问题标题】:XSD none, one, or bothXSD 无、一个或两个
【发布时间】:2015-06-02 00:58:48
【问题描述】:

我搜索了一大堆,在发布之前找不到任何类似的问题。我正在创建一个 XSD,一个 complexType 有这些要求:

  • 元素不能有两个字段
  • 元素可以有字段一
  • 如果元素有字段二,它也必须有字段一

它基本上可以解决“无,一个(但一个特定的),或两者兼而有之”,但我无法弄清楚我做错了什么,任何帮助将不胜感激

<xs:complexType name="Foo">
    <xs:choice minOccurs="0">
        <xs:sequence>
            <xs:element name="fieldOne" maxOccurs="1"/>
        </xs:sequence>
        <xs:sequence>
            <xs:element name="fieldOne" maxOccurs="1"/>
            <xs:element name="fieldTwo" maxOccurs="1"/>             
        </xs:sequence>
    </xs:choice>
</xs:complexType>

【问题讨论】:

    标签: xml xsd


    【解决方案1】:

    你可以用一个可选的序列来做到这一点:

    <xs:complexType name="Foo">
        <xs:sequence minOccurs="0">
            <xs:element name="fieldOne"/>
            <xs:element name="fieldTwo" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>
    

    fieldTwo 存在的唯一方法是序列出现一次,在这种情况下,fieldOne 也是必需的。

    【讨论】:

    • 我不知道你能做到这一点。感谢您的快速响应!
    猜你喜欢
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    相关资源
    最近更新 更多