Using stylesheets

Assuming the existence of a stylesheet that is applicable to a particular source XML document, a mechanism is needed to indicate that this stylesheet is to be used to process the document. This can be done in a number of different ways, each with its own strengths and weaknesses.

Embedded stylesheets

Stylesheets can be embedded within the documents to process. This can be done because both the XSLT stylesheet and the document containing it are XML structures.

A later section describes how this approach works in detail. The main problem with the approach is that the same embedded stylesheet may be replicated across a large number of similar documents, which is inefficient and makes maintenance very difficult.

Referenced stylesheets

It is more common practice for a document to include a reference to the stylesheet. This reference is detected by the XSLT processor. The stylesheet is accessed and interpreted, then applied to the content of the document. In this way, the same stylesheet can be easily applied to a number of XML documents.

This is done using an XML processing instruction, with the name 'xml-stylesheet', a Type parameter that indicates the stylesheet language in use ('text/xml'), and a parameter holding a URL pointer to the stylesheet called Href:

<?xml-stylesheet type="text/xml"
                 href="styles/format1.xsl"?>
<book>...</book>

The obvious remaining weakness of this (and the previous) approach is that it is necessary to edit the XML declaration when a different stylesheet is to be applied to the document.

Unlinked stylesheets

Another possible approach is to pass the document and the stylesheet to the processor separately. This approach avoids any issues with applying different stylesheets at different times:

Using a command-line interface, the following example would be typical. It also includes the name of the file that the processor should create for the output:

C:xsl> XSLProcessor MyStylesheet.XSL MyDoc.XML MyDoc.HTM

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset