I am writing a rule engine where user can specify rules in any sequence but once specified, they are executed in same order as configured. Something like this
<RuleEngine>
<Rule1 ..>
<Rule2 ..>
<Rule3 ...>
</RuleEngined>
(Execution : Rule1
->Rule2
->Rule3
)
OR like this
<RuleEngine>
<Rule3 ..>
<Rule1 ..>
<Rule2 ...>
</RuleEngine>
(Execution : Rule3
->Rule1
->Rule2
)
I am writing a schema for this and am not able to get desired result with xsd:all
or xsd:sequence
. If I use xsd:all
it will allow flexibility for user but will NOT preserve order in code while parsing.
<xs:element name="RuleEngine">
<xs:complexType>
<xs:all>
<xs:element ref="Rule1" ..
<xs:element ref="Rule2" ..
<xs:element ref="Rule3" ..
</xs:all>
</xs:complexType>
</xs:element>
I can't use xsd:sequence
either, else it will prevent users from specifying Rules in the order of their choice, though in CODE it will let developer know the order.
<xs:element name="RuleEngine">
<xs:complexType>
<xs:sequence>
<xs:element ref="Rule1" ..
<xs:element ref="Rule2" ..
<xs:element ref="Rule3" ..
</xs:sequence>
</xs:complexType>
</xs:element>
I read few threads e.g. thread1 , thread2 and it seems there is no direct way out with XML/XSD for this. You can either enforce order OR not enforce it, can't have both like I am dreaming :) One not-so-preferred option is to use "all" and enforce user to put an numeric ID along with the rule, as a hint for code to sort out rules before execution. Other tedious one is to migrate onto Boost Property Tree
Any suggestion if I can achieve desired behavior with existing framework?