Setting minOccurs and maxOccurs in XSD based on value of other XML field?

醉酒当歌 提交于 2019-11-28 12:59:41

Design recommendation: If your XML design can still be changed, eliminate the size element and convey that information implicitly rather than explicitly. By eliminating the duplication of information, you'll not need to check that the duplication is consistent.

If your XML design cannot still be changed, or if you choose not to change it...

XSD 1.0

Not possible. Would have to be checked out-of-band wrt XSD.

XSD 1.1

Possible using xs:assert:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" 
           elementFormDefault="qualified" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
           vc:minVersion="1.1">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="child">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="size" type="xs:integer"/>
              <xs:element name="childElement" maxOccurs="unbounded"/>
            </xs:sequence>
            <xs:assert test="count(childElement) = size"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!