2

当元素包含在 XML 中时,如何确保至少指定一个location子元素 ( locality, wkt) ?location

<xs:element name="location" nillable="true" minOccurs="0">
  <xs:complexType>
    <xs:group ref="cs:locationGroup"></xs:group>
  </xs:complexType>
</xs:element>

的定义locationGroup

<xs:group name="locationGroup">
  <xs:all>
    <xs:element name="locality" minOccurs="0"/>
    <xs:element name="wkt" minOccurs="0"/>
  </xs:all>
</xs:group>

我的 XSD 版本是 1.0。

4

1 回答 1

1

对于这么少的可能子元素,只需定义一个xs:choice允许的组合:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="location">
    <xs:complexType>
      <xs:group ref="locationGroup"></xs:group>
    </xs:complexType>
  </xs:element>

  <xs:group name="locationGroup">
    <xs:choice>
      <xs:sequence>
        <xs:element name="locality"/>
        <xs:element name="wkt" minOccurs="0"/>
      </xs:sequence>
      <xs:sequence>
        <xs:element name="wkt"/>
        <xs:element name="locality" minOccurs="0"/>
      </xs:sequence>
    </xs:choice>
  </xs:group>
</xs:schema>

请注意,这种方法

  • 需要其中之一或两者localitywkt存在
  • 当两者都存在时允许任何订单
  • 适用于 XSD 1.0(和 1.1)

按照要求。

于 2016-11-29T13:31:08.507 回答