Introduction
And
the mobile client which consumes the system payload expects lower
camel case format to process the payload.
We have define new namespace with following pattern.
XSLT
2.0 offers a rich set of transformation tools
for xml transformation. But still in some cases we may encounter
very specific business data transformations which
can not be handled by generic transformation function. To handle
those cases we may have to use the extension capability of
the underling
processing engine.
Problem
Lets
imagine a legacy backend system responding with following xml
payload.
It
follows the upper camel case format to construct the xml payload.
Solution
We
are going to implement extension function
using Xalan XSLT processor.
Let's divide the
solution in to few steps.
1.Create the Java implementation of the logic.
1.Create the Java implementation of the logic.
Google guvava
api has a rich set of string manipulation tools.
We are going to reuse one of those for converting UpperCamelCase string to lowerCamelCase string.
2. Plug the custom function to XSLT.
We have define new namespace with following pattern.
xmlns:
prefix
="xalan://
packagename.classname
"
In
our example we have
xmlns:custom
=
"xalan://custom.xpath.CustomXpath"
And we can call the function as follows
{custom:toLowerCamelCase(local-name())}
3. Test the custom Xpath function.
As the final step we are creating a Junit test for test the custom Xpath function. One thing to note here is we are not using the default xslt processor instead we set the
transformerfactory as
"org.apache.xalan.processor.TransformerFactoryImpl" so that
transformation will be done using Xalan apis.
Conclusion
Developing a custom Xpath function and plugin it to your code is not that hard but always look for existing Xpath function or existing extension function set like EXSLT. After all its good old Java , open for you to play with.
Developing a custom Xpath function and plugin it to your code is not that hard but always look for existing Xpath function or existing extension function set like EXSLT. After all its good old Java , open for you to play with.
You
can find the eclipse project with the sample code
at
https://github.com/KalpaD/xalan_custom_xpath_sample