Making XQuery inputs optional (OSB)

In this post..  

I hope to describe how to make input items on an XQuery mapping callout optional, and possible ways of handling the situation.

Possible uses:

  • You are creating an output XML document with an optional field, and are passing this is passed in as a variable on OSB.

Lets Begin..

First you will need to create an XQuery mapping, mine takes in a few Variables, and then populates a document with some optional fields, it looks as follows: 


To make a input value optional, you need to go to the source tab.

Note: Based on personal experience, the design tab is good at creating a simple mapping, but as soon as they become more complex, with if's and for's the source code is the best place to go. I'v spent hours writing some source code, only for it to be overwritten by a change on something completely irrelevant on the design screen. 

Within the source tab, to make an input field optional you need to do two things. 
  1. Set the input variable to be optional, this is done using a question mark after the element type, look at the red boxes on the image below. 
  2. Doing a check to see if the variable even exists before creating the element - If this is not done, the XQuery will still result in an error if a null value is passed in. 
Both of these are shown in the image below: