I have a program that uses XML formatted rules to create executable code for run-time. I have to define some actions and logical constructs using my own dialect. I have OR, AND, and NOT constructs and now I need to implement IF..THEN..ELSE.
I'm trying to come up with a syntax that would make sense and here's what I have so far:
<IF id='if-1'>
<TIME from="5pm" to="9pm" />
</IF>
<THEN id='if-1'>
<...some actions defined.../>
</THEN>
<ELSE id='if-1'>
<...other set of actions defined here.../>
</ELSE>
If looks difficult to read to me, but I don't see a cleaner way to represent this without doing too much nesting. Does anyone have a suggestion? (not using XML is not an option at this point in time :) )
The <xsl:if> Element To put a conditional if test against the content of the XML file, add an <xsl:if> element to the XSL document.
You can't, as such: XML isn't a programming language. But you can have conditional criteria in a Schema, DTD, or a processor, and some DTDs provide attributes for conditional processing.
Metadata applications: XML makes it easier to express metadata in a portable, reusable format. Pervasive computing: XML provides portable and structured information types for display on pervasive (wireless) computing devices such as personal digital assistants (PDAs), cellular phones, and others.
Faced with a similar problem sometime ago, I decided to go for a generalized "switch ... case ... break ... default" type solution together with an arm-style instruction set with conditional execution. A custom interpreter using a nesting stack was used to parse these "programs". This solution completely avoids id's or labels. All my XML language elements or "instructions" support a "condition" attribute which if not present or if it evaluates to true then the element's instruction is executed. If there is an "exit" attribute evaluating to true and the condition is also true, then the following group of elements/instructions at the same nesting level will neither be evaluated nor executed and the execution will continue with the next element/instruction at the parent level. If there is no "exit" or it evaluates to false, then the program will proceed with the next element/instruction. For example you can write this type of program (it will be useful to provide a noop "statement" and a mechanism/instruction to assign values and/or expressions to "variables" will prove very handy):
<ins-1>
<ins-11 condition="expr-a" exit="true">
<ins-111 />
...
</ins11>
<ins-12 condition="expr-b" exit="true" />
<ins-13 condition="expr-c" />
<ins-14>
...
</ins14>
</ins-1>
<ins-2>
...
</ins-2>
If expr-a is true then the execution sequence will be:
ins-1
ins-11
ins-111
ins-2
if expr-a is false and expr-b is true then it will be:
ins-1
ins-12
ins-2
If both expr-a and expr-b are false then we'll have:
ins-1
ins-13 (only if expr-c evaluates to true)
ins-14
ins-2
PS. I used "exit" instead of "break" because I used "break" to implement "breakpoints". Such programs are very hard to debug without some kind of breakpointing/tracing mechanism.
PS2. Because I had similar date-time conditions as your example along with the other types of conditions, I also implemented two special attributes: "from" and "until", that also had to evaluate to true if present, just like "condition", and which used special fast date-time checking logic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With