|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
XML Protocols Got XSLT?
Got XSLT?
By: Shouki Souri
Nov. 1, 2001 12:00 AM
Extensible Stylesheet Language Transformation (XSLT) is part of the XSL standard. The most popular use of XML and XSL is to create a separation of content and presentation. Although several XML/XSLT articles and tutorials are available, none really explain and demonstrate the true flexibility and ease of transforming a single XML document into data formats such as HTML, WML, and VoiceXML. XSLT provides an easy, efficient transformation for simple applications. In this article we'll focus on marking up a book catalog using XML and Microsoft's Internet Explorer 5.0 browser to perform the transformation. I prefer to use XML in our example because we can create other sets of HTML, WML, or even VoiceXML pages from the same XML document. I'll start with HTML; subsequent articles will deal with the other formats. I'm assuming readers are familiar with HTML, XML, and XSLT (see the Resources section for more background information). In this tutorial I'll focus on creating XML and XSL documents and the use of an IE 5.0 XML parser and XSL engine. IE 5.0 is currently the only widely available browser that contains an XML parser with an XSL engine. The example code in this tutorial will work only in IE 5.0 or later. XML and XSL are increasingly used to enable customized data presentation for different browsers, devices, and users. By using XML documents along with XSL stylesheets on the client, middletier, or server, we can transform, organize, and present XML data tailored to individual users for a variety of client devices, including graphical and nongraphical Web browsers, PDAs such as Palm Pilots, digital cell phones, and pagers. In doing so, we can focus on business applications, without concern for the kind of output devices that will present the data now or in the future. XSL actually consists of three languages:
XSLT can be used on either the back end or within the browser. IE 5.0 has an XSLT processor built in, but it isn't 100% compatible with the latest release of the W3C XSL standard. (See sidebar or Resources section for more information.)
Creating the XML Document
Let's create an XML document representing a catalog of books. Start with your data in its current form. As an example, my data format is a yellow sticky note plastered on my library wall, as follows:
Enterprise JAVABEANS Since XML is a text-based markup language, we can use any text editor to create the XML document. XML authoring editors can help create XML elements and tags, such as the freely available Microsoft XML Notepad (see Resources). Listing 1 illustrates book information in XML syntax - mybooks.xml. The XML document contains the following additional information: ISBN, CATEGORY, and RELEASE_DATE. You're free to add or delete data to meet your catalog preference. We won't use a DTD to validate our ".xml" file since we're not interested in validation in this article. If you load the XML document as written in Listing 1, using IE 5.0, you'll see the display shown in Figure 1. This is a raw form of XML document. In fact, IE 5.0 had applied a default stylesheet to an XML document without any stylesheet specified.
The Goal
Creating the Stylesheet Document
XSLT is basically a declarative high-level language. It has several programming-like features such as if, choose-when-otherwise, sort, filter, for-each, and many others. Listing 2 shows a complete XSL file to render our XML file into HTML file. In Listing 1 if we uncomment a reference of our XSL stylesheet (line number 3) by simply removing "<!---"\" and "-->" around the statement so it appears as:
<?xml-stylesheet type="text/xsl" href= and then load the XML document using IE 5.0 browser, we'll see the HTML page as displayed in Figure 2.
Understanding Our Stylesheet
<?xml version="1.0"?> This indicates XSLT is an XML document, and will comply with the XML syntax.
<xsl:stylesheet Next, we define the namespace for the XSL stylesheet with the <xsl:stylesheet> element. <xsl:template match="/"> The template has been wrapped with match="/" to indicate that it corresponds to the root (/) of the XML source document. <xsl:for-each select="BOOKS/BOOK"> The XSL <xsl:for-each> element locates elements in the XML document and repeats a part of the template for each one. <xsl:value-of select="CATEGORY"/> The XSL <xsl:value-of> element can be used to select the value of the element matched by the select predicate. The XSL stylesheet then closes all open XML tags in order, with no nesting allowed.
Conclusion
Enterprise JAVABEANS Resources
Additional Information
The main reason I'm working with MSXML 2.0 in this tutorial is because IE 5.0 with MSXML 2.0 is still widely in use on the Internet. Some developers aren't even aware of the incompatibility issue, and waste valuable time and effort getting the transformation to work. Additionally, having to download MSXML 3.0 or a newer version of MSXML isn't necessary for our simple example. In fact, the only visible difference you'll find is the XSL stylesheet declaration. This is the standard way, from the W3C XSL Recommendation:
<xsl:stylesheet xmlns:xsl="http:// This is the (incorrect) IE 5.0 with MSXML 2.0 (from the XSL Working Draft):
<xsl:stylesheet xmlns:xsl="http:// Reader Feedback: Page 1 of 1
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week
Breaking Cloud Computing News
|
|||||||||||||||||||||||||||||||||||||||||||||||||