Tips & TechniquesAbstract
This month's Tips and Techniques is designed to get you up and running using XSL. The new Microsoft Internet Explorer 5 implements an XSL processor that allows you to view XML inside a browser by attaching a style sheet. Here are four short steps to using XSL today. Get the softwareGo to the Microsoft IE installation area at http://microsoft.com/windows/ie/ie5 . The installation routine could be quite time-consuming on a slow line. The browser installation requires between 8 and 24 megabytes, depending on the options you choose. Microsoft considers XML to be a critical technology, so they include the XML and XSL processors in all installations. When you install IE5, the DOM objects and interfaces are loaded into your operating system as COM objects. That is, they are accessible to any program in your system, including server-side scripting, Java, Visual Basic, and other development environments. Both the XML and XSL processors are part of the DOM. Create your XML documentCreate a valid or well-formed XML document. If you can't decide, use the example in . Notice that the example is well-formed; sending valid documents to a browser is overkill. <?xml version="1.0"?> <article> <title>New High-Viscosity Mayonnaise To Aid In American Swallowing</title> <location>Englewood Cliffs, NJ</location> <paragraph>The act of swallowing will soon be easier for millions of food-shoveling Americans, thanks to QX-1, a revolutionary new high-viscosity/ low-friction mayonnaise developed by scientists at Hellmann's.</paragraph> <paragraph>The mayonnaise, which received FDA approval Monday and is set to hit the nation's shelves early next month, utilizes special lubricant additives and anti-breakdown agents to help keep America's high-intake gullets running smoothly and efficiently.</paragraph> <paragraph><quote>Americans' high-load, high-capacity eating puts a tremendous amount of stress on the alimentary canal </ quote>, Hellmann's mayochemical engineer Gerald Lund said. <quote>Often, when the canal is overtaxed, it can <quote>seize up</quote>, resulting in choking and, in some cases, total eater breakdown. QX-1 was formulated with today's harder-working ingestion in mind</quote>.</paragraph> </article>
XML Example
You can verify that this document is well-formed by loading it directly into the IE5 browser. When you do, you will see a collapsible representation of it, shown in .
Example of raw
XML in browser
Create your XSL stylesheetCreate an XSL stylesheet according to the Microsoft extensions. Yes, Microsoft extensions. There are pieces of the Microsoft XSL implementation that are not included in the W3CXSL working draft. The most obvious change is the lack for the formatting object half of XSL. Microsoft suggests integrating "well-formed HTML" with the XSL templates in order to create formatting effects. If you can't decide, use the stylesheet shown in .
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template>
<HTML>
<BODY STYLE="font-family:helvetica;font-size:12pt;">
<xsl:apply-templates/>
</BODY>
</HTML>
</xsl:template>
<xsl:template match="textnode()">
<xsl:value-of/>
</xsl:template>
<xsl:template match="paragraph">
<P><xsl:apply-templates/></P>
</xsl:template>
<xsl:template match="article/title">
<H1 style="color:green;font-size:24pt;font-style:bold">
<xsl:apply-templates/>
</H1>
</xsl:template>
<xsl:template match="location">
<DIV STYLE="text-transform:uppercase;font-size:75%;">
<xsl:apply-templates/>
</DIV>
</xsl:template>
<xsl:template match="quote">
'<xsl:apply-templates/>'
</xsl:template>
</xsl:stylesheet>
XSL stylesheet
XSL works by defining templates that match patterns in the text. Patterns can be as simple as a single element name ("paragraph" in this example), or can be identified by parent ("article/title" means any title that is a direct child of an article element) or many more ways. Notice that this example contains HTML markup (the tags in uppercase) that wrap around some XSL elements. The <xsl:apply-templates/> element will recursively apply other templates that are found in the style sheet. For example, the template for the paragraph element outputs a <P> tag with some style information, then has a <xsl:apply-templates/> rule. The paragraph contains other information, (the actual text of the paragraph) which is collected by the textnode() template, which actually processes the text. In this way, templates are recursive. Notice that our XML source has a quote that exists inside of another quote. There are two templates that handle quotes. The one matching the pattern "quote" surrounds the text with double quotation marks. The one matching the pattern "quote/quote" surrounds the text with single quotation marks. The order of these two templates is important, as XSL has rules concerning tie-breaking. That is, if more than one template matches a given event in the text, only one is selected. If this happens, the last one wins. So, when a quote comes in that is inside of another quote, the "quote/quote" pattern is matched, selecting the actions that follow. If these two template rules were swapped, the "quote/quote" rule would never be found. Load it into the browserYou need to indicate to the browser which style sheet the document will use. Add this line to your XML document immediately following the XML declaration: <?xml:stylesheet type="text/xsl" href="article.xsl"?> Then, just load the XML document and see the display shown in .
XML shown in browser with
XSL stylesheet applied
For more information on XSL support in the Microsoft browser, see http://microsoft.com/xml/xsl/reference/start.asp . <end/> |