CodeMarx
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.
<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.
ASKER
How about applying a XLST to the XML document first?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
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