XML technologies / XML / XML in a Nutshell / Cascading Style Sheets (CSS) / Associating stylesheets with XML documents
(Excerpt from "XML in a Nutshell" by Elliotte Rusty Harold & W. Scott Means)
CSS stylesheets are intended primarily for web pages. Browsers find a document’s stylesheet by looking for xml-stylesheet processing instructions in the XML document’s prolog. This processing instruction (Processing Instruction) should have a pseudo-attribute type with value text/css and a pseudo-attribute href, whose value is an absolute or relative URL locating the stylesheet document. This processing instruction associates the stylesheet in the code example A CSS stylesheet for recipes (rezept.css) with the file in the code example Marjorie Anderson’s recipe for Southern Corn Bread (cornbread.xml) if both are in the same directory:
<?xml-stylesheet type="text/css" href="rezept.css"?> Including the required pseudo-attributes type and href the processing instruction xml-stylesheet can have up to six pseudo-attributes:
typeThe MIME type of the stylesheet;
application/cssfor CSS andtext/xml(nottext/xsl!) for XSLT.hrefThe absolute or relative URL locating the stylesheet.
charsetSpecifies the stylesheet’s character set, such as UTF-8 or ISO-8859-7. This need not match the document’s character set. The names are the same as those used for the pseudo-attribute
encodingin the XML declaration.titleThis pseudo-attribute names the stylesheet. If several stylesheets are available, the browser may show readers a list of their titles and ask them to choose one, although this is not required.
mediaPrinted pages, televisions, and computer monitors are fundamentally different media requiring different styles. Comfortable screen reading, for example, requires much larger fonts than print. This pseudo-attribute specifies the media type for the stylesheet. There are ten predefined values:
screenprintttybrailletvembossedprojectionspeechhandheldall
By including several
xml-stylesheetprocessing instructions referencing different stylesheets and media types, you can prepare one document for many environments.alternateThis pseudo-attribute must have one of the values
yesorno.yesmeans this is an alternative stylesheet not normally used.noindicates the primary stylesheet, used unless users request another. The default value isno.
For example, this group of xml-stylesheet processing instructions could be placed in the recipe document’s prolog to make it accessible to a broader range of devices:
<?xml-stylesheet type="text/css" href="rezept.css" media="screen" alternate="no" title="Für Webbrowser" charset="US-ASCII"?>
<?xml-stylesheet type="text/css" href="druckbares_rezept.css" media="print" alternate="no" title="Für den Druck" charset="ISO-8859-1"?>
<?xml-stylesheet type="text/css" href="rezept_gross.css" media="projection" alternate="no" title="Für Präsentationen" charset="UTF-8"?>
<?xml-stylesheet type="text/css" href="tty_rezept.css" media="tty" alternate="no" title="Für Lynx" charset="US-ASCII"?>
<?xml-stylesheet type="text/css" href="rezept_klein.css" media="handheld" alternate="no" title="Für Palm Pilots" charset="US-ASCII"?>
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