In the previous step, you added an XML map to your web service, but one part of the map was left unfinished. The <us_percentile> element, which gives the applicant's percentile ranking, currently displays a default value of 50% in all cases.
<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>50</us_percentile> </score_info> <message>Credit Report complete.</message> </applicant>
In the following step you will finish the XML map by calculating a value for the <us_percentile> element. To do this, you will write a function in ECMAScript (also known as JavaScript), and then refer to the function from the XML map.
The tasks in this step are:

/* A function to calculate the applicant's credit score percentile. */
function calcPercentile (score) {
var percentile = 0;
var scoreLowEnd = 300;
var number = 3162.5;
percentile = Math.ceil((Math.pow(score, 2) - Math.pow(scoreLowEnd, 2))/(2 * number));
return percentile;
}
public interface Callback extends com.bea.control.ServiceControl
{
/**
* @common:message-buffer enable="true"
* @jws:conversation phase="finish"
* @jws:parameter-xml schema-element="ns0:applicant" xquery::
* declare namespace ns0 = "http://www.openuri.org/"
* declare namespace ns1 = "http://openuri.org/bea/samples/workshop"
*
* <ns1:applicant id = "{ xs:int( data($input/ns0:applicant/ns0:taxID) ) }">
* <ns1:balance_remaining>{ xs:short( data($input/ns0:applicant/ns0:availableCCCredit) ) }</ns1:balance_remaining>
* <ns1:bankrupt>{ data($input/ns0:applicant/ns0:currentlyBankrupt) }</ns1:bankrupt>
* {
* for $message in $input/ns0:applicant/ns0:message
* return
* <ns1:message>{ data($message) }</ns1:message>
* }
* {
* for $firstName in $input/ns0:applicant/ns0:firstName
* return
* <ns1:name_first>{ data($firstName) }</ns1:name_first>
* }
* {
* for $lastName in $input/ns0:applicant/ns0:lastName
* return
* <ns1:name_last>{ data($lastName) }</ns1:name_last>
* }
* {
* for $approvalLevel in $input/ns0:applicant/ns0:approvalLevel
* return
* <ns1:risk_estimate>{ data($approvalLevel) }</ns1:risk_estimate>
* }
* <ns1:score_info>
* <ns1:credit_score>{ xs:short( data($input/ns0:applicant/ns0:creditScore) ) }</ns1:credit_score>
* <ns1:us_percentile> {investigateJWS.PercentileScript.calcPercentile(data($input/ns0:applicant/ns0:creditScore))} </ns1:us_percentile>
* </ns1:score_info>
* </ns1:applicant>
* ::
*/
void onCreditReportDone(investigateJCS.Applicant applicant);
}
Note that the value of the <us_percentile> element is based on the
value of the <credit_score> element: when the calcPercentile()
function is called, the value of the <credit_score> element is passed
in as a parameter.Note: Use one of the following (9 digit) taxID's to test your web service throughout the tutorial:
Handling XML with ECMAScript Extensions
Click one of the following arrows to navigate through the tutorial: