【问题标题】:XSD choice of elements and simple typesXSD 元素和简单类型的选择
【发布时间】:2011-08-24 15:31:14
【问题描述】:

XSD怎么写,简单类型和复杂类型的选择应该在哪里。

例如,所有这些 XML:

<a>
  <b/>
</a>

<a>
  <c/>
</a>

<a>
  100
</a>

(最后一个 XML 中只允许整数)应该是有效的。

PS: 标签内只允许有一个元素。

<a>
  <b/>
  <c/>
</a>

无效。

【问题讨论】:

    标签: xml xsd


    【解决方案1】:
    <xs:element name="a">
      <xs:complexType mixed="true">
        <xs:choice>
          <xs:element name="b" type="xs:string" />
          <xs:element name="c" type="xs:string" />
        </xs:choice>
      </xs:complexType>
    </xs:element>
    

    希望对您有所帮助。

    编辑:我已经更新了示例。但是,您不能将第三个示例中的类型限制为整数。 XSD 不支持你想做的事情。

    【讨论】:

      【解决方案2】:

      可以这样:

      <xsd:element name="a">
        <xsd:complexType>
          <xsd:choice>
            <xsd:element name="b">
              <xsd:complexType/>
            </xsd:element>
            <xsd:element name="c">
              <xsd:complexType/>
            </xsd:element>
            <xsd:element>
              <xsd:simpleType>
                <xsd:restriction base="xsd:integer"/>
              </xsd:simpleType>
            </xsd:element>
          </xsd:choice>
        </xsd:complexType>
      </xsd:element>
      

      挽救局面?

      【讨论】:

      • 没有。您在这里说过&lt;a&gt; 可能包含&lt;b&gt;&lt;c&gt; 或其他简单类型整数的元素。但是,该元素需要一个名称,因此架构片段无效。
      猜你喜欢
      • 1970-01-01
      • 2013-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多