Question

In XSD schema, I know I can define the sequence or element, choice of element, or required set that doesn't have to be in verbatim order, but is there a way to say a complex type should have either element John or element Jack, or none of them, and only if element John is present, should element Anna be possible to be specified once (but not required) at the same nesting level as John?

Possible scenarios:

<John /> | <John /> | <Jack /> | <!-- Neither. -->
           <Anna /> |          | 

If yes, I suppose the following should also be possible to say:

<FolderPath /> | <MainFilePath />      | <!-- Neither. -->
               | <Sidekick1FilePath /> |
               | <SideKick2FilePath /> |

Again, neither is required, but once folder is specified, none of the files must be present, and when all of the files are specified, folder must not be specified. It should not be possible to specify one or two files only, either a folder or three files or nothing.

Was it helpful?

Solution

You can achieve this by nesting choices and sequences. Optional contents ("neither" in your example) is done with minOccurs="0" cardinality. Cardinality restrictions can be used on element declarations or sets like here on <xs:choice> Sample type below.

<xs:complexType name="something">
    <xs:choice minOccurs="0">
        <xs:sequence>
            <xs:element name="John" type="xs:string" />
            <xs:element minOccurs="0" name="Anna" type="xs:string" />
        </xs:sequence>
        <xs:element name="Jack" type="xs:string" />
    </xs:choice>
</xs:complexType>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top