Import Java Custom Method in Xquery

1.1k Views Asked by At

I am using Weblogic Integration framework. While transforming one XML format to another using .xq file, I want to apply some logic written in a custom Java Class.

For example, XML1 has tag: <UnitCode>XYZ</UnitCode>

Custom Java Class:

public class unitcodemapper{
public static String getMappedUnitCode(String unitCode){
    if(unitCode=="XYZ")
       return <<value from DB table>>
    else
       return unitCode;
}
}

XML2 will have a tag: <UnitCode>unitcodemapper.getMappedUnitCode(XML1/UnitCode)</UnitCode>

I cannot find any documentation or example to do this. Can someone please help in understanding how this can be done?

2

There are 2 best solutions below

0
keshlam On

This is known as an "extension function". The documentation for your XQuery implementation should have a section telling you how to write such functions and plug them into the processor. (The details may differ from one XQuery processor to another, which is why I'm referring you to the manual.)

0
adamretter On

Whilst @keshlam mentions Extension Functions, which are indeed supported by many implementations each with their own API.

I think perhaps what you are looking for instead is Java Binding from XQuery. Many implementations also support this and tend to use the same approach. I do not know whether WebLogic supports this or not! If it does, the trick is to use java: at the start of your namespace URI declaration, you can then use the fully qualified Java class name of a static class, each static method you may then call directly from that namespace.

You can from two examples of implementations that offer the same Java Binding from XQuery functionality here:

http://exist-db.org/exist/apps/doc/xquery.xml#calling-java

http://docs.basex.org/wiki/Java_Bindings

These could serve as examples for you to try on WebLogic to see if it is supported in the same way. However, I strongly suggest you check their documentation as they may take a different approach.