I recently started in a project working with the Oracle Service Bus (release 10gR3) and stumbled upon some issues regarding XQuery transformations.
I should note I did not work previously with XQuery, so it is all new to me.
But when trying to use some builtin functions I noticed that some that not work or did not work as expected, so I created some functions on my own and searched on the internet for some solutions.

One site which was useful for me was the FunctX XQuery Function Library (http://www.xqueryfunctions.com/xq/).
Nilled elements
One issue was how do you check if an element is nilled (“xsi:nil=true”). There is a builtin function avaible: fn:nilled. But I appeared that this function did not work (at least not within the Oracle Service Bus). So I needed to look for a solution. The following XQuery function will provide you with the expected result:
declare function xf:is_nilled
  ( $element as element() )  as xs:boolean {

   if (fn:exists($element/@xsi:nil)) then
      fn:exists($element[@xsi:nil="true"])
   else
      fn:false()
 };
Note: xf is the alias for the local used main namespace; xsi is the alias for the XML Schema Instance namespace.
Empty elements
Another issue was how to check for empty elements. For this there are several solutions, but the builtin function fn:empty does not work.
One solution is to use a function from the FunctX XQuery Function Library:
declare namespace functx = "http://www.functx.com"; 

declare function functx:has-empty-content
  ( $element as element() )  as xs:boolean {

   not($element/node())
 } ;
And of course you can also check manually:
{
  let $var := ${path}
  return
    if (fn:string-length($var) > 0) then
       ... 
    else
      ()
};
If both nilled and empty elements are allowed, you can combine both XQuery functions into a new single function:
declare function xf:isEmptyOrNilled
  ( $element as element() )  as xs:boolean {

   functx:has-empty-content($element) or xf:is_nilled($element)
 };
Type casts
When a type cast must be performed with the XQuery transformation you should always check for empty element. Because if you don’t, the transformation will return an error stating the type cast can not be made.
For example when transforming from datetime to date:
{
  let $var := {oorspronkelijke xpath expressie}
  return
    if ( fn:string-length($var > 0) then
 { (xs:date( fn:substring($var,1,10) ) ) } prefix:tag>
    else

};
Note that when nil values are allowed within the source element the above statement will not work. In that case you should check explicitly for nil values (using either the xf:is_nilled or xf:isEmptyOrNilled function):
{
  let $var := {oorspronkelijke xpath expressie}
  return
    if ( fn:not( fn:is_nilled($var)) then
 { (xs:date( fn:substring($var,1,10) ) ) } prefix:tag>
    else

};
Note: in the above example we assume that the element may be nilled. If not you should replace by (). This will result in no element tag being added.