8
votes

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?

5

5 Answers

10
votes

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>
2
votes

This is my solution.

<filter source="boolean(get-property('yourProperty'))='null'" regex="false">
    <then>           
           <!-- NULL OR NON EXIST -->
    </then>
    <else>
           <!-- EXIST -->
    </else>
</filter>
0
votes

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

0
votes

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>
0
votes

This solution also works.

<filter source="boolean(get-property('yourProperty'))" regex="true">
    <then>
           <!-- EXIST -->
    </then>
    <else>
           <!-- NULL OR NON EXIST -->
    </else>
</filter>