![]() |
![]() ![]() ![]() |
||
|
|||
Example: processing an order |
A customer places an order for a computer, selecting specific components, on the website. The initial form is set up so that it returns DEFAULT, or it returns a code for the item specified. The codes are unique for each component. On the server-side, an XML file exists with tags of format as follows (where we put the monitor's name in the tags rather than as an attribute, looking ahead to monitors which have quotes in their names e.g. 17"):
|
||
The server-side needs to:
|
|||
Solutions |
Any text not part of an XML Script command is sent to standard output, in this case the customer's web-browser. We must therefore begin the content with a MIME-type, which must be the very first line after the XST tag, with not even a new line between the two (see below). The usual HTML formatting is trivial to produce in XML Script: it ensures well-formed XML/HTML output (i.e. br and hr tags must have closing tags, not part of the HTML 4.0 specification) although !DOCTYPE tags should have their angle brackets escaped - < - to avoid confusing the XML parser.
|
||
Firstly let us assume something about the incoming CGI data: it consists of tags called "monitor" etc, which contain the code of the desired component. The list of components, a list of the prices, a running total... all these can be produced with one template-within-the-template.
|
|||
This _template takes an object and formats its contents; specifically an item object which matches the code that the customer has requested (or in case of no match at all, the default="YES" object. As regards the rest of the script, we may first tell X-Tract the form data we expect to obtain:
|
|||
We can then run through the form elements that we've just told X-Tract about within the data tags and, for each one, look for a CGI object whose name matches the type attribute, then match the code with the local database, and then finally return item names, prices, running total etc. An example code for doing this is:
|
|||
This code assumes that you've already used a _method tag between it and the above _template: _method stops a _process'ed object from being simply dumped to the output stack, and instead runs it through the template. All that is left now is for a title object, of the same format as item to be _templated before the _foreach loop, and the whole thing to be wrapped up in a table. The full code of this is shown at the bottom of this section. |
|||
Tip |
If the client-side web-browser is complaining of "server internal errors," you might try running your XML Script templates from a command line, possibly declaring a CGI object within the code using _data. X-Tract may have found some not well-formed XML content in the script, and the bad form may be sufficient to be a fatal error. Server logs may also help: they will suggest either X-Tract crashing, or "premature end to HTTP headers," which means that the server is not even receiving the Content-type: header. |
||
Full source |
All text within <!-- --> is comments: XML Script will ignore them, and you can do too if you wish!
|
||
Development |
The above code can easily be expanded to, for example, show the total cost plus VAT of the order, or to log the details of the order (CGI variables exist on the ENV data branch which tell you e.g. the IP of the customer's computer, or at least the computer that made the request to your server) -- this is left as an exercise: if you're interested, or you would like some advice on where to go from here, please feel free to e-mail us at support@xmlscript.org and we will provide you with a solution to your problem. |
||
Next... | |||
XML Script homepage | Documentation home | XML Script docs | Command list | Function list | X-Tract docs X-Stream, X-Tract and XML Script are trade marks of DecisionSoft Limited© Copyright 1998-2000 DecisionSoft Limited |