XSD schema with choice

2019-02-27 19:16发布

问题:

I need to validate XML request data in below array:

<studyYear></studyYear>
<orgID></orgID>
<originID></originID>
<providerID></providerID>
<userOID></userOID>

Problem - I have to get either (orgID) or (userOID) or (originID and providerID) together. 'studyYear' will always be there. How I can realise it? If need more information just write. I referenced this link to use so as to try using xs:choice inside xs:all but could not get it working.

回答1:

This XSD,

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="r">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="studyYear" type="xs:string"/>
        <xs:choice>
          <xs:element name="orgID" type="xs:string"/>
          <xs:element name="userOID" type="xs:string"/>
          <xs:sequence>
            <xs:element name="orginID" type="xs:string"/>
            <xs:element name="providerId" type="xs:string"/>
          </xs:sequence>
        </xs:choice>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

will require studyYear to be followed by one of the following cases,

  • orgID, or
  • userOID, or
  • both originID and providerID

as requested.