Show TOC

Creating ExpressionsLocate this document in the navigation structure

Prerequisites

You have opened the Process Development or the Service Composer perspective in the SAP NetWeaver Developer Studio and have expanded your project in Project Explorer view .

Context

The expression is a combination of values, variables and functions that are evaluated at runtime to produce a new value. You create expressions in an expression editor using a subset of the XML Path Language (XPath) grammar. For more information about Xpath, see http://www.w3.orgInformation published on non-SAP site.

The expression editor contains a context and functions. The context contains all the data, relevant for the expression scope. You use the functions to process the data from the context.

You can use comments in the expression editor. Comments delimited in the following way are supported:

  • // some text

    The text after the // delimiter until the end of the line is part of the comment.

  • /* some text */

    The text, which can consist of multiple lines, between the delimiters /* */ is part of the comment.

After creating an expression, you can extract it to create a new function directly from the expression editor. For more information about creating functions, see Performing Complex Data Transformation with Functions .

Using Functions from Different DCs

In the expression editor, you can also use functions that were created in different development components (DC) than the one you create your project in. To do that, you proceed as follows:

  1. You configure dependencies to the DC containing the function you want to use. For more information about dependencies, see Defining Development Component Dependencies .

  2. You use the function in the expression editor:

    1. If you type the function's name in the editing area of the expression editor, you have to define the namespace prefix of the DC containing the function you want to use.

    2. If you drag the function from the Rules and Functions node to the editing area of the expression editor or double-click that function from the Rules and Functions node, an import statement for its namespace is automatically generated in the editing area.

      Example

      You want to use the function "ExampleFunction" , created in the project "my.company.com/myProject1" . You define the namespace prefix of the DC containing the project in the following way:

                                 xmlns:myProject1="my.company.com/myProject1"
                              
      Example

      You use the function "ExampleFunction" after you defined the namespace prefix, in the following way:

                                 myProject1:ExampleFunction( argument1,argument2... )
                              

When you use functions created in the current DC, you can omit the namespace prefix.

Note

When you apply changes to an expression or a mapping function defined in one DC and referenced in other DCs, you must rebuild and redeploy all referencing DCs.

Casting Data Types

You can use the cast as function in case you want to process one data type as another. With the cast as function you define how the expression editor must treat the original data type when it evaluates the expression.

Example

The required data type is xsd:byte but you provide myDecimalRef as xsd:decimal. You cast xsd:decimal as xsd:byte in the following way:

               myDecimalRef cast as xsd:byte
            

When you cast data types, you have to do that in compliance with the World Wide Web Consortium data type hierarchy. For more information about the data type hierarchy, see http://www.w3.orgInformation published on non-SAP site.

Creating Conditional Statements

You can use a conditional statement in case you want to have different actions depending on the evaluation of a given expression. You can create this statement using the keywords if , then and else in the editing area of the expression editor:

               if (<ExpressionOne>) then <ExpressionTwo> else <ExpressionThree>
            

The <ExpressionOne> is a conditional expression and must have a return type Boolean:

  • if it is true - the value of the second expression, <ExpressionTwo> , is returned

  • if it is false - the value of the third expression, <ExpressionThree> , is returned

If <ExpressionOne> is evaluated to true, then any errors in <ExpressionThree> encountered at runtime are ignored. Similarly, if <ExpressionOne> is evaluated to false, then errors in <ExpressionTwo> encountered at runtime are ignored.

Note

The return value of a conditional if statement depends on the type of the value returned after the evaluation of <ExpressionTwo> or <ExpressionThree> . If these types are different, then the return type of the conditional if statement would be the least common multiple (LCM) of the two types. The LCM is the first common parent of those types in the data type hierarchy. For more information about the data type hierarchy, see http://www.w3.orgInformation published on non-SAP site.

Following are expressions describing the different usage of conditional statements:

  • The expected results of these two expressions are of type String:

    Sample Code
                            if (true) then c else d 
    if (a>b) then c else d 
    
                         
  • The example below uses the following context:

                         Data_Object_0
            Priority ( xsd:int )
            actualOwner ( glx:principal )
            initiator ( glx:principal )
    
                      
    Sample Code
                            principal/if (type=1) then "type=1" else "type!=1"
                         
  • The example below uses the following context:

                         Data_Object_0
            Priority ( xsd:int )
            actualOwner ( glx:principal )
            initiator ( glx:principal )
    
                      
    Sample Code
                            Data_Object_0/(if (priority=1) then actualOwner else initiator)/principalId
                         

Procedure

  1. Open the expression editor.
  2. Expand Rules and Functions , containing the type of functions you want to use to process the data.

    You can use both the built-in functions and functions you have created in your project, or other existing functions in different development components.

    Note

    A list with all built-in functions and their descriptions is available in the expression editor. To show the list, click the empty editing area of the expression editor and press CTRL + SPACEBAR . The list with all functions appears. To show the description of a function, click the function's name. The description appears on the right of the list. You can also place a function in the editing area by double-clicking the function's name in the list.

    For more information about built-in functions, see Built-In Functions and Generic Functions .

    For more information about EJB functions and using them as mapping functions, see Creating an External Function, Exposing It as an EJB, and Using It as a Mapping Function .

  3. Drag a function or functions from the Rules and Functions node in the expression editor to the editing area of the expression editor.

    Alternatively, double-click a function from the Rules and Functions node in the expression editor. It appears in the editing area of the expression editor.

  4. Expand Context and drag the necessary data that is processed by the function to the editing area of the expression editor.

    You can also enter data that is different from the data in Context directly in the editing area.

    Note

    Each function in Rules and Functions contains information about what data it expects and processes. This information is written in parentheses ((...)).

  5. Use the XPath grammar to create a valid expression using the functions and the data. Choose the OK pushbutton.
    Note

    An error marker next to the expression means that the expression has an error and cannot be evaluated. Errors in expressions are visible in the expression editor and in the Problems view.

    After the data transformation, that is, after the expression is evaluated, a new value is produced (principal, Boolean value and so on).

  6. (Optional) Click Extract Function... to extract the expression you created as a new function.
  7. (Optional) In the dialog that appears, specify a name for the new function. Choose the OK pushbutton.

    The new function is extracted in the Functions folder of your project.

Example

Here is a list of examples for creating expressions in the expression editor.

The context to use in the examples is the following:

            SourceRoot1
        SubRoot
                mInt( xsd:int )
                mInt1( xsd:int )
         
            SourceRoot2
        mInt2( xsd:int )
        mStr( xsd:string )
         
  • In this example, the data in the context is of type integer and the expected result after the expression is evaluated is also integer (xsd:int):

                      SourceRoot1/SubRoot/mInt + SourceRoot2/mInt2
                   
  • In this example, the data in the context is of type integer and the expected result after the expression is evaluated is String (xsd:string). The expression converts the xsd:int into xsd:string:

                      string( SourceRoot1/SubRoot/mInt )
                   
  • In this example, the data in the context is of type String and the expected result after the expression is evaluated is also String:

                      concat( "Prefix ", concat( SourceRoot2/mStr, " Sufix") )
                   
  • This example shows the usage of list of function arguments. The data used is of type String and the expected result is also String. The list is defined in the following way:

                      ( "ElementOne", "ElementTwo", "ElementThree" )
                   

    The defined list is used as function argument in the following way:

                      myFunc( ( "ElementOne", "ElementTwo", "ElementThree" ) )
                   
  • The following is an example of an expression, which returns a Boolean value:

                      not( true AND 1<=2 )