Stage Two

How tags are controlled by XSL

XSL is the real power behind XML. 90% of your code will be in writing and perfecting the XSL. Remember, the XSL is the template for the entire site. It is entirely up to you how restrictive you make it, or how expansive, although XML lends itself to being restrictive first - it will only do what the XSL dictates.

XSL starts with a template definition. This contains all the static HTML code - all the backgrounds, general purpose javascript, mime tags, head and body structure, DHTML, CSS links and content, and anything else which does not need to change between documents using this XSL stylesheet.

At certain points within the code, you enter an xsl command to add data from the XML file by applying a template defined in the second part of the XSL file. This fine tunes the code, adding tags, attributes, text, links, images, etc. The code to start an xsl stylesheet is always the same:

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">

Now we add the contents of the top level template (the / match) - this is the place for the static HTML code. Say we add <html> <head> <title>test</title> </head> <body>

Now we can add the tags for the form itself - everything except the password and text controls. Make sure you know where the controls will need to go in the code so that the template can be applied in the right place. Depending on the configuration of the server, you are likely to find that the script that processes this form will stall when trying to read data from the last field in the form. For this reason, add a hidden input with a junk name and junk value as the last field before closing the form. e.g. <input type="hidden" name="foo" value="bar">. Now finish the HTML using </body> </html> as usual and close the top level template with an xsl commmand: </xsl:template>

There are two parts to applying the templates: Adding the marker in the top level template to dictate where the code is added and defining the template to be applied which contains the actual HTML to be inserted, complete with changes relevant to the XML document being viewed. So add the marker in the top level template - I did remind you to remember where the controls need to go! Add this code:

<xsl:apply-templates select="PASSWORD" />

Now add the following code at the END of the XSL file, AFTER the </xsl:template> tag.

<xsl:template match="PASSWORD">
<input type="password" name="pass" size="10" />
</xsl:template>

So, select the template to insert within the top-level template. Match the template outside. Now every time a <PASSWORD /> tag occurs in the XML file, the password input control will be inserted at the equivalent point in the final HTML.

To change the attributes of a tag in the final HTML, move to stage three. Note: The XSL is not complete at this stage - you will receive an error if you try to use it to view an XML page.



This is part of www.codehelp.co.uk Copyright © 1998-2004 Neil Williams
See the file about.html for copying conditions.