CharbelBadr
CharbelBadr

Reputation: 77

Validate XML attribute based on other attribute value

I have the below XML.

<?xml version="1.0" encoding="utf-8" ?>
<Items>
    <Item type="none" attNone1="test" attNone2="test3" />
    <Item type="action" attAction1="test" attAction2="test2" />
</Items>

I need to create an XSD based on some rules:

1) the attribute "type" is required.

2) If the attribute value should be "none" or "action"

3) If the attribute value is "none" I should have two other required attribute, "attNone1" and "attNone2". I should not accept "attAction1" or "attAction2"

4) If the attribute value is "action" I should have two other required attribute, "attAction1" and "attAction2". I should not accept "attNone1" or "attNone2"

Any idea how to do this XSD?

Thanks in advance,

Regards,

Upvotes: 1

Views: 546

Answers (1)

Michael Kay
Michael Kay

Reputation: 163262

XSD 1.0 does not allow "co-occurrence constraints" where the type of one attribute depends on the value of another.

In XSD 1.1 you can use "conditional type assignment" which was invented for this purpose. XSD 1.1 is currently supported in Xerces and Saxon.

Upvotes: 1

Related Questions