XML Script logo up prev
Turning XML into HTML
Introduction

You can use XML Script to convert XML into HTML so that you can view it in your browser. If you have a local web-server, you can do this dynamically, so that the HTML is generated every time you view it. In this example, however, we will save the HTML to a static file, so that it can be viewed like any other HTML.

Import the source data

Use the _data command to open the existing XML file and read the contents in as data.

<_data file="PurchaseOrder.xml" />
Define the output file

Use the _output command to create a new file which will contain the output generated by this script. mode="replace" tells the _output command to replace the file DisplayPO.html if one already exists.

<_output file="DisplayPO.html" mode="replace" >
... you need to put your HTML in here ...
</_output>
Defining the HTML format

You will need to decide on the format of the HTML. The HTML shown below is standard, with one exception. Data values are not hard-coded: instead, we use interpolations which point to data items in the source XML. In this example we will display the contents of the Purchase Order. Note the interpolation in the BIG element, which will return the DealerID attribute of the Order root-level element. The BIG element includes both free text and an interpolation, which together will appear as text in the final output.

<HTML >
<BODY >
<BIG >Purchase order received from # \Order.DealerID #</BIG>

<TABLE >
<TR >
	<TD >Model</TD>
	<TD ># \Order\Model.Name #</TD>
</TR>
<TR >
	<TD >Engine</TD>
	<TD ># \Order\Model\Engine #</TD>
</TR>
<TR >
	<TD >Color</TD>
	<TD ># \Order\Model\Color #</TD>
</TR>
<TR >
	<TD >Trim</TD>
	<TD ># \Order\Model\Trim #</TD>
</TR>
</TABLE>

</BODY>
</HTML>
Well-formed XML

You must take care, when including HTML in an XML Script, that the HTML is 'well-formed' XML. This is because the XML Script template must itself be valid XML. What this means in practice is that each opening HTML tag must be matched by a closing tag, or the element must be represented by an empty-element tag. In this respect, XML processors such as X-Tract are more 'fussy' about their input than HTML browsers, who do not require certain HTML commands - for instance, <P> or <BR> - to include closing tags. There are two ways round this. The simplest is to convert these into well-formed XML tags - in the case of <P> this would require putting an end-tag (</P>) after the paragraph as well as the start-tag before the paragraph, and in the case of <BR> this involves converting to an empty-element tag like this - <BR />. The other option is to 'disguise' the angle brackets so that X-Tract does not recognise them as tags. This is done using standard HTML entity definitions: for the left angle bracket, use the ampersand ('&') followed immediately by 'lt;'. For the right angle bracket, use ampersand together with 'gt;'. Your browser will recognise these and interpret them as being part of the HTML code.

Saving the script

The finished script is shown below. Save the following script to a file in the directory where X-Tract is installed, and name it XMLtoHTML.xst.

<XST __XMLscript="1.1" >
<_data file="PurchaseOrder.xml" />
<_output file="DisplayPO.html" >
<HTML >
<BODY >
<BIG >Purchase order received from # \Order.DealerID #</BIG>

<TABLE >
<TR >
	<TD >Model</TD>
	<TD ># \Order\Model.Name #</TD>
</TR>
<TR >
	<TD >Engine</TD>
	<TD ># \Order\Model\Engine #</TD>
</TR>
<TR >
	<TD >Color</TD>
	<TD ># \Order\Model\Color #</TD>
</TR>
<TR >
	<TD >Trim</TD>
	<TD ># \Order\Model\Trim #</TD>
</TR>
</TABLE>

</BODY>
</HTML>
</_output>
</XST>
Running the script

At the command line, type xtract XMLtoHTML.xst. This will create a file, DisplayPO.html, with the following content:

<HTML >
<BODY >
<BIG >Purchase order received from AB123</BIG>

<TABLE >
<TR >
	<TD >Model</TD>
	<TD >Mustang</TD>
</TR>
<TR >
	<TD >Engine</TD>
	<TD >1.8i</TD>
</TR>
<TR >
	<TD >Color</TD>
	<TD >Black</TD>
</TR>
<TR >
	<TD >Trim</TD>
	<TD >Gold</TD>
</TR>
</TABLE>
</BODY>
</HTML>
Viewing the HTML

Point your browser at the DisplayPO.html file. The browser should display:

Purchase order received from AB123
Model Mustang
Engine 1.8i
Color Black
Trim Gold

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