Using a custom Java bean in the XML mapping editor

When mapping XML elements, you may want to perform an action that is not provided by a standard XSLT function. One option is to use a custom Java bean. This topic uses an example to show how to call a method that is defined in your own custom Java bean.

In our example, we want to convert a value from float to String. Since the XML mapping editor does not provide a converter function, we will use our custom BeanTypeConverter Java class.

Consider the following map in the XML mapping editor. We want to place the value of customerIDFloatAttr, an element of type float, into customerIDAttr, an element of type string. Since the target element's type is string, we need to convert the float to a string. We will do so by calling the floatToString method that is defined in the BeanTypeConverter Java class, and passing the float value as an argument to the method. The resulting string will be placed into the customerIDAttr element.
Example map in the XML mapping editor.

Note: The bean that you want to use must exist in a Java project in your workspace
  1. Open the map in the XML mapping editor.
  2. Right click on the target customerIDAtrr element, and Define XSLT function.

  3. In the XSLT functions window, select Custom Java Bean, and click Next.
  4. Select the project, Java bean and Java method that you want to use. Click Add
  5. In the list of possible XPaths and variables, select the element customerIDFloatAttr as input. Click OK. In the XSLT Functions window, click Finish.
    XSLT functions window
The overview section of the XML mapping editor shows the applied function
floatToString function in the XML mapping editor
This is the resulting XSL for the customerIDFloatAttr and customerIDAttr elements
           <xsl:variable name="var0_floatToString" select="user0:floatToString(/body/getQuote/inputGetQuote/@customerIDFloatAttr)"/>
            <xsl:if test="@customerIDFloatAttr">
                <xsl:attribute name="customerIDFloatAttr">
                    <xsl:value-of select="@customerIDFloatAttr"/>
                </xsl:attribute>
            </xsl:if>
            <xsl:attribute name="customerIDAttr">
                <xsl:value-of select="$var0_floatToString"/>
            </xsl:attribute>

Feedback
(C) Copyright IBM Corporation 2005, 2006. All Rights Reserved.