Defining an XPath conditional expression for a transform
You can define an XPath expression to set the conditional expression that determines whether a transform is applied in a message map. When the XPath expression evaluates to true, the transform is applied.
About this task
When you define an XPath expression, you use the variable names of input elements. The value of an input element has an effective Boolean value.
- The input element is Boolean and its value is false.
- The input element is a sequence, and the sequence is empty.
- The input element is string and its value is the empty string "".
- The input element is a float or a decimal and the value is NaN (not a number).
- The input element is of a numeric type and its value is 0.
- Logical operators such as and, or, not.
- Comparison operators such as =, !=, <, >, <=, >=.
Procedure
Complete any of the following steps to set a conditional expression in a transform:
Results
The input element is evaluated against the condition. If the condition evaluates to true, the transform is applied to the input element.
Example
The following examples show how to define simple XPath conditional expression for a transform when you have non-repeating elements:
Example: XPath expression to check a Boolean input element
This example shows how to define the XPath expression for a Boolean element so that it evaluates to true. The expression depends on the value of the Boolean element.
The XML schema for the element is the following:
<element
name="IsEmployee" type="boolean"></element>
When the value of a Boolean element is set to false, the XPath expression that you define is the following:
fn:not($IsEmployee)
When the value of the Boolean element is set to true, the XPath expression that you define is the following:
$IsEmployee=fn:true()
The $IsEmployee expression on its own tests if the element exists and always evaluates to true if the element exists regardless of what value it is set to.
Example: XPath expression to check if the value of a numerical input element is greater than a constant value
This example shows how to define an XPath expression that evaluates to true when the value of a numerical element is greater than 200.
The XML schema for the element is the following:
<element name="BusinessUnit"
type="int" ></element>
The XPath expression that you define is the following:
$BusinessUnit > 200
Example: XPath expression to check if a numerical input element has a specific value
This example shows how to define an XPath expression that evaluates to true when the numerical input element has a value of 0.
The XML schema for the element is the following:
<element name="QtyBooks"
type="int" ></element>
The XPath expression that you define is the following:
$QtyBooks = 0
Example: XPath expression to check the string length of an input element
This example shows how to define an XPath expression that evaluates to true when the length of a string is 10.
- The Move transform has a primary connection wired from the element Name.
- The Move transform has a secondary connection wired from the element BusinessUnit. This connection is supplementary because this element is not used to calculate the value of the output element.
- The Move transform should only execute if the length of the element BusinessUnit is 10.
The XML schema for the elements are the following:
<element
name="Name" type="string" ></element>
<element
name="BusinessUnit" type="string" ></element>
The XPath expression that you define is the following:
fn:string-length($BusinessUnit) = 10
Example: XPath expression to check if a string input element is set to the empty string
This example shows how to define an XPath expression that evaluates to true when a string element is empty.
To define an XPath expression that checks if a string element is empty, you must use the operator !=.
The XML schema for the element is the following:
<element
name="Name" type="string" ></element>
The XPath expression that you define is the following:
$Name
!= ''
Example: XPath expression to check if a repeating element is empty
This example shows how to define an XPath expression that evaluates to true when a repeating element, which is referred to as a sequence, is empty.
The effective Boolean value
of an empty sequence is false. You could use fn:not($Address)
,
but it is easier to understand if you use fn:empty()
.
The XPath function fn:empty() evaluates to true if a sequence is empty.
The XML schema for the element is the following:
<xsd:element form="qualified" name="Address" type="mqsistr:Address"
maxOccurs="unbounded" minOccurs="0" />
The XPath expression that you define is the following:
fn:empty($Address)
Example: XPath expression to check if an optional input element is present
This example shows how to define an XPath expression that evaluates to true when an optional element is present.
Use
the fn:exists
XPath function if the input element
is a boolean. Otherwise, you can use the effective Boolean
value.
The XML schema for the element is the following:
<element name="BookName" type="string" maxOccurs="unbounded"
minOccurs="0" ></element>
The XPath expression that you define is the following:
$BookName
Example: XPath expression to check if a complex input element has no content, that is, it is empty
To determine whether a complex element is empty, you must check for the presence of child elements or attributes.
Testing the effective Boolean
value of elements or attributes that are present in a complex
type will yield true. You can use the fn:not
XPath
function to invert a boolean.
The fn:not
function
accepts a sequence of items. The value that this function
returns is true if any of the arguments is either a single Boolean
value false, a zero-length string, the number 0 or NaN, or
the empty sequence. Otherwise, it returns false.
This example shows how to define an XPath expression that evaluates to true when the complex input element has no children.
The XML schema for the elements are the following:
<complexType name="Address">
<sequence>
<element name="Type" type="string"/>
<element name="Number" type="integer"/>
<element name="Street" type="string"/>
<element name="Postcode" type="string"/>
<element name="City" type="string" />
<element name="Country" type="string"/>
<element name="AdditionalInfo" type="string"/>
</sequence>
</complexType>
The XPath expression that you define to check for child elements being present in a complex element is the following:
fn:not($Address/*
)
The XPath expression that you define to check for attributes being present in a complex element is the following:
fn:not($Address/@* )
The XPath expression that you define to check for elements and attributes being present in a complex element is the following:
fn:not($Address/*)
and fn:not($Address/@* )
What to do next
Deploy and test the message map. For more information, see Troubleshooting a message map.