Tutorial 2 - Address
Setup
Before you start this tutorial, make sure you have created a solution and
a project. In the following example, we assume the B2B/MyProject project
is created.Under Perspective, switch to the XML Perspective. The DTDs for
this tutorial is located in plugins\xmlmappingui\samples\address.
Use File->Import to import the Contact.dtd and the Member.dtd
into the project MyProject.
Getting Started
From the Workbench, choose File New->XML Mapper, the DTD to DTD Mapping
wizard will come up. If necessary, specify the appropriate project and
rename the default mapping session name to addressmap.xmx.
Turn, to the next page, to specify the source DTD Use the Add
button to add the Contact.dtd.
Turn to the next page to specify the target DTD. Use the Browse button
to add the Member.dtd.
Turn to the next page. Select Contact to be the source root element.
Select Member to be the target root element.
When you hit Finish, an addressmap.xmx resource under B2B/MyProject
is created, and the XML Mapping Editor is launched.
Define mappings between the source and target elements
The next step is to map the source elements to the target elements. Similar
to tutorial 1, you will be selecting one element from the source, and one
element from the target, then right mouse button 2 to select Add Mapping
to define a mapping between the source element and the target element.
The following table summarizes the mappings that you will need to define.
Notice how you will need to define the mapping between DeliverTo
and LastName, as well as DeliverTo and FirstName.
In the next section, we will show you how you can define a string function
to take the string in DeliverTo and split that into two fields,
LastName
and FirstName.
Source: Contact |
Target: Member |
Name |
AddressType |
DeliverTo |
LastName FirstName |
Street |
AddressLine |
City |
City |
State |
State |
PostalCode |
Zip |
Country |
Country |
Email |
Email |
Once the mapping is completed, the Mapping Summary Table should look
something like this:
Adding a string function
The Contact.dtd defines the element DeliverTo. It is intended
to store the full name of a person, for example, Bob Smith. An alternate
way to store the name of a person is represented in the Member.dtd.
It is divided into two elements, LastName, and FirstName.
In XSLT, you can use various string functions to transform its arguments.
The XML Mapping Editor allows you to specify these string functions. In
our example, we will apply a substring-before function, and a substring-after
function to the DeliverTo element as follows:
Define substring-before
Assuming the DeliverTo text is set to something like "Bob Smith",
to assign the word "Bob" as text to the FirstName element, do the
following:
-
Select the mapping between FirstName and DeliverTo.
-
Right mouse button 2 and select Define Function to bring up the
Define
XSLT Functions wizard as shown below.
-
Choose the substring-before function.
-
Accept the Target default ' '. This will invoke the substring-before
function on the target string and return the substring that is before any
blank character.
Define substring-after
To assign the word "Smith" as text to the LastName element, do the
following:
-
Select the mapping between LastName and DeliverTo.
-
Right mouse button 2 and select Define Function to bring up the
Define
XSLT Functions wizard as shown below.
-
Choose the substring-after function.
-
Accept the Target default ' '. This will invoke the substring-after
function on the target string and return the substring that is after any
blank character.
In the future, the XML Mapping Editor will support more of the XPath
functions.
Testing the XSLT Script
Once you have generated your XSLT script, you can invoke the Xalan processor
to do the transformation. You can use the plugins\xmlmappingui\samples\xslt.bat
file to invoke the Xalan processor. You will need to set up the environment
variable VABASE to point to where you have installed the WebSphere Studio
Workbench.
A plugins\xmlmappingui\samples\address\Contact.xml file is provided
for you to try out your generated XSLT script. Go to the directory plugins\xmlmappingui\samples\address
and type:
..\xslt -in Contact.xml -xsl Member.xsl > Member.xml
to get the result Member.xml file.