简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM