Search code examples
xmlxsdxml-validationxmlschema

xsd requires different elements based on xml message content


I have 2 xml messages that are nearly similar but do have some dedicated elements. now I want to combine these to messages and create a "flexible" xml schema for it so I can validate it before processing.

The idea is to have a common structure where all the fields are defined that are in both xml messages and then 2 dedicated parts for the fields that differ. And then somehow determine which message is being received and then using the common and correct dedicated part of the xml schema to validate.

a simple example is shown below. Depending on the TransportType (which can be car or boat) the xml message will contain 3 elements. 2 elements (make and model) are common for both and the other 2 (airbags and motorized) are specific depending on the choice.

Car example

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <TransportType>Car</TransportType>
    <Input>
        <Make>String</Make>
        <Model>String</Model>
        <Airbags>true</Airbags>
    </Input>
</root>

the boat example

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <TransportType>Boat</TransportType>
    <Input>
        <Make>String</Make>
        <Model>String</Model>
        <Motorized>true</Motorized>
    </Input>
</root>

I've been playing around with choices and references but I can't get it to work without adding elements (which is not allowed).


Solution

  • Unless you're prepared to use an xsi:type attribute in the source document to distinguish the different cases, this can't be done in XSD 1.0. In XSD 1.1 you can do it using assertions - though if you can make TransportType an attribute rather than an element, you can do it using conditional type assignment, which is a much neater solution as it's custom-designed for this job.

    See https://www.xml.com/articles/2018/05/29/co-occurrence-cta-xsd/