Table of Contents
<?xml version="1.0"?> <document> <body>You've probably heard the propaganda by now: XML blesses you with a way to separate content from presentation.</body> </document>
<xsl:template match="/">
<html>
<body>
<p><xsl:apply-templates/></p>
</body>
</html>
</xsl:template>
<xsl:template match="/"> element uses the value of its match attribute to find a node in the XML source. The forward slash operator, an XPath expression, specifies the document's root node. The rule could also match on the same node by specifying it explicitly in the template rule, that is, <xsl:template match="document">. By matching on the root node of the document, we're able to build an HTML container that provides the skeleton code (here, just <html> and <body>) for our document. Below, I will expand on this concept.
<xsl:apply-templates> element invokes a built-in XSLT template rule that processes the children of the matched node, meaning roughly that it outputs the children. Because there is only one child of the <document> node in our XML file, <xsl:apply-templates> suffices to print the meager contents of the file. If, however, the <document> element contained more children, <xsl:apply-templates> would print them all out, too, and we would want additional template rules to control how they are formatted.
<xsl:stylesheet> element, which all XSL stylesheets require as their top-level element, and set a namespace for it (note that some versions of Internet Explorer and the MSXML parser may require a different namespace; see http://msdn.microsoft.com/ and the Unofficial MSXML XSLT FAQ at http://www.netcrucible.com/xslt/msxml-faq.htm for details):
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<html>
<body>
<p><xsl:apply-templates/></p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0"?> <?xml:stylesheet type="text/xsl" href="my_stylesheet.xsl"?> <document> <body>You've probably heard the propaganda by now: XML blesses you with a way to separate content from presentation.</body> </document>
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0" >
<xsl:template match="document">
<html>
<head>
<link rel="stylesheet" type="text/css" href="doc.css"/>
</head>
<body>
<p><xsl:value-of select="body"/></p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
This stylesheet first matches the root node explicitly by name (document), builds an HTML container for it as before, and then uses the <xsl:value-of> element to select and output the text contents of the message node. The select attribute identifies the element whose contents are to be processed.
Cascading Style Sheets: The Definitive Guide, published by O'Reilly, provides a detailed account of how to use CSS. The W3C CSS specifications are available at http://www.w3c.org/Style/CSS/.
The XSLT stylesheet is also based on the DTD. Why bring a DTD into this discussion? The answer is that it's best to build the stylesheet based on your DTD to ensure that all elements and attributes are processed fully and according to your requirements. The DTD provides the cues by which you build your stylesheet. That is, to build a suitable stylesheet -- one that processes all the elements and attributes in your XML document fully and appropriately -- you should develop your stylesheet based on your DTD, not on a mere XML document alone. For more information about analyzing DTDs to develop stylesheets, see Chapter 21, DTD Analysis, in The XSL Companion, by Neil Bradley (Addison-Wesley).
<?xml version="1.0"?>. The next line contains an optional document type declaration for the stylesheet to specify the stylesheet's root element, xsl:stylesheet. I'm including the document type declaration in our stylesheet because I want to declare several general entities as an internal DTD subset for use in the stylesheet. (Recall that general entities let you replace an entity with its value; that is, wherever the entity pub.date appears in the stylesheet as text, it is replaced by its value as defined in the entity declaration, here June 1, 2002.)
<!DOCTYPE xsl:stylesheet [ <!ENTITY pub.date "June 1, 2002"> <!ENTITY mdash "--"> <!ENTITY nbsp " "> ]>
XML in a Nutshell for additional information about the namespace):
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" doctype-public="-//W3C//DTD HTML 4.0 Transitional//EN"/>
<xsl:template match="document">
[...HTML SKELETON CODE...]
</xsl:template>
<document>, and circumfuses it with HTML code, forming a skeleton that will contain the body of the XML document. In the case of this stylesheet, the skeleton is in the form of a header, a content area, and a footer. <xsl:value-of select="docinfo/title"/> -- to select the title of the XML document and to output it in the title slot of the HTML document that I'm building.
<head> tags I've also created an HTML link to a Cascading Style Sheet, which contains as much as possible of the styling information for my HTML formatting:
<link rel="stylesheet" type="text/css" href="doc.css"/>

In the next XSL command, I again select the value of the <title> tag in the XML document, this time for use as a headline atop the HTML document:
<xsl:value-of select="docinfo/title"/>
<xsl:apply-templates select="body"/>
<xsl:template match="body">. This template does three things: It inserts a Table of Contents headline, calls another template that dynamically builds a table of contents, and processes all <div1> elements. The <xsl:call-template name="toc"/> command calls a macro (located toward the end of the stylesheet in the Macros section) that cycles through the six levels of <div> elements, makes a reference to the <div> element's heading in the form of a hyperlink, and numbers the section headings using the 1.1 format. (Here's an example of how the HTML output looks; the hyperlinks are colored brown but are not underlined.)
XSLT Programmer's Reference at
http://www.wrox.com/. In IE5.0 or greater, you can view the XML source code for the W3C's XML Recommendation at http://www.w3.org/TR/2000/REC-xml-20001006.xml; or you can download it and view it using an editor like XML Spy.
<div> elements, mapping the <head> element in <div1> to the HTML heading element <h1> and so forth through <div6> and <h6>. The template-matching rules for the head elements also call another template, named "head", which inserts an ID attribute in each heading and numbers them with the command mode="number".
<div> section, the stylesheet uses the following code to process all the children of the <div1> element:
<xsl:template match="div1">
<xsl:apply-templates/>
</xsl:template>
<xsl:apply-templates/> simple tells the XSLT processor to process all the child elements of <div1> and to output them according to the template rules for each child element listed further down in the stylesheet. Many of these child elements are processed simply by what I've called the XHTML Quick Print Module. This template rule selects any of the elements listed in the value of the match attribute, makes a copy of the element, makes a copy of all the element's attributes, and simply passes them through to the target output file intact. Because my DTD contains several XHTML elements for low-lying nodes, I can use this template rule instead of writing a separate template rule for each XHTML element.
Learning XML.
XML in a Nutshell.XML authoring tools like
XMetal, Epic Editor, and XML Spy empower writers to separate content from presentation.
About the Author

|
Copyright © 2004 Steve Hoenisch and Criticism.Com. All rights reserved. |