Sunday, January 22, 2012

XQuery to return a property value

Too often in OSB developers hardcode property values everywhere in the Message Flow.
This makes life difficult, especially if these are environment-dependent values.

Waiting for Oracle to provide a decent way to externalize variables in a property file and read it from OSB (you can do that with a Custom XPath.... but the problem is that the property file would not be visible/editable from the OSB console, which is a pity...)...
you can use this approach:

xquery version "1.0" encoding "Cp1252";

declare namespace xf = "http://tempuri.org/OSBProject1/getProperty/";

declare function xf:getProperty($propertyName as xs:string)
    as xs:string {
    let $properties := 
    <properties>
     <stageErrorDirectory>bla</stageErrorDirectory>
     <targetErrorDirectory>bla</targetErrorDirectory>
    </properties>
    
    let $match := $properties//*[name()=$propertyName]
    return
    if ($match) then
     $match/text()
    else 
     fn:error(xs:QName('IllegalArgument'), $propertyName)
};

declare variable $propertyName as xs:string external;

xf:getProperty($propertyName)


This code shows also 2 interesting things:
- how to find a node with a given element name (using *[name()='something'] )
- how to raise a well readable error message with fn:error()

No comments: