Link to home
Start Free TrialLog in
Avatar of CodeMarx
CodeMarxFlag for United States of America

asked on

XSD Conditional Elements

I need to make some elements required based on the value of another element.

<xs:element name="LoanType" type="xs:string" />
If LoanType is Adjustable then
<xs:element name="Margin" type="xs:int" />
else
<xs:element name="Margin" type="xs:int" minOccurs="0' />

Thanks in advance.
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

You can't achieve this by simply XSD.
What you are asking for is a co-occurence constraint, a type of constraint that can not be modelled by XSD schema.
You basically have three options
- have a second schema that uses schematron constraints (http://www.schematron.com/)
- use an XSD parser that allows embedded schematron rules (this would be the approach I would use)
- use an XSD parser that understands XSD 1.1, a new schema evolution that has "assert", very similar to schematron assertions
Avatar of CodeMarx

ASKER

How about applying a XLST to the XML document first?
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
One defines a complecType called LoansType, which is a choice of various loan types (elements). One such element is called AdjustableLoanType which then can have Margin as either a sub-element or an attribute.

Generally if you have an enumeration which causes structural differences then you should enumerate the structures rather than use a scalar and overload the structure.