【发布时间】:2016-03-19 00:40:24
【问题描述】:
我在for-each 中使用Choose 和When 来执行多个条件检查
例如下面简单检查某个节点的和,检查值是否小于50
<xsl:when test="sum(scores/score) < 50">
<td>
<xsl:text>A1</xsl:text>
</td>
</xsl:when>
我有五个,它们运行良好,除了最后一个进行多次检查。
我需要检查这两个条件是否都满足
- 总和大于某个数字,例如 20
-
score的属性@mandatory等于'True'至少有一个score
如果这两个条件都满足,我需要运行另一个条件
- 将
score的值除以另一个属性weight - 如果结果小于 0.5,则使用
<xsl:text>将结果打印到表中
我试过了
<xsl:when test="sum(scores/score) > 20 and @mandatory='True'">
<td>
<xsl:text>F1</xsl:text>
</td>
</xsl:when
这甚至无法进行第二次检查 mandatory 是否等于 True
即使我使用路径scores/score/@mandatory,结果也是一样的
我尝试将第二个条件移动到嵌套在 when 内的 if,但这也失败了
<xsl:when test="sum(scores/score) > 20">
<xsl:if test="@mandatory='True'" // even if I use scores/score/@mandatory
<td>
<xsl:text>F1</xsl:text>
</td>
</xsl:when
所以基本上它在第一关就失败了。我要实现的另一个条件是将scores/score 除以scores/score/@weight
我正在考虑但还没能到那里去玩它
<xsl:if test="(scores/score) div (scores/score/@weight) < 0.5">
// print result into table
</xsl:if>
这里是一些与 xslt 相关的示例 xml
<scores>
<score no="1" weight="10" mandatory="False">8</score>
<score no="2" weight="20" mandatory="True">13</score>
</scores>
只是一个额外的说明。一个scores 节点可能有两个以上的score 元素。此外,mandatory 属性不一定必须具有"True" 值,同样可能有多个元素具有mandatory="true" 属性。
以下 xml 示例有效
多个mandatory="true"
<scores>
<score no="1" weight="10" mandatory="False">8</score>
<score no="2" weight="20" mandatory="True">13</score>
<score no="3" weight="50" mandatory="True">35</score>
</scores>
没有mandatory="true"
<scores>
<score no="1" weight="10" mandatory="False">8</score>
<score no="2" weight="20" mandatory="False">13</score>
<score no="3" weight="50" mandatory="False">35</score>
</scores>
【问题讨论】:
-
"
score的属性@mandatory等于'True'"。这种情况是否必须是:a。由所有score或b 遇到。至少遇到了一位score? -
@har07 感谢您提及这一点,我已在问题中添加了相关的业务逻辑。条件必须满足至少一个分数
-
好的,这部分现在很清楚了,谢谢。接下来,"如果结果小于 0.5 ..."。同样,这是否必须适用于所有
score/weight比率,或者至少一个? -
@har07 至少一次
标签: xml xslt xpath conditional-statements