单个XSLT when语句中的多个条件

单个XSLT when语句中的多个条件,xslt,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: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>

为什么第一个表达式不正确?

为了简化,第一个测试表达式的形式如下

@attr = 'A' or 'B'
在某些语言中,甚至不允许使用这种语法。这实际上与执行此操作相同:

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

因此,您必须在这里写入@attr='A'或@attr='B'

如果您正在寻找一些较短的语法,如果您可以使用XSLT 2.0,则可以编写以下代码:

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

这就像说@attr是否等于序列中的任何值一样?

第一个表达式的问题是它是一个逻辑运算符,而不是序列连接符。所以这个表达式@ws:PriorValue='A'或'B'毫无意义

在XPath1.0中,简化同一节点集上的多个比较的习惯用法是使用点。表达方式如下:

ws:Additional_Information
   /ws:Company
      /@ws:PriorValue[.='A' or .='B' or .='C' or .='D' or .='E']
and ws:Eligibility='false'
ws:Additional_Information
   /ws:Company
      /@ws:PriorValue[
         contains(' A B C D E ', concat(' ',.,' '))
      ]
and ws:Eligibility='false'
另一个是这样使用:

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