Here is an example of a well-formed and valid XML document that is transformed by a simple XSL file to create an HTML file for presentation on the Web. The three-part example below explains the XML document, how it's transformed by XSL, and the resulting HTML produced. Keep in mind that this is an example for starting out, not an in-depth tutorial on XML/XSL. Please refer to the Tips section of this Web site for links to more comprehensive tutorials.
The XML sample shown below adheres to the DocBook standard. Notice how the elements (<book>, <chapter>, <title>, <para>) describe the structure of the content. This XML file contains text that would typically be displayed in an "About Us" section of a Web site. The line numbers have been added for easy reference to the explanation of the file.
1. <?xml version="1.0" encoding="UTF-8"?> 2. <?xml-stylesheet type="text/xsl" href="modest_xml.xsl"?> 3. <book label="CTG"> 4. <title>About Us</title> 5. <chapter label="mission"> 6. <title>Mission</title> 7. <para> 8. The <emphasis>Center for</emphasis> Technology in Government works with government to 9. develop information strategies that foster innovation and enhance the quality and 10. coordination of public services..asp 11. </para> 12. <para> 13. We carry out this mission through applied research and partnership projects that address the 14. policy, management, and technology dimensions of information use in the public sector. 15. </para> 16. <para> 17. The results generated by each project add to a growing knowledge base designed to support 18. the work of both government professionals and academic researchers. Our guides, reports, 19. and tools are freely available on our <ulink url="/publications">Publications</ulink> page. 20. </para> 21. </chapter> 22. </book>
Line 1 above is the standard XML declaration that identifies this file as an XML document and the character encoding set used. Line 2 above links the XML file to the XSL file that will transform and display it as an HTML page. This is the simplest way to connect an XML file with its XSL file. Other file management methods are explained in the Environment section of this Web site. Line 3 contains the root element of this XML document. The remaining lines contain all the other elements of this XML document, properly nested according to the DocBook specification and the XML standard.
Unlike HTML, the XML file above contains no markup code that will display it on a Web page as HTML (or any other format or medium). However, its standard and consistent structure allows it to be transformed into a variety of formats and media, including HTML for a Web browser. That's where XSL comes in as seen in the example below.
1. <?xml version="1.0" encoding="UTF-8"?> 2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 3. <!-- Match modest_xml.xml file document node, called "book" into html page tags --> 4. <xsl:template match="book"> 5. <!-- Print DOCTYPE declaration at top of HTML document --> 6. <xsl:text disable-output-escaping="yes"> 7. <![CDATA[ 8. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 9. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 10. ]]> 11. </xsl:text> 12. <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"> 13. <head> 14. <title> 15. <xsl:value-of select="title"/> 16. </title> 17. </head> 18. <!-- Create the Main Body of the HTML page --> 19. <body> 20. <h1> 21. <xsl:value-of select="chapter/title"/> 22. </h1> 23. <xsl:for-each select="chapter/para"> 24. <p> 25. <xsl:value-of select="."/> 26. </p> 27. </xsl:for-each> 28. </body> 29. </html> 30. </xsl:template> 31. </xsl:stylesheet>
Because XSL files are XML documents, they also begin with the XML declaration in Line 1. Line 2 identifies this file as an XSL stylesheet. The "template match" in Line 4 goes back to the XML file and gathers all the content in that file that is enclosed within a "book" element. (Since "book" is the root element of our example XML file, this XSL example gathers all the content from the XML file.) The remaining lines contain many HTML elements because this XSL file is creating an HTML page. The actual content for the HTML page is selected from the XML file via the "xsl:value-of select" elements. No actual text content is contained in this XSL file. Rather, it refers back to the XML file and selects content contained within the referenced elements (such as chapter/title and chapter/para). The advantage in this XML/XSL approach is that this single XSL file could transform any number of individual XML files into HTML as long as those XML files were structured in a similar manner (with book, chapter, title, para elements).
As shown in the example below, the XML/XSL combination above produces an HTML file. Notice that in the resulting HTML file the actual text content from the XML source file has been inserted within the HTML elements that were contained in the XSL file.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"> <head> <title>About Us</title> </head> <body> <h1>Mission</h1> <p> The Center for Technology in Government works with government to develop information strategies that foster innovation and enhance the quality and coordination of public services. </p> <p> We carry out this mission through applied research and partnership projects that address the policy, management, and technology dimensions of information use in the public sector. </p> <p> The results generated by each project add to a growing knowledge base designed to support the workof both government professionals and academic researchers. Our guides, reports, and tools are freely available on our Publications page. </p> </body> </html>
The simple Web page formatted and processed by the XML/XSL files above would appear like the sample shown below.