XML technologies / XML / XML in a Nutshell / Cascading Stylesheets (CSS)
(Excerpt from "XML in a Nutshell" by Elliotte Rusty Harold & W. Scott Means)
Most element names describe the semantic meaning of their content. Often, however, that content must be formatted and presented to users. This requires a step that applies formatting information to the XML document and converts semantic markup into presentation markup. Many syntaxes are available for this presentation layer. Two deserve particular mention:
Cascading Style Sheets (CSS)
XSL Formatting Objects (XSL-FO)
CSS is a non-XML syntax used to describe the appearance of particular elements in a document. CSS is a very direct language: no transformation takes place. The document’s parsed character data is displayed more or less exactly as it appears in the XML document. Of course, you can first transform the document with XSLT and then apply a CSS stylesheet if its content needs rearranging before presentation. A CSS stylesheet does not change the XML document’s markup; it only applies styles to existing content.
By contrast, XSL-FO is a complete XML application describing the layout of text on a page. Its elements represent pages, text blocks, graphics, horizontal rules, and much more. You normally do not work with it directly. Instead, you write an XSLT stylesheet that transforms the document’s markup into XSL-FO. The application preparing the document for display reads the XSL-FO and presents it to the user.
In this section, we demonstrate the features and properties of CSS by applying them to the simple, well-formed XML document in the following code example. This document has no document type declaration, although a DTD or schema could easily be added. In general, DTDs and schemas do not affect stylesheets except by changing the document’s content through entity declarations, default attribute values, and so on.
<?xml version="1.0" encoding=ISO-8859-1" standalone="yes"?>
<?xml-stylesheet type="text/css" href="rezept.css"?>
<rezept quelle="Marjorie Anderson">
<gericht>Southern Corn Bread</gericht>
<zutaten>
<zutat>
<menge>1 Tasse</menge>
<komponente>Mehl</komponente>
</zutat>
<zutat>
<menge>4 Esslöffel</menge>
<komponente>Backpulver</komponente>
</zutat>
<zutat>
<menge>½ Teelöffel</menge>
<komponente>Salz</komponente>
</zutat>
<zutat>
<menge>1 Tasse</menge>
<komponente>Maismehl</komponente>
</zutat>
<zutat>
<menge>1 ½ Tassen</menge>
<komponente>Vollmilch</komponente>
</zutat>
<zutat>
<menge>4 Esslöffel</menge>
<komponente>weiche Butter</komponente>
</zutat>
</zutaten>
<anweisungen>
<schritt>Mehl, Backpulver, Zucker & Salz sieben und gut mischen.</schritt>
<schritt>Geben Sie 1 Tasse Maismehl hinzu.</schritt>
<schritt>Verquirlen Sie ein Ei in einer Tasse, und fügen Sie das verquirlte Ei sowie 1 ½ Tassen Vollmilch hinzu, um einen Teig zu erhalten. Gut rühren.</schritt>
<schritt>Geben Sie das weiche Backfett hinzu, und schlagen Sie den Teig, bis sich alles gut vermischt hat.</schritt>
<schritt>Füllen Sie ihn in eine gefettete flache Form oder in ein gefettetes Muffin-Blech.</schritt>
<schritt>Backen Sie das Brot im heißen Ofen bei <temperatur>220° C</temperatur> für <dauer>25 Minuten</dauer>.</schritt>
<schritt optional="yes">Falls Sie das Brot in einer flachen Form gebacken haben, schneiden Sie es in Quadrate.</schritt>
</anweisungen>
<geschichte>Nach dem Tod meiner Schwiegermutter <person>Marjorie Anderson</person> haben Beth und ich dieses Rezept auf der Seite für "eigene Rezepte" in einem regionalen Kochbuch in ihrem Schrank gefunden. Es wurde von den Kirchenfrauen der Episcopal Church of Ascension, <stadt>Mt. Sterling</stadt>, <staat>Kentucky</staat>, veröffentlicht.</geschichte>
</rezept>Code example: Marjorie Anderson’s recipe for Southern Corn Bread
Copyright © 2005 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from the book "XML in a Nutshell" is subject to the same conditions as the printed edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de
back to top