XML Script logo up prev
Creating XML Script templates
Introduction

XML Script is used to write scripts for transforming XML data. XML Script files are also often called templates.

The XST element

An XML Script template is a valid XML document, with the XML Script enclosed in XST tags.

<XST >

</XST>
Commands

This example shows a _data command. The command is a normal, well-formed XML element: it is enclosed in angle brackets, has a file attribute with the value PurchaseOrder.xml and in this case has no content so can be represented by an empty-element tag. It is also recognised by the XML Script processor as being a command. The _data command tells the XML Script processor to open the file whose name is given by the file attribute, and read in the contents of that file as an XML data tree. This data tree can then be referred to by the rest of the XML Script template.

<_data file="PurchaseOrder.xml" />
Interpolations

One way of referring to the data we have read in is to use an 'interpolation'. This is identified by its surrounding '\#' signs. The backslash in the example below refers to the 'root' data element, and Order refers to the sub-element of the data root called Order. When the template is processed, the hash signs and all the text between them will be replaced by the content of the Order element.

# \Order #
Saving the script

To test this, save the following script to a file in the directory where X-Tract is installed, and name it DisplayPO.xst.

<XST >
<_data file="PurchaseOrder.xml" />
<My_Output >
# \Order #
</My_Output>
</XST>
Running the script

At the command line, type xtract DisplayPO.xst. X-Tract will then read the template file, looking for commands it recognises. If it finds any, it will process them. In this case, it will first skip over the My_Output start-tag as this is not a built-in XML Script command, it's just something we made up. The first command X-Tract recognises is the _data command. This tells the processor to read in PurchaseOrder.xml as an XML data tree. Once _data has been processed, it is replaced by its output: in this case, nothing. X-Tract then processes the interpolation, by replacing it with the content of the Order element of the data tree. Having dealt with all of the elements that it recognises, X-Tract finally echoes the template to the output. In this example, all that is left of the template after processing is the My_Output tags and the content of the Order element which had replaced the interpolation, and so the output should be as follows:

<My_Output >
<Manufacturer Name="Ford Motor Company" >
	<Address >
		<Street >The American Road</Street>
		<City >Dearborn</City>
		<State >Michigan</State>
		<ZipCode >48121</ZipCode>
	</Address>
</Manufacturer>
<Model Name="Mustang" >
	<Engine >1.8i</Engine>
	<Color >Black</Color>
	<Trim >Gold</Trim>
</Model>
</My_Output>
Contents...

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