Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
As far as I have been able to discover the only attributes that influence Internet Explorer or Firefox are the
media
,
href
, and
alternate
attributes, and
alternate
is not very useful, because both browsers choose the
alternate = “no”
stylesheet without giving the user any opportunity to override the choice.
An
processing instruction must appear, if it appears at all, as part of the document prolog, that is, before the start tag of the document element. The
href
attribute identifies the location of the stylesheet by an absolute or relative URI reference. For example:
According to the W3 C spec, it is possible to have several
processing instructions that match the required criteria. The idea is that, as with CSS, the different stylesheets should be merged. Again, however, the practical reality seems to be different: it appears that Internet Explorer uses the first stylesheet specified, and Firefox uses the last. (Because this might be a bug that could be fixed at any time, I would advise against relying on this observation in the design of your application.)
It isn't mandatory to use the
processing instruction, and most products will offer some other way of saying which stylesheet you want to apply to a particular document. It's mainly useful when you want to apply a stylesheet to an XML document within the browser; specifying this processing instruction means that the browser can apply a default stylesheet to the document, without any extra scripting being needed.
Clearly, one of the reasons for separating the stylesheet from the source XML document is so that the same information can be transformed or presented in different ways depending on the user, their equipment, or the particular mode of access. The various attributes of the
processing instruction are designed to define the rules controlling the selection of an appropriate stylesheet. The mechanism is geared toward stylesheets that are used to display information to users; it has less relevance to the more general use of XSLT for performing data transformations.
Embedded Stylesheets
There is one exception to the rule that the stylesheet module must be an XML document. The principal stylesheet module can be
embedded
within another XML document, typically the document whose style it is defining.
The ability to embed stylesheets within the source document is best regarded as a carryover from CSS. It can be useful if you have a freestanding document that you want to distribute as a self-contained unit, but in most situations it is better to use an external stylesheet that can be used for many different source documents. I sometimes use an embedded stylesheet when I have a “one-of-a-kind” document such as a diary of events to be displayed on a Web site, as it simplifies things to keep the stylesheet and the data together. Some people like to embed stylesheets to reduce download time, but this can be counterproductive, because it means the browser cannot spot that the stylesheet is already present in its cache.
Not all products support embedded stylesheets. The example below works in Firefox but not in Internet Explorer.
The outermost element of the stylesheet is still an
id
attribute to identify it and will be referenced within its containing document using the
processing instruction, as shown in the following example.
Example: Embedded Stylesheets
This example shows a stylesheet embedded within an XML source document containing a list of books.
Source
The data file,
embedded.xml
, containing both source document and stylesheet, is as follows:
]>
xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”>
A list of books
You can run this stylesheet either by simply opening it in Firefox or by using Saxon with a command of the form:
java -jar c:\saxon\saxon9.jar -a embedded.xml
The
-a
option tells Saxon to look for an
processing instruction in the supplied source document, and to process the source document using that stylesheet. Saxon doesn't allow you (when using the command line interface) to specify the criteria for selecting a specific stylesheet, so if there are several, it uses a composite stylesheet that imports all of them.
Saxon will recognize the relative URI
#style1
only if it refers to the value of an attribute of type
ID
. The
id
attribute of the
entry. This isn't sufficient to invoke validation of the document (if you do try to invoke validation, by specifying the
-v
option on the command line, you will get a string of error messages referring to undeclared elements), but it is sufficient to register the attribute type of the
id
attribute.
This example doesn't work with Saxon on the .NET platform; this is because the Microsoft XML parser on .NET doesn't notify ID attributes unless you do DTD validation. An alternative is to use the
xml:id
attribute, which doesn't depend on a DTD. In the downloaded code, the file
embedded2.xml
illustrates this variation.
Output
The output of this embedded stylesheet, when viewed in a Web browser, is shown in
Figure 3-2
.