0

如何使每条记录具有相等的值计数?现在每条记录都可以有不同的值计数。

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema">
 <element name="list">
  <complexType>
   <sequence>
    <element name="record" minOccurs="0" maxOccurs="unbounded">
     <complexType>
      <sequence>
       <element name="value" type="string" minOccurs="0" maxOccurs="unbounded"/>
      </sequence>
     </complexType>
    </element>
   </sequence>
  </complexType>
 </element>
</schema>

对于此架构,此 xml 将有效:

<?xml version="1.0" encoding="UTF-8"?>
<list>
    <record>
        <value>value 11</value>
        <value>value 12</value>
        <value>value 13</value>             
    </record>
    <record>
        <value>value 21</value>
        <value>value 22</value>
        <value>value 23</value>             
    </record>
</list>

这也是有效的:

<?xml version="1.0" encoding="UTF-8"?>
    <list>
        <record>
            <value>value 11</value>
            <value>value 12</value>
            <value>value 13</value>             
        </record>
        <record>
            <value>value 21</value>
            <value>value 22</value>
            <value>value 23</value>
            <value>value 24</value>
            <value>value 25</value>
        </record>
    </list>

如何更改架构以使第二个 xml 文件无效?

4

1 回答 1

1

似乎 XSD 1.0 唯一可能的解决方案是定义多个记录元素:

<xs:element name="list">
  <xs:complexType>
    <xs:choice>
      <xs:element name="record1" minOccurs="0" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element ref="value" minOccurs="1" maxOccurs="1" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="record2" minOccurs="0" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element ref="value" minOccurs="2" maxOccurs="2" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="record3" minOccurs="0" maxOccurs="unbounded">
        <xs:complexType>
          <xs:sequence>
            <xs:element ref="value" minOccurs="3" maxOccurs="3" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <!-- and so on... -->
    </xs:choice>
  </xs:complexType>
</xs:element>

<xs:element name="value" type="xs:string" />

但 XML 会有所不同:

<list>
  <record3>
    <value>value 11</value>
    <value>value 12</value>
    <value>value 13</value>
  </record3>
  <record3>
    <value>value 21</value>
    <value>value 22</value>
    <value>value 23</value>
  </record3>
</list>
于 2013-11-06T16:27:47.193 回答