Can we make an element optional or required depending on the value of any other element in XSD

In XSD, I have one x element that takes values ​​(a, b)

There is another element y

Is it possible to use XSD so that I can make y mandatory if the value of x is b and optional if the value of x is

+4
source share
1 answer

See XML Schema Part 0: Primer Second Edition , Section 2.2.1 Appearance Restrictions .

It says:

Typically, an item should appear when the value of minOccurs is 1 or more. The maximum number of times an item can appear is determined by the value of the maxOccurs attribute in its declaration. This value can be a positive integer, such as 41, or an unlimited member to indicate that the maximum number of occurrences does not exist. The default value for the minOccurs and maxOccurs attributes is 1.

Since the attribute value cannot be inferred from other contents of the schema in the XML schema, what you want to achieve is not possible (using the XML schema).

(I don't know if Relax NG can , but you can find out if it supports such content-dependent definitions.)

+1
source

All Articles