9

I want to check if a property is null in a filter condition. It might not exist in the context or it can be null as well. I want to cover both cases. How can I write a filter expression? I tried something like the code below, but it's not working

<filter xpath="(get-property('studentId')!=null)">

What is the correct way to achieve this?

Community
  • 1
  • 1
Janier
  • 3,982
  • 9
  • 43
  • 96

7 Answers7

11

You can check the existence of property by using boolean XPath function as below

<filter source="boolean(get-property('yourProperty'))" regex="false">
    <then>
           <!-- NULL OR NON EXIST -->
    </then>
    <else>
           <!-- EXIST -->
    </else>
</filter>
Chandana
  • 2,578
  • 8
  • 38
  • 55
2

This is my solution.

<filter source="boolean(get-property('yourProperty'))='null'" regex="false">
    <then>           
           <!-- NULL OR NON EXIST -->
    </then>
    <else>
           <!-- EXIST -->
    </else>
</filter>
David Buck
  • 3,752
  • 35
  • 31
  • 35
2

This solution covers a missing field, null field and empty field validation:

<filter regex="true" source="get-property('fieldName') = 'null' or get-property('fieldName') = ''">
        <then>
            [.. error ..]
        </then>
        <else/>
    </filter>
user666
  • 1,750
  • 3
  • 18
  • 34
1

This solution also works.

<filter source="boolean(get-property('yourProperty'))" regex="true">
    <then>
           <!-- EXIST -->
    </then>
    <else>
           <!-- NULL OR NON EXIST -->
    </else>
</filter>
Pradeep Sanjeewa
  • 1,911
  • 1
  • 14
  • 25
0

Use below code snippet

    <api xmlns="http://ws.apache.org/ns/synapse" name="TestFilter" context="/test1">
   <resource methods="POST" url-mapping="/filter">
      <inSequence>
         <filter xpath="$body//*[local-name()='FilterCondition']/text()">
            <then>
               <log>
                  <property name="ThenCondition" expression="."/>
               </log>
            </then>
            <else>
               <log>
                  <property name="ElseCondition" expression="."/>
               </log>
            </else>
         </filter>
      </inSequence>
   </resource>
</api>

Try with the sample XML

 <Check>
<FilterCondition>123</FilterCondition>

</Check>

So your test case should be as below

1: Use the same XML, the flow should go to then condition. 2: Pass empty value in FilterCondition like <FilterCondition/>, this should go to else condition. 3: remove the tag FilterCondition and just pass

<Check></Check>

, this should again go to else condition

amg_amit
  • 495
  • 1
  • 3
  • 9
0

You can use:

<property
expression="$body//*[local-name()='Address']/text()"
name="Address" scope="default" type="STRING"/>

and then:

<filter regex="Tehran" source="get-property('Address')" xmlns:ns="http://org.apache.synapse/xsd">
    <then/>
    <else>
        <log/>
        <send/>
    </else>
</filter>
Masoud Keshavarz
  • 2,166
  • 9
  • 36
  • 48
  • 2
    Usually it's better to explain a solution instead of just posting some rows of anonymous code. You can read [How do I write a good answer](https://stackoverflow.com/help/how-to-answer), and also [Explaining entirely code-based answers](https://meta.stackexchange.com/questions/114762/explaining-entirely-%E2%80%8C%E2%80%8Bcode-based-answers) – Anh Pham Dec 25 '18 at 12:07
  • This does not answer the original question of checking a property for null value. You are checking a property you know that exists (you set it yourself) against a specific value. – ophychius Dec 26 '18 at 09:06
0
<template name="TEMPLATE_NAME" xmlns="http://ws.apache.org/ns/synapse">
<parameter name="VAR1"/>
<parameter name="VAR2"/>

 <property expression="$func:VAR1" name="REFERENCE_VAR1" scope="default" type="STRING"/>
<filter regex="true" source="boolean(boolean($func:VAR1 = 'null')   
                     and boolean($func:VAR2 = 'null')   
                     and boolean($func:VAR3 = 'null')    
                     and boolean($func:VAR4 = 'null'))">
    <then>   
         <log category="INFO" level="custom">
           <property expression="$ctx:REFERENCE_VAR1" name="All_NULL"/>
         </log>
    </then>
    <else> 
        <log category="INFO" level="custom">
           <property expression="$ctx:REFERENCE_VAR1" name="NORMAL"/>
         </log>
    </else>   
</filter>

Hope this helps