Just as with XML maps you create, ECMAScript you write for mapping acts as a translation layer. For example, in a map designed to translate an incoming XML message to Java, you can include a reference to a script function that is designed to do a calculation, separate or combine pieces of data, and so on.
You store an ECMAScript function for mapping in a JSX file. From your XQuery map you point to the function. A map with a reference to a script function might look like this example:
/** * @common:operation * @jws:parameter-xml schema-element="ns0:emp" xquery:: * declare namespace ns0="http://openuri.org/test" * declare namespace ns1="http://www.openuri.org/" * <ns1:submitPerson> * {xqueryMap.TestScript.getPerson($input)} * </ns1:submitPerson> * :: */ public String submitPerson(String personName, int personAge)
The text in bold is the call to script. The script file is TestScript (in the xqueryMap folder), and the function being called is getPerson. The $input parameter represents, as it does elsewhere in maps, the XML document; because this is a parameter-xml map on a method, $input represents the incoming XML.
In other words, the incoming XML is passed to the script function. The function is responsible for handling the XML and extracting the portion needed. In this simple example, the function simply constructs the XML that corresponds to the method parameters; it inserts values from the incoming XML into the XML is it constructing, as shown here:
function getPerson(xml) { /* * Create a variable that contains the XML that contains values to be * passed to the method parameters. Use <> and </> to enclose what is * actually an XML fragment. Access values from the incoming XML with the * dot operator, enclosing those expressions in {} to indicate * where their return values should be inserted. */ var responseXml = <> <test:personAge xmlns:test='http://www.openuri.org/'>{xml.person.age}</test:personAge> <test:personName xmlns:test='http://www.openuri.org/'>{xml.person.name}</test:personName> </>; return responseXml; }
The function reference (everything between and including the curly braces) must be the only child of its parent element. For example, the following XML map fragment will cause an error:
<ns1:submitPerson> {xqueryMap.TestScript.getPerson($input)} <ns1:personAge>{data($input/ns0:person/ns0:age)}</ns1:personAge> </ns1:submitPerson>