3.7 XML and Stylesheets

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.


Stylesheets affect security because they must be included under a signature if the signature is meant to securely indicate approval of information as presented to a user. In addition, the transform (XSLT) part of XSL, as described in Section 3.7.1, is used as a transformation algorithm to change the structure of XML being presented.

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 Sheets

Cascading 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 CSS
 Hello {   Display: block;   Font-size: 24pt;   Font-weight: bold;   Color: blue; } 

3.7.2 Extensible Stylesheet Language

The 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:

  • Constructing a result tree from the XML source tree (referred to as tree transformation). In XSL, this tree is called the element and attribute tree. It represents a formatting object as an XML element, with the properties being represented by a set of XML attribute value pairs. The XSL Transformations W3C Recommendation [XSLT] defines tree transformation.

    With tree transformation, the structure of the result tree can differ quite dramatically from the structure of the source tree. During the construction of the result tree, the source tree can be filtered and reordered. Also, an arbitrary structure and generated contents may be added. Including formatting semantics as formatting objects in the result tree specifies the desired formatting. Numerous classes of formatting objects are available for this purpose.

  • Interpreting the result tree to produce formatted results suitable for presentation on a display, on paper, in speech, or onto other media.

    Each formatting object represents a specification for a part of the pagination, layout, and styling information. When formatting of the entire result tree occurs, the process applies the particular specifications to the content of that formatting object. Each formatting object class represents a particular kind of formatting behavior.

    Properties associated with an instance of a formatting object control the formatting of that object.


The XSL Recommendation [XSL] states that XSL processors must use the XML namespace mechanism [Names] to recognize elements and attributes from this namespace. Elements from the XSL namespace are recognized only in the stylesheet, not in the source document. Implementers must not extend the XSL namespace with additional elements or attributes. Instead, any extension must take place in a separate namespace.

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 CSS

Both 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.

Secure XML(c) The New Syntax for Signatures and Encryption
Secure XML: The New Syntax for Signatures and Encryption
ISBN: 0201756056
EAN: 2147483647
Year: 2005
Pages: 186

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net