XML Script logo next up
Converting between two XML formats
Introduction

XML Script was created to make it easy to convert data between different XML formats. You can also use it to convert between XML and a non-XML format, or between two non-XML formats. In this example we will take the XML input data from the previous example and convert it into a new XML format.

Import the source data

Use the _data command to open the existing XML file and read it in to the XML data tree. Note that objects which are stored in the data tree are distinct from objects which are part of the XML Script template itself.

<_data file="PurchaseOrder.xml" />
Defining the new XML format

You will need to decide on the format of the output data. In this example we will create a WorkTicket element which contains data from the original Purchase Order. The WorkTicket element below contains all the sub-elements that we want to see in our final output - but does not yet include any of the attributes or content.

<WorkTicket Factory="" >
	<Body ModelName="" >
		<Engine />
	</Body>
	<Paintshop >
		<BodyColor Description="" />
		<Trim Description="" />
	</Paintshop>
	<Delivery >
		<Customer ID="" />
	</Delivery>
</WorkTicket>
Including elements

We want to identify elements in the source data that we want to include in the output data. In this case, we want to take the City element from the original Purchase Order and make it the value of the Factory attribute in the output data. We do this by inserting an 'interpolation' inside the double quotes which follow Factory in the WorkTicket element above. Within the two 'hash' or 'pound' signs, the first backslash tells the processor to go to the root of the data tree; Order tells it to identify the first Order element; \Manufacturer tells it to go to the Manufacturer sub-element of the Order element. The completed interpolation tells the processor to identify the City sub-element of the Address sub-element of the Manufacturer sub-element of the root-level Order data element.

Factory="#\Order\Manufacturer\Address\City#"
Including attributes

We also want to identify attributes in the source data for inclusion in the output data. Attributes are identified within the interpolation string by using the 'period' or 'dot' to mean 'is an attribute of'. In this example, we are asking the processor to insert a value into the ModelName attribute of the output data. The completed interpolation tells the processor to identify the Name attribute of the Model sub-element of the Order root-level data element.

ModelName="#\Order\Model.Name#"
Saving the script

Fill in the remaining attribute values in the output data by using interpolations to identify data in the incoming data source. The finished script is shown below. Save the following script to a file in the directory where X-Tract is installed, and name it DisplayWorkTicket.xst.

<XST __XMLscript="1.1" >
<_data file="PurchaseOrder.xml" />  
<WorkTicket Factory="#\Order\Manufacturer\Address\City#" >
	<Body ModelName="#\Order\Model.Name#" >
		<Engine >#\Order\Model\Engine#</Engine>
	</Body>
	<Paintshop >
		<BodyColor Description="#\Order\Model\Color#" />
		<Trim Description="#\Order\Model\Trim#" />
	</Paintshop>
	<Delivery >
		<Customer ID="#\Order.DealerID#" />
	</Delivery>
</WorkTicket>
</XST>
Running the script

At the command line, type xtract DisplayWorkTicket.xst. The output will be:

<WorkTicket Factory="Dearborn" >
	<Body ModelName="Mustang" >
		<Engine >1.8i</Engine>
	</Body>
	<Paintshop >
		<BodyColor Description="Black" />
		<Trim Description="Gold" />
	</Paintshop>
	<Delivery >
		<Customer ID="AB123" />
	</Delivery>
</WorkTicket>
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