XML

Wiring a Style Sheet to an XML Document

CSS style sheets used with XML documents are usually referred to as external style sheets because they are stored in separate text files with a .css extension. The .css file is then referenced by XML documents that use the style sheet to determine how their content is displayed. The xml-stylesheet processing instruction is used to associate an external style sheet with an XML document. This processing instruction includes a couple of attributes that determine the type and location of the style sheet:

  • type The type of the style sheet (text/css, for example)

  • href The location of the style sheet

These two attributes are both required in order to wire a style sheet to an XML document. Following is an example of how to use the xml-stylesheet processing instruction with the type and href attributes:

<?xml-stylesheet type="text/css" href="talltales.css"?>

In this example, the type attribute is used to specify that the type of the style sheet is text/css, which means that the style sheet is a CSS style sheet. The style sheet file is then referenced in the href attribute, which in this case points to the file talltales.css.

By the Way

It is necessary to specify the type of a style sheet in the xml-stylesheet processing instruction because there are other types of style sheets, such as XSLT and XSL-FO style sheets, which you learn about in upcoming lessons.


External style sheets represent the only way to use CSS directly with XML documents without the assistance of any other technology. I say this because it is possible to incorporate style sheets into the formatting of XML documents a little differently when you are also using XSLT. More specifically, with XSLT you are actually translating an XML document into an HTML document for display purposes. Knowing this, it is possible to use inline styles directly with HTML elements in order to apply styles to XML content indirectly. You can also use external style sheets with HTML documents that are translated from XML. You learn how to carry out both of these style approaches in Tutorial 11, "Getting Started with XSL."