Editing the XML mapping
We now create the mapping between the two XML schemas:
| In the XML Mapping editor, select the account element from Accounts.xsd on the left hand side, and drag it to the account element from AccountList.xsd on the right hand side.
|
Figure 10-16 Inline map
| Click Generate XSLT script. See Figure | -16 for help in finding this icon. Check the Accounts-out.xml file and you will see that it has changed.
|
Note: Alternatively, you can simply save the mapping file and the changes are automatically reflected in the resulting XML file.
|
Inline mapping
| Click Edit at the top right corner of the Inline map. See Figure | -16 to find the Edit icon.
|
| In the Inline map details view, perform the following mapping transforms:
|
Map the accountID, accountType, balance, and interest by dragging the elements from the left to the corresponding elements on the right. Note that we map the element accountID to the attribute accountId.
|
Click Generate XSLT script and verify how the account information is generated in the XML output.
|
Map the customerInfo element from left to right. This creates an inline map.
|
Figure 10-17 Account mapping
| Click Generate XSLT script to see the change in the output XML file.
|
Concatenation mapping
The mapper allows us to define mappings where a set of input values are concatenated to a single output value. Here we concatenate firstName and lastName in the source into one Name element in the target:
| Click Edit at the top right corner of the customerInfo Inline map and add these transformations:
|
Select the firstName element and drag it to the name element on the right.
|
Select the lastName element and drag it to the Move transform box between the firstName and name.
|
When we drag a second element to the transform type box, the transform type automatically changes to Concat.
![](images/concat.jpg)
Figure 10-18 Concatenation mapping
| We want to concatenate the name in the format lastName, firstName:
|
Select the Concat transformation, and in the Properties view, select the Order tab.
|
Select lastName and click the Reorder Up icon .
|
Select the General tab, select lastName, and put `, ' in the Delimiter column (with a space after the comma).
|
Click Generate XSLT script icon. The changes are visible in Accounts-out.xml file.
|
Substring mapping
The phone number is stored as a single data type in the source document and we want to separate it into the sub-elements of area code and local number in the target document.
| The following steps perform the substring mapping transformation:
|
Select the phoneNumber element on the left and drag it to the areaCode element on the right. You have to expand the phoneNumber element in the target to see the areaCode element.
|
Click the drop-down arrow in the transformation and select Substring from the list.
|
Right-click the transformation and select Show in Properties.
|
In the Properties view, select the General tab. In the Delimiter field put a space. Because the phone number format is (xxx) xxx-xxxx, the space should be the delimiter between area code and local number.
|
In the Substring index field, type 0.
|
Select the phoneNumber element and drag it to the localNumber element.
|
Change the transform type to Substring.
|
In the Substring index field, type 1. The current mapping is shown in.
|
Figure 10-19 Substring mapping
| To return to the main map, click the Up a level icon at the top right of the inline map details page. Do it twice to return to the main map.
|
Calculation
We want to calculate the sum of the balance from all accounts and put it in the balanceSum attribute of the output document. An XPath expression is used to calculate this total:
| Select the accounts element on the left and drag it to the balanceSum attribute on the right.
|
| Click the transform type box, and select Custom.
|
Figure 10-20 Custom mapping
| Select the Custom mapping transformation. In the Properties view select the General tab. Select XPath option for Code and type sum(./*/in:balance) as the XPath expression.
|
Figure 10-21 Custom mapping using XPath
| Save the mapping file and click Generate XSLT script to see the final output XML file. Notice the balanceSum attribute of the accounts element, the concatenated name, and the area code and local number...
|
Example 10-4 Final output showing the balanceSum attribute
<?xml version="1.0" encoding="UTF-8"?>
<out:accounts xmlns:out="http://itso.rad75.xml.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" balanceSum="300">
<out:account accountId="123456" accountType="Savings">
<out:balance>0.0</out:balance>
<out:interest>5.5</out:interest>
<out:customerInfo>
<out:name>Hainey, Brian</out:name>
<out:phoneNumber>
<out:areaCode>(123)</out:areaCode>
<out:localNumber>456-7890</out:localNumber>
</out:phoneNumber>
</out:customerInfo>
</out:account>
......
<out:accounts>