0

I have a requirement where I am trying to define require fields (minOccurs=1) based on other field value in XSD but not able to do so.

I am trying to give simple example to understand my problem a bit more. Please refer below two simple xml where state is the field which defines what event occured. I need to define processingTime and processResult as required fields in XSD file if state field value is PROCESS_END because there is no need of these fields when process starts. This is just simple example to illustrate my problem. Thanks in advance.

<?xml version = "1.0"?>
<metadata>
    <title>The processing started event</title>
    <state>PROCESS_START</state>
</metadata>

<?xml version = "1.0"?>
<metadata>
    <title>The processing ended event</title>
    <state>PROCESS_END</state>
    <processingTime>100s</processingTime>
    <processResult>success</processResult>
</metadata>
Vishal Zanzrukia
  • 4,902
  • 4
  • 38
  • 82
  • 1
    You will first need to ensure your parser supports XSD 1.1 to do that. Then, have a look at https://stackoverflow.com/questions/27878402/how-to-make-type-depend-on-attribute-value-using-conditional-type-assignment, which is closely related to your issue here? – potame Apr 24 '20 at 06:50

1 Answers1

1

In XSD 1.1, define processingTime and processResult as optional elements, and on the complexType for metadata, define <xs:assert test="if (state='PROCESS_END') then exists(processingTime) and exists(processResult) else true()"/>.

It can't be done in XSD 1.0. Many of the commonly used XSD validators, e.g. the one from Microsoft, have never been upgraded to XSD 1.1.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164