IF...THEN...ELSE 使用 XML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6061470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 14:43:09  来源:igfitidea点击:

IF...THEN...ELSE using XML

xml

提问by Lirm

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.

我有一个程序,它使用 XML 格式的规则为运行时创建可执行代码。我必须使用我自己的方言来定义一些动作和逻辑结构。我有 OR、AND 和 NOT 结构,现在我需要实现 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 :) )

If 对我来说看起来很难读,但我没有看到一种更清晰的方式来表示它而不做太多嵌套。有人有建议吗?(此时不使用 XML 不是一种选择:))

采纳答案by The Communist Duck

I personally think that the if/then/else needs to be linked somehow.

我个人认为 if/then/else 需要以某种方式链接。

<IF something>
    <some actions>
<THEN something>
    <some actions>
</THEN>
<ELSE something>
    <some actions>
</ELSE>
</IF>

回答by Christoph Mühlmann

Perhaps another way to code conditional constructs in XML:

也许另一种在 XML 中编码条件结构的方法:

<rule>
    <if>
        <conditions>
            <condition var="something" operator="&gt;">400</condition>
            <!-- more conditions possible -->
        </conditions>
        <statements>
            <!-- do something -->
        </statements>
    </if>
    <elseif>
        <conditions></conditions>
        <statements></statements>
    </elseif>
    <else>
        <statements></statements>
    </else>
</rule>

回答by Costas

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):

前段时间遇到类似的问题,我决定采用通用的“switch ... case ... break ... default”类型的解决方案以及带有条件执行的 arm 样式指令集。使用嵌套堆栈的自定义解释器用于解析这些“程序”。此解决方案完全避免了 id 或标签。我的所有 XML 语言元素或“指令”都支持“条件”属性,如果该属性不存在或计算结果为真,则执行元素的指令。如果有一个“exit”属性求值为真且条件也为真,则相同嵌套级别的以下一组元素/指令既不会被求值也不会执行,执行将继续执行下一个元素/指令父级。如果没有“退出”或评估结果为假,则程序将继续执行下一个元素/指令。例如,您可以编写这种类型的程序(提供 noop“语句”会很有用,并且将值和/或表达式分配给“变量”的机制/指令将证明非常方便):

<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:

如果 expr-a 为真,则执行顺序为:

ins-1
ins-11
ins-111
ins-2

if expr-a is false and expr-b is true then it will be:

如果 expr-a 为假而 expr-b 为真,那么它将是:

ins-1
ins-12
ins-2

If both expr-a and expr-b are false then we'll have:

如果 expr-a 和 expr-b 都是假的,那么我们将有:

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.

附注。我使用了“exit”而不是“break”,因为我使用了“break”来实现“断点”。如果没有某种断点/跟踪机制,这样的程序很难调试。

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.

PS2。因为我有与您的示例类似的日期时间条件以及其他类型的条件,所以我还实现了两个特殊属性:“from”和“until”,如果存在,也必须评估为真,就像“条件”一样,并且使用了特殊的快速日期时间检查逻辑。

回答by Michael Kay

I don't think you can design the if-then-else construct without taking the design for other constructs into account. I think it's a good principle that each expression should be an element, and its subexpressions should be child elements. There are then questions about whether the name of an element should reflect the type of expression it is, or its role relative to the parent. Or you can do both:

我认为您无法在不考虑其他结构的设计的情况下设计 if-then-else 结构。我认为每个表达式都应该是一个元素,而它的子表达式应该是子元素,这是一个很好的原则。然后是关于元素的名称是否应该反映它的表达式类型,或其相对于父级的角色的问题。或者你可以两者都做:

<if>
  <condition>
    <equals>
      <number>2</number>
      <number>3</number>
    <equals>
  <condition>
  <then>
    <string>Mary</string>
  </then>
  <else>
    <concat>
      <string>John</string>
      <string>Smith</string>
    </concat>
  </else>
</if>

But you can sometimes get away with a design that omits the role-names (condition, then else) and relies on positional significance of elements relative to their parent. It depends a bit on how much you want to keep it concise.

但是有时您可以通过省略角色名称(条件,然后其他)并依赖元素相对于其父元素的位置重要性的设计逃脱。这在一定程度上取决于您希望保持简洁的程度。

回答by Jasoon

<IF id='if-1'>
    <CONDITION>
        <TIME from="5pm" to="9pm" />
    </CONDITION>
    <THEN>
        <...some actions defined.../>
    </THEN>
    <ELSE>
        <...other set of actions defined here.../>
    </ELSE>
</IF>

Seems easier to read to me. There's more nesting, but if anything that helps with the readability?

对我来说似乎更容易阅读。还有更多的嵌套,但是否有任何有助于提高可读性的内容?

回答by MarcoS

Personally, I would prefer

就个人而言,我更喜欢

<IF>
  <TIME from="5pm" to="9pm" />
  <THEN>
    <!-- action -->
  </THEN>
  <ELSE>
    <!-- action -->
  </ELSE>
</IF>

In this way you don't need an idattribute to tie together the IF, THEN, ELSEtags

通过这种方式,您不需要一个id属性来将IF, THEN,ELSE标签联系在一起

回答by Bryce Siedschlaw

<IF>
    <CONDITIONS>
        <CONDITION field="time" from="5pm" to="9pm"></CONDITION>
    </CONDITIONS>
    <RESULTS><...some actions defined.../></RESULTS>
    <ELSE>
        <RESULTS><...some other actions defined.../></RESULTS>        
    </ELSE>
</IF>

Here's my take on it. This will allow you to have multiple conditions.

这是我的看法。这将允许您拥有多个条件。

回答by Brian Driscoll

I think that the thing you must keep in mind is that your XML is being processed by a machine, not a human, so it only needs to be readable for the machine.

我认为您必须牢记的是,您的 XML 是由机器而非人类处理的,因此它只需要机器可读即可。

In other words, I think you should use whatever XML schema you need to make parsing/processing the rules as efficient as possible at run time.

换句话说,我认为您应该使用您需要的任何 XML 模式,以便在运行时尽可能高效地解析/处理规则。

As far as your current schema goes, I think that the idattribute should be unique per element, so perhaps you should use a different attribute to capture the relationship among your IF, THEN, and ELSEelements.

至于你当前的模式去,我认为id属性应该是每个元素唯一的,所以也许你应该使用不同的属性来捕捉你之间的关系IFTHENELSE元素。

回答by Matthias

<IF id="if-1">
   <TIME from="5pm" to="9pm" />
<ELSE>
   <something else />
</ELSE>
</IF>

I don't know if this makes any sense to anyone else or it is actually usable in your program, but I would do it like this.

我不知道这对其他人是否有意义,或者它实际上可用于您的程序,但我会这样做。

My point of view: You need to have everything related to your "IF" inside your IF-tag, otherwise you won't know what ELSE belongs to what IF. Secondly, I'd skip the THEN tag because it always follows an IF.

我的观点:您需要在 IF 标签中包含与“IF”相关的所有内容,否则您将不知道 ELSE 属于什么 IF。其次,我会跳过 THEN 标签,因为它总是跟在 IF 之后。