How to Write a Web Page Using XML
The key to creating a Web page using XML is to understand that XML elements must be converted into HTML in order view your XML as a Web page. That's where the XSL transformation (XSLT) language comes in. XSLT lets you place instructions in a style sheet that reads your XML elements, formats them and outputs your elements in HTML. This process is called transformation.
The precise steps will depend upon the development tools you use, and how your transformation will be deployed. Typically, transformations of this type are executed on a Web server. However, it is possible to perform a transformation directly in an XML-enabled browser, such as Firefox or Internet Explorer.
- Difficulty:
- Moderately Challenging
Instructions
-
Creating a Web Page Using XML
-
1
Create a "well-formed" XML document containing elements.
-
2
Place the following instruction directly below your <?xml version=""?> declaration.
<?xml-stylesheet type="text/xsl" href="my_xslt_file.xsl"?> -
3
Save your document and give it a .xml file extension.
-
4
Create a standard XSL style sheet.
-
5
In the style sheet, set the output method to "HTML" as follows:
<xsl:output method="html" /> -
6
Create a template that matches an element in your XML, then select its value as follows:
<xsl:template match="item">
<xsl:value-of select="item"/> -
7
Add HTML directly to the template to further format your Web page. For example, placing a <br /> element in the template will insert a line break in the final Web page.
-
8
Insert the closing tag for your XSL template as follows:
</xsl:template> -
9
Save the file as my_xslt_file.xsl
-
10
Point your XML-enabled browser to the XML file you created. The browser will transform the document using the style sheet and display the resulting Web page in your browser window
-
1
Tips & Warnings
Click on "View Source" in your browser to view the HTML that was generated.
If you encounter errors, check the syntax of both your XML and XSL files.
Be sure to use an XML-enabled browser such as Firefox, Opera, or Internet Explorer (6.0 or later)
You can apply style sheets programatically. The method used to apply your style sheet depends on the programming language (Java, C#, perl) you are using.
Server technologies such as Java Server Pages (JSP) and Active Server Pages (ASP)may also affect the method for applying a style sheet programatically.