Wiring an XSL Style Sheet to an XML Document

In Tutorial 10, "Styling XML Content with CSS," you learned how to create and connect CSS style sheets to XML documents. These types of style sheets are known as external style sheets because they are stored in separate, external files. XSL style sheets are also typically stored in external files, in which case you must wire them to XML documents in order for them to be applied. XSL style sheets are typically stored in files with a .xsl filename extension and are wired to XML documents using the xml-stylesheet processing instruction. The xml-stylesheet processing instruction includes a couple of attributes that determine the type and location of the style sheet:

By the Way

You can also use the general file extension .xml or the more specific extension .xslt for your XSLT style sheets. The extension really doesn't matter so long as you reference the style sheet properly from the XML document to which it applies.

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

  • HRef The location of the style sheet

By the Way

You may notice that this discussion focuses on XSL style sheets in general, as opposed to XSLT style sheets. That's because XSLT style sheets are really just a specific kind of XSL style sheet, and from the perspective of an XML document there is no difference between the two. So, when it comes to associating an XSLT style sheet with an XML document, you simply reference it as an XSL style sheet.

These two attributes should be somewhat familiar to you from the discussion of CSS because they are also used to wire CSS to XML documents. The difference in their usage with XSL is revealed in their valuesthe type attribute must be set to text/xsl for XSL style sheets, whereas the HRef attribute must be set to the name of the XSL style sheet. These two attributes are both required in order to wire an XSL style sheet to an XML document. Following is an example of how to use the xml-stylesheet processing instruction with an XSL style sheet:

<?xml-stylesheet type="text/xsl" href="contacts.xsl"?>

In this example, the type attribute is used to specify that the type of the style sheet is text/xsl, which means that the style sheet is an XSL style sheet. The style sheet file is then referenced in the HRef attribute, which in this case points to the file contacts.xsl.