The XML Recommendation defines a way to structure, store, and send information, but it says nothing about how the content should be displayed. Stylesheets store information indicating how an XML document should appear to a user. For example, a stylesheet can define how the source content should be styled, laid out, and paginated onto some presentation medium. The presentation medium could be a window in a Web browser or a handheld device, or it could be printed pages in a book or other form of print media.
Different stylesheets can produce radically different appearances for the same document. Cascading Style Sheets and Extensible Stylesheet Language style sheets [XSL] are both examples of stylesheet languages that you can use with XML documents. Conversely, because they can be extracted from the data, any number of XML documents can share stylesheets. 3.7.1 Cascading Style SheetsCascading Style Sheets (CSS) were introduced in 1996 as a standard way to add information about style properties to HTML documents. Two W3C documents make up the CSS family of recommendations: Cascading Style Sheets, Level 1 [CSS1], and Cascading Style Sheets, Level 2 [CSS2]. CSS is a simple declarative language that allows authors and readers to attach styles such as fonts, colors, and spacing to HTML or XML documents. The CSS language is human readable and writable, and it expresses styles in common desktop publishing terms. CSS stylesheets "cascade," which means that authors can attach one style sheet to the document and the reader, depending on his or her preference, can attach a different stylesheet. For example, a reader might attach a stylesheet that accommodates human or technological handicaps. Although we do not go into that level of detail here, the CSS recommendations specify the rules for resolving conflicts between different stylesheets. CSS2 builds on CSS1 by supporting media-specific style sheets. Authors can tailor their documents for presentation on various media such as visual browsers, aural devices, printers, Braille devices, and handheld devices. CSS allows the rendering of elements by associating them with properties such as font size, font weight, color, and values. For instance, in Example 3-13, CSS renders the "Hello" element as a block-level element in 24-point bold blue text. Example 3-13 Use of CSSHello { Display: block; Font-size: 24pt; Font-weight: bold; Color: blue; } 3.7.2 Extensible Stylesheet LanguageThe Extensible Stylesheet Language (XSL) W3C Recommendation [XSL] defines the XSL language for expressing stylesheets. XSL builds on the prior work on Cascading Style Sheets Level 1 [CSS1] and Level 2 [CSS2] and the Document Style Semantics and Specification Language [ISO 10179]. XSL was developed to give designers greater control over needed features during pagination of documents and to provide an equivalent "frame"-based structure for browsing on the Web. It also incorporates the use of the XLink language [Xlink] to insert elements into XML documents that create and describe links between resources. XSL uses the XSL Transformations [XSLT] W3C Recommendation, formatting objects, and the XML Path Language W3C Recommendation (see Chapter 6) for tree construction, format control, and pattern selection. This approach provides detailed control over the presentation of portions of the source content and the association of properties with those content portions. An XSL stylesheet processor accepts a document or data in XML and an XSL stylesheet, then produces the presentation format of that XML source content as specified by the designer of that stylesheet. This process involves two steps:
In Example 3-14, XSL transforms the Hello element to 24-point bold blue text. Example 3-14 Use of XSL<?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="Hello"> <FONT COLOR="blue" SIZE=24> <B><xsl:apply-template/></B> </FONT> </xsl:template> </xsl:stylesheet> 3.7.3 XSL Versus CSSBoth XSL and CSS are formatting languages, but they differ in several respects. CSS is a simple formatting specification. It reads each XML element as it is scanned in the document and applies styles in that order. CSS doesn't change the structure of the XML; it merely changes the visual appearance of each node. In addition, CSS does not allow the user to access or display the content of the attributes. It can apply only to the elements' content, not their attributes. XSL is more of a real programming language. That is, it allows the user to rearrange, filter, and add elements. XSL is more flexible and powerful than CSS, and better suited to XML documents except for simple display cases. This language allows the user to access and display the content of the attributes easily. The user can take an XML document and convert it to another XML document, an HTML document, an ASCII text file, or even a proprietary text format. |