Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional validation with XML Schema

Tags:

xml

xsd

This is probably basic but I am struggling to implement it with my basic knowledge of XML and XML Schema.

Any help is gratefully received.

Given the following XML file snippet:

<record>
   ...
   <update_type>3</update_type>
   <reason>User entered in error</reason>
   ...
</record>
<record>
   ...
   <update_type>1</update_type>
   <reason></reason>
   ...
</record>

Is there a way I can conditionally validate the XML file ensuring the <reason> string element is mandatory when the <update_type> numeric element has a value of 3 but optional when the <update_type> is another value?

Thanks in advance...

like image 432
Ollie Avatar asked Jun 24 '26 21:06

Ollie


1 Answers

No you can't, not with XML Schema.

You need to write that logic in your code, after you validate the XML structure with the Schema and make sure it's content is valid.

like image 67
JohnDoDo Avatar answered Jun 27 '26 18:06

JohnDoDo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!