![]() |
![]() ![]() |
||
|
|||
Introduction |
Like any other programming language, XML Script allows you to create, store and manipulate variables and data during processing. This section describes the basic mechanisms used to assign and retrieve values using variables within your scripts. |
||
Assignment by example |
Because XML Script uses XML to store both code and data, values can be assigned by setting out the data, in XML format, within the script. You can choose whether to store such a value as an element or an attribute. In the example below, the _data command is used to create an invoice element, with three item sub-elements, each with name and price attributes. The interpolation \invoice tells the processor to return the contents of the invoice element at the root level in the data tree.
|
||
Running the script |
Save the above script to a file in the directory where X-Tract is installed, and name it AssignmentByExample.xst. Run it using the command xtract AssignmentByExample.xst. It will generate the following output:
|
||
Assignment using _set |
Another way of creating XML data from within a script is to use the _set command. The _set command evaluates the text within its own opening and closing tags, and makes the result the content of the element or attribute described by the command's name parameter. In the following example the text content of the _set command is put into a new element called info, where info is a child of the root-level data tree element invoice.
|
||
Running the script |
Save the above script to a file in the directory where X-Tract is installed, and name it AssignmentBySet.xst. Run it using the command xtract AssignmentBySet.xst. It will generate the following output:
|
||
Explicit assignment |
We can also use explicit assignments within interpolations to create elements and attributes within the XML tree. In the following example, the first interpolation assigns the value '0.175' to an attribute called TaxRate. The second line includes an interpolation which echoes the value back to the output for display. You might ask what TaxRate is an attribute of - in this case the answer is that it is an attribute of the root element of the data tree. The data tree is covered in more detail later.
|
||
Running the script |
Save the above script to a file in the directory where X-Tract is installed, and name it ExplicitAssignment.xst. Run it using the command xtract ExplicitAssignment.xst. It will generate the following output:
|
||
Attributes as variables |
In the preceding tutorial, \.tax_rate referred to the tax_rate attribute of the root element of the data tree. We could equally have defined and retrieved it using the template tree, in which case we would have used the interpolation $\.tax_rate . In either case we are using XML attributes to store the temporary program variables used by our scripts.
|
||
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 |