Converting an XML Document into an HTML Document | KeyContent.org.... Unlocking Communication
Converting an XML Document into an HTML Document
Print
or How to Transform an XML Document into an HTML Document (for a browser)

This tutorial was created by Richard Bomstein.

As an overview of the process, here is a diagram showing the steps.

Image


The following is an example of a simple XML document.


<?xml version="1.0"?>
<greeting>
Hello, World!
</greeting>


• The first line (above) defines the XML version ("1.0") this document is based on.
• The second line, <greeting>, looks like a start tag in HTML. In XML we call <greeting> an element.
• Tags and elements both have to close with a closing (/) indicator. In the fourth line, note how </greeting> has a closing (/) indicator.
• XML stands for extensible markup language; you can extend and invent new elements and markup languages in XML. In HTML you live with the tags that already exist.
• Keep this in mind: The <greeting> element (above) is called the document’s root. It appears first after the version statement and it contains all document content.


Next we look at a stylesheet, the traffic cop that tells the code how to build an HTML document. This stylesheet example is from Doug Tidwell's book "XSLT".


Below I provide comments (<!directly above the code>) to explain how a stylesheet helps build an HTML document.

<!The opening XSL:stylesheet element begins our stylesheet document>

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<!The XSL:output element defines HTML as the output format>

<xsl:output method="html">

<!The XSL:template element tells the code to go to the XML document’s root ("/") and locate greeting elements that will be transformed.>

<xsl:template match="/">
<xsl:apply-templates select="greeting "
</xsl:template>

<!This time the XSL:template element tells the code to transform the XML greeting element to the 6 HTML tags below and copy these tags to the output document.>

<xsl:template match="greeting ">
<html>
<body>
<h1>

<!-- The XSL:value-of element tells the code to print out the text inside the greeting element and to place this text inside the h1 tags in the output document.-->

</xsl:value-of select=".">

</h1
</body>
</html>
</xsl:template>

<!The closing XSL:stylesheet element concludes our stylesheet document>

</xsl:stylesheet>

Now we’ll see how a command from the DOS prompt can be used to generate the HTML document.

This DOS command transforms an XML document into an HTML document. Name the XML document and the XSLT stylesheet in the command (Saxon is a processor that transforms an XML document).

Here's the DOS command:


C:\> saxon greeting.xml greeting.xsl


After you run the command, this is the generated HTML document.

<html>
<body>
<h1>
Hello, World!
</h1
</body>
</html>


And this is how the HTML document displays in a browser.


Hello, World!


{SUBMIT()}{SUBMIT}




Contributors to this page: Bill Albing .
Page last modified on Tuesday, October 09, 2007 07:07:12 pm EDT by Bill Albing.

Key Pick New

Bill Albing has some suggestions to improve STC competitions. Read and let us know, what do you think?

Other Key Picks... Read more

About KeyContent

About KeyContent
KeyContent.org is an idea space where you can express your insights about your profession. Think of this site as a white board with a brain. You create and edit articles or portals to other sites and share your insights... Read More


Key Ads

Key Connections

Join KeyContent on these networks:



Key Promotions



Key Products