![]() |
![]() ![]() ![]() |
||
|
|||
Storing XML data |
It is often convenient to store XML data in a file independently of the code which may be used to process it. The rules regarding the format of the stored data are the same as those which produce well formed XML, that is, all elements must have opening and closing (or empty-element) tags. Save the following piece of XML to a file called Invoice.xml.
|
||
Importing XML data |
The _data command tells the processor to build a branch of the data tree at the "current location" in the tree. The _data command can also be used to import data from an external file. The file attribute is used to identify the source file, and the contents of the file are then treated as though they were physically present in the script, in between the _data tags.
|
||
Running the script |
Save the above script to a file in the directory where X-Tract is installed, and name it DataFile.xst. Run it using the command xtract DataFile.xst. It will generate the following output:
|
||
Importing other data |
The _echo command also allows data to be imported from an external file, but it assumes the file is plain text rather than XML, and so does not pay attention to angle brackets, ampersands or any other characters which are 'special' to XML. This can be used to deal easily with non-XML text. Save the following fragment to the file Header.txt.
|
||
Combining _echo with other commands |
On its own, _echo will simply read in text from a file. When combined with another command, the output from _echo becomes part of the content of the parent command. In this example, the contents of the file Header.txt will become the content of the _set command.
|
||
Running the script |
Save the above script to a file in the directory where X-Tract is installed, and name it Echo.xst. Run it using the command xtract Echo.xst. The contents of the info sub-element of the invoice root-level data tree element will be set to be the same as the contents of the file Header.txt, as generated by the _echo command. It will generate the following output:
|
||
Importing code |
As well as storing data (XML or non-XML) in independent files, it is also possible to store code in independent files, to use when necessary. Save the following code to a file in the directory where X-Tract is installed, and name it Script.xst.
|
||
The _eval command |
By default, _eval differs from _data in two significant ways. First, it reads the incoming XML onto the template tree instead of the data tree. Second, it instructs the processor to run, or process, the contents. The following script demonstrates the use of the _data, _echo and _eval commands. For clarity we have added comments, which are ignored by the processor.
|
||
Running the script |
Save the above script to a file in the directory where X-Tract is installed, and name it Import.xst. Run it using the command xtract Import.xst. It will generate the following output:
|
||
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 |