A condition in a return type defines two groups of elements: those returned when an expression is true, and those returned when an expression is false. When you add a condition to a return type, you see two groups of return type elements.
To add a condition to the return type:
Click the Query Map tab.
Right-click an element in the return type, and choose Make Conditional.
The conditional element is now duplicated.
Create the Expression
You must add the conditional expression, that determines which element is returned, in the XQuery source. You cannot add a conditional expression in the expression editor.
Click the Source tab.
The primary Read function now has an if..else clause:
declare function tns:read() as element(cus:CustomerOrder)*{
for $CUSTOMER in cus1:CUSTOMER()
returnif (true()) then
<cus:CustomerOrder>
...
</cus:CustomerOrder>
else
<cus:CustomerOrder>
...
</cus:CustomerOrder>
};
The expression after the if statement is evaluated, and the service returns either the first or second set of elements. The XQuery true() function simply returns the Boolean value true.
In the XQuery source, replace true() with another XQuery expression, for example:
if ( fn:data( $CUSTOMER/LAST_NAME ) = "Black" ) then
You can use any XQuery expression that returns a value of true or false. In this example, if a customer has the last name Black, the first element group is returned. If not, the second element group is returned.
To add the value of an element in a For block, use the XQuery fn:data function, which takes the value of an element:
<LAST_NAME>Black</LAST_NAME>
Click the Query Map tab.
In the return type, add or delete elements in either group to create the return groups you want.
Remember that the first group is returned if the expression is true, and the second group if the expression is false.
Click the Test tab. Choose the Read function, and click Run. Check that the results are what you intend.
In this example, the full group of elements is only returned for customers with the last name Black. For other customers, only the CUSTOMER_ID is returned.