# Step 4: Add Support for XML Mapping

Currently your web services return XML reports of the following form.

    <applicant>
       <availableCCCredit>2000</availableCCCredit>
       <currentlyBankrupt>false</currentlyBankrupt>
       <lastName>Smith</lastName>
       <firstName>John</firstName>
       <approvalLevel>Applicant is a high risk.</approvalLevel>
       <creditScore>560</creditScore>
       <taxID>222222222</taxID>
    </applicant>

But what if a client asked you to return an XML report of the following form?

    <applicant id="222222222">
       <name_last>Smith</name_last>
       <name_first>John</name_first>
       <bankrupt>false</bankrupt>
       <balance_remaining>2000</balance_remaining>
       <risk_estimate>Applicant is a high risk.</risk_estimate>
       <score_info>
          <credit_score>560</credit_score>
          <us_percentile>19.0</us_percentile>
       </score_info>
       <message>Credit Report complete.</message>
    </applicant>

In this step you will construct a credit report to your client's specifications with the aid of the schema file ClientB.xsd and a set of corresponding XMLBeans classes, ApplicantDocument and ApplicantDocument.Applicant, which are located in the Schemas folder.

When a schema file is placed in the Schemas project, WebLogic Workshop produces a set of XMLBean Java classes corresponding to the schema. These XMLBean classes let you construct or parse XML documents that are instances of the original schema. In this case, you will use the XMLBean classes to construct an XML document of the form desired by your client.

The tasks in this step are:

- to add an XQuery map
- to test the Investigate web service

To Add an XQuery Map

In this task you will place an XQuery map on the callback onCreditReportDone. The XQuery map will reshape the XML document currently sent back to clients into an XML document that conforms to the schema ClientB.xsd.

1. From the Window menu, select Investigate.jws.
2. Click the Design View tab.
3. Select the arrow icon for the onCreditReportDone callback.
4. On the Property Editor, in the section labeled parameter-xml, locate the xquery property. Click the ellipses on the far right-hand side of the value field, as shown in the following illustration.
5. Click Ok, when asked if you would like to define a schema for this operation.
6. In the XQuery Mapper - Select Schema Element dialog, click Choose.
   (Look under Sample XML. Note that the method onCreditReportDone already has a default schema associated with it. This is the schema that the method currently uses to convert Applicant objects into XML messages.)
7. In the Select Schema Element dialog, select XML Types-->ClientB.xsd-->applicant and click Ok.
   (In the XQuery Mapper - Select Schema Element, look under Sample XML. Notice that the new schema, ClientB.xsd, is now associated with onCreditReportDone method.)
8. In the XQuery Mapper - Select Schema Element dialog, click Edit XQuery.
9. In the XQuery - Edit XQuery dialog, map the XML elements by dropping and dragging icons from the Source Schema pane to the Target Schema pane. Form the following matching pairs:

Source Schema | Target Schema
taxID | id
firstname | name_first
lastName | name_last
currentlyBankrupt | bankrupt
availableCCCredit | balance_remaining
creditScore | credit_score
approvalLevel | risk_estimate
message | message

The Map tab of The XQuery Mapper - Edit XQuery dialog should like the following illustration.
(Solid lines indicate a data conversion function is applied to the mapping. For example the mapping availableCCCredit-->balance_remaining also includes a data conversion from the int data type to the short data type. Dotted lines indicate that no data conversion function is applied.)
10. On the XQuery Mapper - Edit XQuery dialog, in the Target Schema pane, select the us_percentile element.
11. In the Advanced Options Panel, click the Target Expression tab.
12. In the Edit Function pane, enter the value 50 and click Apply.
    (This is a default value. You will add a script to calculate the <us_percentile> element in the next step of the tutorial.)
13. Click Ok to close the XQuery Mapper - Edit XQuery dialog.
14. Click Ok to close the XQuery Mapper - Select Schema Element dialog.
15. In Design View, click the name of the onCreditReportDone callback.
    (Notice that the arrow icon for the onCreditReportDone callback has a green color. This indicates that there is an XQuery map now associated with this callback.)
    The callback onCreditReportDone now has the following XQuery map associated with it.
    In the XQuery map, there are two schemas at work: (1) the default schema automatically created for the Applicant object and (2) the new schema which you are imposing, i.e., ClientB.xsd. When an Applicant object is being prepared as an XML document for transport over the wire, XQuery expressions ( such as xs:int(data($input/ns0:applicant/ns0:taxID)) ) select values from the default XML schema and insert them into the imposed schema ( such as <ns1:applicant id="..."> ). The result is an XML document conforming to the imposed schema.
16. Press Ctrl+S to save your work.

Your web service's onCreditReportDone callback now sends XML messages that conform to the ClientB.xsd schema.

To Test the Investigate Web Service

1. Confirm that Investigate.jws is displayed in the main work area.
2. Click the Start button, shown below.
3. Your web service compiles and Workshop Test Browser launches.
4. In the taxID field, enter 222222222 and click the requestCreditReport button.

   Note: Use one of the following (9 digit) taxID's to test your web service throughout the tutorial:

   123456789, 111111111, 222222222, 333333333, 444444444, and 555555555.
5. Click Refresh until the Message Log shows an entry for callback.onCreditReportDone.
6. Click callback.onCreditReportDone.
   Note that the shape of the XML message conforms to the schema ClientB.xsd.
7. Return to WebLogic Workshop, and click the Stop button (shown below) to close the Workshop Test Browser.

Related Topics

Getting Started with XMLBeans

Click one of the following arrows to navigate through the tutorial: