- 1). Design the XHTML layout within which you want to display your XML. Using graphic design programs or just a pencil and paper, sketch the appearance that you want your XML data to have when it's contained within XHTML structures. Figure out which XHTML elements you want to use in order to best present the types of data your XML contains. Remember that XHTML is more strict than HTML, and your final markup will be able to contain only four XHTML tags.
- 2). Include a link to an XSLT document in your XML:
<?xml-stylesheet type="text/xsl" href="/links/?u=my_style.xsl"?>
Create a new file in a text editor and save it as "my_style.xsl" to match the reference in your XML. This is where you will list the rules that you want to apply to the XML data, and within what XHTML markup you want the XML data elements to be contained. Enter the following basic outline for your XSLT:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" omit-xml-declaration="yes" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>
</xsl:stylesheet> - 3). Create the basic structure of an HTML page to transform your XML elements into XHTML elements. In your XSLT, before the "</xsl:stylesheet>" line, enter the following to create a template for your document:
<xsl:template match="/">
</xsl:template>
This applies to the entire XML document. Between the opening and closing "<xsl:template>" tags, enter the structure of a Web page:
<html>
<head></head>
<body>
</body>
</html>
This simply creates an XHTML page outline in which to present your data. - 4). Transform each element in your XML into an XHTML structure. In your XSLT, between the "<body>" tags, enter code to turn XML content into XHTML using the following syntax:
<xsl:apply-templates/>
After the "</xsl:template>" line but before "</xsl:stylesheet>" at the end of the document, enter specific instructions for your XML elements as in the following example:
<xsl:template match="person">
<div><xsl:apply-templates/></div>
</xsl:template>
This would identify an element "<person>" in the XML and present its contents within a "<div>" XHTML element. - 5). Test your XHTML output by opening your XML document in a Web browser. View the XHTML page source using your browser menus and extensions if you have any, checking that the structures and syntax you want are present. Validate your XHTML to check that it is well formed, as this is essential when you present XHTML content. If your XHTML does not match the design you had in mind, or has elements missing from the original XML, go back to the XSLT and reconsider its structure.
SHARE