SERPland Blog

Oracle Service Bus XQuery Transformations: Using own custom function to check empty element content (XML Null Value / Null If)

· 681 words · 4 minutes to read

In a Oracle Service Bus (OSB) environment, a proxy service usually does some XML transformations. So a XML element conversion from business service data to endpoint WSDL structure will be done. In some cases, returning empty elements ( as ) is not allowed. This can only be handled programmatically. All the optional elements have to be checked against null value (NVL in SQL).

Pseudocode: If Element Value is Null, then do not return the complete Element

With XQuery it would look something like this:

(:: if element content is empty, then do not return the element ::)
{
if (data($detailViewCollection1/ns1:detailView[1]/ns1:email1) > "") then
(
{ data($detailViewCollection1/ns1:detailView[1]/ns1:email1) }
)
else()
}
(:: if element content is empty, then do not return the element ::)
{
if (data($detailViewCollection1/ns1:detailView[1]/ns1:email2) > "") then
(
{ data($detailViewCollection1/ns1:detailView[1]/ns1:email2) }
)
else()
}
(:: if element content is empty, then do not return the element ::)
{
if (data($detailViewCollection1/ns1:detailView[1]/ns1:telefon1) > "") then
(
{ data($detailViewCollection1/ns1:detailView[1]/ns1:telefon1) }
)
else()
}

Uuupps, very ugly, isn’t it? No programmer likes to program like this…..

Actually, in XQuery normal user defined functions can be created as like in most other languages:

declare function local:emptyValue($element as element() )
{
if (data($element) > "")
then
(
$element
)
else()
};

With this little function the main XQuery code looks much better. Unfortunatelly, the Oracle Service Bus (OSB) Editor in Eclipse OEPE doesn’t like this very much. The graphical tool shows a warning on each function call … “the content of one of the XML element is not supported by the mapper” ….

OK, let’s just ignore this graphical mapper problem, because we only initially use this mapper, after that all XQuery will be changed/edited in the source code itself.

So now it’s a little more clean XQuery code:

{local:emptyValue(
{ data($detailViewCollection1/ns1:detailView[1]/ns1:email1) }
)}
{local:emptyValue(
{ data($detailViewCollection1/ns1:detailView[1]/ns1:email2) }
)}
{local:emptyValue(
{ data($detailViewCollection1/ns1:detailView[1]/ns1:telefon1) }
)}


Update 2024

Update zu Oracle Service Bus XQuery Transformationen 🔗

In einer Oracle Service Bus (OSB)-Umgebung führt ein Proxydienst in der Regel einige XML-Transformationen durch. Eine XML-Elementkonvertierung von den Daten des Geschäftsdienstes zur Endpunkt-WSDL-Struktur wird vorgenommen. In einigen Fällen ist es nicht erlaubt, leere Elemente zurückzugeben. Dies kann nur programmgesteuert gehandhabt werden. Alle optionalen Elemente müssen auf Nullwert (NVL) überprüft werden.

Mit XQuery könnte es wie folgt aussehen: Wenn der Elementinhalt leer ist, sollte das Element nicht zurückgegeben werden.

if element content is empty then do not return the element
if data detailViewCollection ns detailView ns email then data detailViewCollection ns detailView ns email
else if element content is empty then do not return the element
if data detailViewCollection ns detailView ns email then data detailViewCollection ns detailView ns email
else if element content is empty then do not return the element
if data detailViewCollection ns detailView ns telefon then data detailViewCollection ns detailView ns telefon

Oh, das sieht sehr unansehnlich aus, oder? Kein Programmierer programmiert gerne auf diese Weise. Tatsächlich können in XQuery normale benutzerdefinierte Funktionen erstellt werden, wie in den meisten anderen Sprachen auch.

declare function local:emptyValue($element as element) as element {
  if data element then element else ()
}

Mit dieser kleinen Funktion sieht der Haupt-XQuery-Code viel besser aus. Leider mag der Oracle Service Bus OSB Editor in Eclipse OEPE das nicht besonders. Das grafische Tool zeigt eine Warnung bei jedem Funktionsaufruf an, da der Inhalt eines der XML-Elemente vom Mapper nicht unterstützt wird.

Nun ja, lassen Sie uns dieses Problem mit dem grafischen Mapper einfach ignorieren, denn wir verwenden den Mapper nur zu Beginn. Danach werden alle XQuery-Transformationen im Quellcode selbst geändert. Jetzt ist der XQuery-Code etwas sauberer:

local:emptyValue(data detailViewCollection ns detailView ns email)
local:emptyValue(data detailViewCollection ns detailView ns email)
local:emptyValue(data detailViewCollection ns detailView ns telefon)

2024 Update:

In den Jahren seit 2011 hat sich die Verwendung von XQuery und Oracle Service Bus weiterentwickelt. Die Entwicklungsumgebung hat möglicherweise mehr Funktionen zur Unterstützung benutzerdefinierter Funktionen hinzugefügt, um die Programmierung von XML-Transformationen zu erleichtern. Es ist ratsam, die neueste Dokumentation von Oracle zu konsultieren, um über die aktuellen Best Practices informiert zu bleiben. 2024 könnte es noch mehr Optimierungen geben, um die XQuery-Entwicklung im Oracle Service Bus effizienter zu gestalten.