Adding transformations
When the structure of the source data is different than the structure of the target data, transformations are required. With the mapping editor, you can add transformation functions to each mapping line. The following example shows how to add a string concatenation function to the mapping model.
- Create a new Mapping Model (as shown in Figure 8)
Student_BookLoansInfo.msl with new
library system.dbm as the source and Student_BookLoansInfo.xsd
as the target.
- Create mappings from STUDENT.STUDENTID to the element Student_ID,
BOOK_LOANS.BOOKID to BookID, and STUDENT.NAME to Name.
Figure 36. Mapping of BookInfo
- You want to use the target XML element BookInfo to store book title and the publisher name. In order to do so, create a mapping from BOOK.TITLE to BookInfo.
- Select the mapping line from BOOK.TITLE to BookInfo, and also select the column BOOK.PUBLISHER_NAME on the source side (keep the CTRL key pressed when selecting the element).
- Right-click on the mapping line, and select Add to Mapping from the context menu.
Figure 37. Add PUBLISHER_NAME to mapping
- This will result in two mapping lines connecting from the source to the target element. Right-click the mapping line, and select Transform > Add from the context menu.
Figure 38. Add transformation
- The Expression Builder dialog comes up to assist with building the transformation. It shows the source columns that are available for the transformation on the left-hand side. On the right, the Expression Builder shows a list of common SQL transformation functions. To add source columns and transformation functions to the transformation expression text box, double-click on them.
Figure 39. Expression Builder
- For the BookInfo target element, you want to concatenate the title and the publisher name. In between, you want to add a forward slash (/) as a separator. For the concatenation, use the DB2 SQL function concat( String1, String2). This DB2 SQL function is not a standardized SQL function -- that's why it does not show up in the list of available functions.
- Manually add the following transformation into the transformation
expression text field:
CONCAT( CONCAT( SCHOOL.BOOK.TITLE, ' / ' ), SCHOOL.BOOK.PUBLISHER_NAME ) |
Figure 40. Expression Builder with transformation
- Generate the SQL/XML query. Note that the transformation function was added to the query.
Figure 41. Student_BookLoansInfo.sql with transformation
- Execute the query. In the result (see also
Ref_StudentBookLoansInfo_result.xml) you'll see that the book title
and the publishers name were correctly inserted into the XML document.
Listing 2. Query result
... <student> <Student_ID>606789032</Student_ID> <Name>Jessica Wong</Name> <BookID>0-06-0522003 </BookID> <BookInfo>C++ Crash Course / Westly Publishing</BookInfo> </student> ... |


