Useful links
Sample E4X (ECMAScript for XML) Scripts
The following set of sample scripts cover some of the key programming topics when using E4X within the MagooClient environment. All of the referenced WSDL, Schema and JavaScript files are already provided as part of the MagooClient download and full step-by-step details on creating and testing these scripts can be found in the 'XML Scripting using E4X' section of the MagooClient User Guide.
Sample Scripts:
Namespace Prefixing
An absolutely vital, though often overlooked aspect of creating code to access elements or attributes within an E4X XML object is to ensure that namespace prefixes are used appropriately- remember that E4X is namespace-aware.
For example, in the GetTopMovers request defined by http://www.xignite.com/xQuotes.asmx?WSDL, all elements must be properly qualified. Sample SOAP for this request is shown below:
The following JavaScript, without namespace prefixing, results in an illegal Header element being created under the Envelope node.
So, rather tham updating the value of the password field as required, the resulting XML looks like:
This is because E4X cannot find the required element due to a lack of namespace prefixing in the accessor code and so assumes that a new element should be added. The correct code, using the Namespace variables defined in the script is:
GetTopMovers.soapenv::Header.s0::Header.s0::Password = "abcdef";
In general it is advisable to drag the required element from the MagooClient XML Viewer tree onto the script editor to ensure that the accessor code is correct – MagooClient will automatically ensure that namespace prefixing is added appropriately. Note that when creating a new script, MagooClient will also insert any required Namespace variables - in this case:
var s0 = new Namespace('http://www.xignite.com/services/');
var soapenv = new Namespace('http://schemas.xmlsoap.org/soap/envelope/');
Performing Calculations
In this example, a script is used to fill out the values of elements based on information contained in other elements within an XML document. This is a familiar activity from order forms where totals need to be calculated based on the items requested. In this case, the order form represents a request to trade certain stocks.
The example is based on TradeRequest.xsd in the MagooClient samples directory. Sample XML for this document is shown below:
To iterate through the sequence of TradeItems, use a for-each-in loop – the complete code is as follows:
Note that the parseFloat() call is used to 'cast' the TradeItem.Cost to a numeric float type - without this, totalcost would be set to a concatonation of the individual TradeItem.Cost strings.
Copying Simple Content
A common mistake when attempting to set the content of a simple element to that of another is to use a simple ‘=’ assignment. This however is incorrect due to subtleties in the way that XML object properties are resolved - using '=' results in the target element being replaced by the source. For example in the case of a GetTopMovers request (http://www.xignite.com/xQuotes.asmx?WSDL), we wish to set the password to the username. Sample XML is shown below:
If a simple ‘=’ expression is used then we get the incorrect structure not the assignment we require – the Password element is replaced by a Username element. Therefore using a simple assignment such as:
In order to set the content of Password equal to that of Username, use the toString() function on the source i.e.
Embedding a Web Service Call
This example takes the TradeRequest XML and introduces dynamic behaviour using a script with an embedded Web Service call. Rather than having to manually look up and enter details for the stocks to be traded, a script is created which makes use of a remote Web Service to obtain information on the top moving stocks on the U.S. markets. The stock list is then presented to the user via the dialog.select() function. The Web Service (xQuotes) is provided on a free trial basis (10 free credits) by Xignite – for more details see http://www.xignite.com/xQuotes.asmx . MagooClient provides a code-generator to automatically insert a javascript function ‘fn_GetTopMovers’ into a script body. This function:
A sample script, generated by MagooClient, to invoke on the GetTopMovers operation is shown below. Full details on using the MagooClient code generator is provided in the User Guide. Alternatively you can view a screencast which contains a fully-worked example.