The Processing Instruction


The <?xml-stylesheet?> Processing Instruction

This processing instruction is not a part of the XSLT or XPath standard; rather it has a short W3C Recommendation all to itself, which you can find at http://www.w3.org/TR/xml-stylesheet . XSLT mentions it, but only in an example, so there is no implication that an XSLT processor is required to support it. However, most processors do.

The <?xml-stylesheet?> processing instruction is used within a source XML document to identify the stylesheet that should be used to process it. There can be several <?xml-stylesheet?> processing instructions present, defining different stylesheets to be used under different circumstances.

This way of controlling a transformation is particularly useful if you want to run the transformation on the client side (that is, in the browser). This is supported by both Internet Explorer and Netscape. It means you can simply send an XML file to the browser, with a processing instruction to identify the stylesheet to be used, and the browser will automatically invoke a transformation to HTML, which is then displayed. No special script is needed to control the process, which means the solution is very portable. Unfortunately, at the time of writing, none of the major browsers includes support for client-side transformation using XSLT 2.0. In fact it is likely to be a long time before support for XSLT 2.0 is sufficiently widespread in browsers for this to be a realistic option.

On the server side, which is where you are more likely to be using XSLT 2.0, it's less likely that you will want to control the transformation, using this processing instruction. It's much more likely that you will either want to drive the process from the operating system command line, or to use an API such as the Microsoft or Java APIs (described in Appendices C and D, respectively). These APIs give you much more control: they allow you to apply different stylesheets to the same documents on different occasions, to set parameters, and to compile a stylesheet once and then use it repeatedly.

So it's quite likely you can skip this section for now; but for completeness, I think it's still important to describe this mechanism.

The <?xml-stylesheet?> processing instruction has an href attribute whose value is the URI of the stylesheet (that is, the principal stylesheet module), and a type attribute that indicates the language in which the stylesheet is written. This doesn't have to be an XSLT stylesheet; it could be a Cascading Style Sheet (CSS).

There's considerable confusion about what the correct value of this attribute should be in the case of XSLT.

Until recently there was no registered media type (often called MIME type) for XSLT stylesheets, so Microsoft invented one: text /xsl. This has never been made official, and the XSLT 2.0 working draft proposes the registration of the name application/xslt+xml. However, the use of text/xsl is now so widespread that it is unlikely to go away.

Technically, XML processing instructions do not contain attributes; they contain a name ( here xml-stylesheet) followed by character data. However, many people like to structure the character data as a sequence of name="value" pairs, like the attributes in an element start tag, and the xml-stylesheet recommendation follows this practice. It refers to the name="value" pairs as pseudo-attributes.

Following is the full list of pseudo-attributes in the <?xml-stylesheet?> processing instruction.

Attribute Name

Value

Meaning

href (mandatory)

URI

The URI of the stylesheet. This may be an absolute or relative URL of the XML document, containing the stylesheet, or it may contain a fragment identifier (for example #styleB) used to locate the stylesheet within a larger file. See the section Embedded Stylesheets on page 95.

type (mandatory)

MIME type

Identifies the language in which the stylesheet is written; typically «application/xslt+xml » or «text/xsl » (see discussionearlier).

title (optional)

String

If there are several <?xml-stylesheet?> processing instructions, each should be given a title to distinguish them. The user can then be allowed to choose which stylesheet is wanted. For example, there may be special stylesheets that produce large print or aural rendition .

media (optional)

String

Description of the output medium, for example «print », «projection », or «aural ». The list of possible values is defined in the HTML 4.0 specification. This value can be used to select from the available stylesheets.

charset optional

Character encoding name, for example iso-8859-1

This attribute is not useful with XSLT stylesheets, since as XML documents they define their character encoding themselves .

alternate (optional)

"yes" or "no"

If «no » is specified, this is the preferred stylesheet. If «yes » is specified, it is an alternative stylesheet.

As far as I have been able to discover, however, the only two attributes that influence Netscape or Internet Explorer are the media and href attributes.

An <?xml-stylesheet?> 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 URL. For example:

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

According to the W3C spec, it is possible to have several <?xml-stylesheet?> 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 Netscape uses the last. (Since 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 <?xml-stylesheet?> 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 <?xml-stylesheet?> 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.




XSLT 2.0 Programmer's Reference
NetBeansв„ў IDE Field Guide: Developing Desktop, Web, Enterprise, and Mobile Applications (2nd Edition)
ISBN: 764569090
EAN: 2147483647
Year: 2003
Pages: 324

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net