Peter
Peter

Reputation: 1796

Conditional AND OR two different values

I have a concatenation of AND and OR for 2 different variables. And I tried the following:

<xsl:if test="$countryCode = '104'
               and 
              (transactionType != 'Allowance'
                or 
               revenueCenter != '1100')">

But that does not work. Is it possible to do a conditional test or do I have to split it up like this:

<xsl:if test="$countryCode='104'>

and in a second element I do:

<xsl:if transactionType!='Allowance' or revenueCenter!='1100'>

Upvotes: 8

Views: 49417

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243459

The XPath expression:

    $countryCode='104' 
   and  
    (transactionType!='Allowance' or revenueCenter!='1100')

is syntactically correct.

We cannot say anything about the semantics, as no XML document is provided and there is no explanation what the expression must select.

As usual, there is the recommendation not to ose the != operator, unless really necessary -- its use when one of the arguments is a node-set (or sequence or node-set in XPath 2.0) is very far from what most people expect.

Instead of != better use the function not():

  $countryCode='104' 
 and  
  (not(transactionType='Allowance') or not(revenueCenter='1100'))

and this can further be refactored to the shorter and equivalent:

  $countryCode='104' 
 and  
  not(transactionType='Allowance' and revenueCenter='1100')

Upvotes: 14

Related Questions