You are at Mapping Guidelines (MAG)
When you map a source group node to a target group node. And then map a source leaf node to the same target group node. It automatically creates a "conditional mapping". What is this feature? Let's have a look at it.
As defined on
the help page:
- Group node: A node that has subnodes and doesn't contain data. It can also be a subnode to another group node.
- Leaf node: A subnode that contains data.
Expression
First of all, you don't write a piece of XSLT in the function, you write an
XPath Expression that returns true/false (xs:boolean).
Friendly comment style is a reminder that you are writing an XPath expression.
(: This is an comment inside XPath expression 🙂
XPath can convert values like string or number to boolean implicitly. This behavior is explained in XLST Specification under
Effective Boolean Value
<xsl:if test=" 'a string value' ">
<!-- returns true -->
</xsl:if>
However, for the outermost function/value of expression, Integration Advisor wants explicit conversion. This doesn't work:
$nodes_in/LEAF1
(: This won't work, but error messages are helpful to guide you 🙂
(: Failed to simulate MAG: The string "10.000" cannot be cast to a boolean 🙂
Using explicit function "boolean()" works. This also works:
not( $nodes_in/LEAF1 )
Behavior
Think about it as it creates the following "xsl:if" expression for each source group node & related leaf value(s).
And you write the expression inside the test attribute, for example:
$nodes_in/QUALF = 'expected value'
Pseudo XSLT generated:
<!-- Check expression using the leaf nodes under the FIRST group node -->
<xsl:if test="$nodes_in/QUALF = 'expected value' ">
<!-- Create target node with the context of FIRST group node -->
</xsl:if>
<!-- Check expression using the leaf nodes under the SECOND group node -->
<xsl:if test="$nodes_in/QUALF = 'expected value' ">
<!-- Create target node with the context of SECOND group node -->
</xsl:if>
<!-- Check expression using the leaf nodes under the THIRD group node -->
<xsl:if test="$nodes_in/QUALF = 'expected value' ">
<!-- Create target node with the context of THIRD group node -->
</xsl:if>
I haven't checked the actual generated XSLT output, but this fits the behavior.
Example: Filtering IDoc segments based on fields
While you can already use qualified nodes (and it is easier to use qualified nodes when you can), thinking about qualifiers is the best way to understand the concept.
The main difference is that you can use multiple leaf values that can't be qualified, and you can write custom XPath "code" for flexibility.
Note that only 1 instance of the target group is created. I was expecting 2 as it shows "Gross Price" on the left node and "Net Value for Item" at the bottom.
The reason I wrote this blog post is that I was preparing to ask a question, and prepared everything. It turns out it is just a UI bug. When you download the output payload, everything works as expected. When in doubt, download the payload.
Some ideas to use inside the expression
Comparison operators
$nodes_in/LEAF1 = 10
Alternative syntax and string comparison
$nodes_in/LEAF1 eq '10.000'
Note that "=" style and "eq" style have a difference.
$nodes_in/LEAF1 eq 10
(: Error: Failed to simulate MAG: Cannot compare xs:untypedAtomic to xs:integer
Operators "=, <, <=, >, >=" does implicit conversation to number.
You can use:
number($nodes_in/LEAF1) eq 10
Related spec:
If the comparison operator is <, <=, >, or >=, then each item in both of the operand sequences is converted to the type xs:double by applying the fn:number function.
:)
I really like checking the value inside a list
$nodes_in/LEAF1 = ('10.000','5.000')
Logical expressions
You can use "and/or" operators.
$nodes_in/LEAF1 = '10.000' or $nodes_in/LEAF1 = '5.000'
(: This is the same as->
$nodes_in/LEAF1 = ('10.000','5.000')
:)
As it is hard to remember precedence rules, I recommend using parentheses for complex logic.
( $nodes_in/LEAF1 > 10 and $nodes_in/LEAF1 < 20 ) or $nodes_in/LEAF1 = 23
If-Then-Else
If-Then-Else can be used for complex selection on a set of possibilities. It also supports "else if":
if ($nodes_in/LEAF1 > 😎
then true()
else if ($nodes_in/LEAF1 > 4)
then false()
else if ($nodes_in/LEAF1 > 2)
then true()
else false()
You can nest If-Else statements:
if ($nodes_in/LEAF1 > 😎
then true()
else if ($nodes_in/LEAF1 > 4)
then (
if ( $nodes_in/LEAF1 = 5.5 ) then true() else false()
)
else if ($nodes_in/LEAF1 > 2)
then true()
else false()
Built-in XLST functions
You can use supported XSLT 2.0 functions
listed in this blog post.
starts-with( $nodes_in/LEAF1 , '6.0' )
You can mix and match all of the above.
Further reading
Gunther Stuhecs's blog post
Integration Advisor – MAG: How to create If-Then-Else Statements in Mapping Elements? explains how you can use conditional expressions in XSLT. Some of the examples can also be used inside a conditional mapping.
Related
help page.
XPath 2.0 specification.