声明

时间:2019-03-11 13:06:29

标签: xslt

当我检查代码时,我在单个测试语句中看到了多个条件。它没有给我正确的结果。

<xsl:choose>
<xsl:when test="(ws:Additional_Information/ws:Company/@ws:PriorValue = 'A' or 'B' or 'C' or 'D' or 'E')and ws:Eligibility='false'">
<xsl:text>T</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="ws:Additional_Information/ws:Employee_Status"/>
</xsl:otherwise>
</xsl:choose>

但是当我开始像下面这样使用时,我得到了正确的答案。

<xsl:choose>
<xsl:when test="(ws:Additional_Information/ws:Company/@ws:PriorValue = 'A' or ws:Additional_Information/ws:Company/@ws:PriorValue ='B' or ws:Additional_Information/ws:Company/@ws:PriorValue ='C' or ws:Additional_Information/ws:Company/@ws:PriorValue ='D' or ws:Additional_Information/ws:Company/@ws:PriorValue ='E')and ws:Eligibility='false'">
<xsl:text>T</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="ws:Additional_Information/ws:Employee_Status"/>
</xsl:otherwise>
</xsl:choose>

为什么第一个不正确?

2 个答案:

答案 0 :(得分:2)

为简化起见,您的第一个“测试”表达式具有以下形式

@attr = 'A' or 'B'

在某些语言中,甚至不允许这样的语法。实际上与执行此操作相同:

(@attr = 'A') or ('B')

您正在问“ 表达式attr = 'A'是真的还是表达式'B'?”。在XSLT中,询问“ 表达式'B'是否为真?”实际上将返回true,因为字符串'B'为非空。因此,无论属性值如何,整个表达式将始终为真。

所以,您必须在这里写@attr = 'A' or @attr = 'B'

如果您正在寻找一些较短的语法,并且可以使用XSLT 2.0,则可以这样编写:

@attr = ('a', 'b')

这就像在说“ @attr等于序列中的任何值吗?

答案 1 :(得分:2)

第一个表达式的问题是or operator是逻辑运算符,而不是序列连接器。因此,该表达式@ws:PriorValue = 'A' or 'B'毫无意义。

在XPath 1.0中,用于简化同一节点集上的多个比较的一个惯用法是使用点.表达式,如下所示:

ws:Additional_Information
   /ws:Company
      /@ws:PriorValue[.='A' or .='B' or .='C' or .='D' or .='E']
and ws:Eligibility='false'

另一种方法是像这样使用contains function

ws:Additional_Information
   /ws:Company
      /@ws:PriorValue[
         contains(' A B C D E ', concat(' ',.,' '))
      ]
and ws:Eligibility='false'