How to force child element to have a value in XSD?

若如初见. 提交于 2019-12-01 20:26:06

XSD 1.0

Your constraint cannot be expressed in XSD 1.0.

XSD 1.1

Your constraint can be expressed in XSD 1.1 using an assertion to state that there be at least one Type child of Order that has a value of 1:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
           vc:minVersion="1.1"> 
  <xs:element name="Order">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Type" maxOccurs="unbounded" type="xs:integer"/>
      </xs:sequence>
      <xs:assert test="Type = 1"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

in XSD 1.1, you can use xs:assertelement:

<xs:element name="Order">
    <xs:complexType>
        <xs:sequence maxOccurs="unbounded">
            <xs:element name="Type" type="xs:string"/>

        </xs:sequence>
        <xs:assert test="count(Type[text() = '1']) > 0"/>
    </xs:complexType>

</xs:element>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!