XML Script logo next up prev
Handling data and code
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.

<invoice >
	<item name="jacket" price="95.99" />
	<item name="skirt" price="35.00" />
	<item name="coat" price="42.25" />
</invoice>
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.

<XST __XMLscript="1.1" >
<_data file="Invoice.xml" />
# \invoice.__element #
</XST>
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:

<invoice >
	<item name="jacket" price="95.99" />
	<item name="skirt" price="35.00" />
	<item name="coat" price="42.25" />
</invoice>
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.

This information was generated by X-Tract
using the XML Script language (v1.1).
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.

<XST __XMLscript="1.1" >
<_set name="\invoice\info" >
<_echo file="Header.txt" />
</_set>
# \invoice.__element #
</XST>
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:

<invoice ><info >
This information was generated by X-Tract
using the XML Script language (v1.1).
</info></invoice>
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.

<XST __XMLscript="1.1" >
<Script __mode="dissolve">
# \.tax_rate := '.175' #

# \.gross := sum( \invoice\item.price )#
# \.net := round( \.gross / ( 1 + \.tax_rate ),2 ) #
# \.tax := \.gross - \.net #

# \invoice\total.gross := \.gross #
# \invoice\total.net := \.net #
# \invoice\total.tax := \.tax #

<_eval ># \invoice.__element #</_eval>
</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.

<XST __XMLscript="1.1" >

<!-- This is a comment -->

<!--
	Evaluate as XML and add to the data
	tree but don't process the contents.
-->

<_data file="Invoice.xml" />

<!--
	Replace the _echo command with the
	file contents but don't evaluate or
	process them.
-->

<_set name="\invoice\info" >
	<_echo file="Header.txt" />
</_set>

<!--
	Evaluate the contents and process them.
-->

<_eval file="Script.xst" />

</XST>
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:

<invoice >
        <item name="jacket" price="95.99" />
        <item name="skirt" price="35.00" />
        <item name="coat" price="42.25" />
<info >
This information was generated by X-Tract
using the XML Script language (v1.1).
</info>
<total gross="173.24" net="147.44" tax="25.8" />
</invoice>
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