XSD validation - Make child element as Mandatory if the Parent Element is Present, Else Make the Child Optional

StackOverflow https://stackoverflow.com/questions/19853760

  •  29-07-2022
  •  | 
  •  

Question

I have an xml which looks like:

<RawData>
                  <EmailAddress>xyz.abc@email.com</EmailAddress>
                  <PassengerName>XYZ/ABC</PassengerName>
</RawData>

and the corresponding XSD like the below:

<xsd:element name="RawData" minOccurs="0" maxOccurs="1">
                    <xsd:complexType>
                        <xsd:sequence>
                        <xsd:element name="EmailAddress" minOccurs="0" maxOccurs="1">
                    </xsd:element>
                        <xsd:element name="PassengerName" minOccurs="0" maxOccurs="1"/>
</xsd:element>
                        </xsd:sequence>
                    </xsd:complexType>

I need to make PassengerName Mandatory if RawData is present, else Optional.

Kindly help with your suggestions.

Thanks in advance.

Was it helpful?

Solution

If the parent element is not present then it is not validated and the schema definitions for that element therefore have no relevance. If the parent is absent, the children will always be absent, and the schema automatically allows them to be absent.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top