【问题标题】:xslt check multiple conditions and then divide value by attribute attributexslt 检查多个条件,然后将值除以属性属性
【发布时间】:2016-03-19 00:40:24
【问题描述】:

我在for-each 中使用ChooseWhen 来执行多个条件检查

例如下面简单检查某个节点的和,检查值是否小于50

<xsl:when test="sum(scores/score) &lt; 50">
    <td>
       <xsl:text>A1</xsl:text>        
    </td>
</xsl:when>

我有五个,它们运行良好,除了最后一个进行多次检查。

我需要检查这两个条件是否都满足

  • 总和大于某个数字,例如 20
  • score 的属性@mandatory 等于'True' 至少有一个score

如果这两个条件都满足,我需要运行另一个条件

  • score的值除以另一个属性weight
  • 如果结果小于 0.5,则使用&lt;xsl:text&gt; 将结果打印到表中

我试过了

<xsl:when test="sum(scores/score) &gt; 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) &gt; 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) &lt; 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


【解决方案1】:

假设上下文元素是scores 的父元素,那么下面的 XPath 表达式应该可以测试前两个项目符号条件:

sum(scores/score) &gt; 20 and scores/score/@mandatory='True'

然后接下来的两个项目符号条件可以转换为 XPath,如下所示:

scores/score[. div @weight &lt; 0.5]

演示:http://xsltransform.net/bFN1ya7

【讨论】:

  • 有趣的是,我之前尝试过第一个表达式,但无法让它工作。将它移到&lt;xsl:when&gt; 的顶部,现在一切都很好。我的问题是划分score@weight 应该是第一个表达式的子条件。所以基本上在检查它是否大于20和@mandatory = True之后,它应该在score@weight之间进行划分
  • 如果我在第一个表达式后面加上另一个 and,然后输入第二个表达式。它似乎没有评估表达式。如果我在when 内嵌套&lt;xsl:If test="scores/score[. div @weight &amp;lt; 0.5]",那么它似乎执行了表达式但不打印任何内容。即使&lt;xsl:text 嵌套在&lt;xsl:if&gt;
  • 知道了!其中一个节点名称有一个简单的拼写错误。非常感谢。今晚晚些时候我会发布代码。再次感谢。我最终在一行上完成了它,但可能会将其移动到嵌套的 &lt;xsl:if&gt; 以提高可读性
  • 这是一个班轮&lt;xsl:when test="sum(scores/score) &amp;gt; 20 and scores/score/@mandatory='True' and scores/score[. div @weight &amp;lt; 0.5]"&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-30
  • 1970-01-01
  • 2022-01-17
  • 2021-09-15
  • 2021-06-17
相关资源
最近更新 更多