XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (226 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
5.26Mb size Format: txt, pdf, ePub

Note that you cannot generate a processing instruction in the output by writing a processing instruction in the stylesheet. Processing instructions in the stylesheet are ignored completely. You can, however, use

or

to copy processing instructions from the source tree to the result tree.

Examples

The following example outputs an

processing instruction at the start of the output file:


   href=“housestyle.css” type=“text/css”


The generated output is:


Writing an XSLT stylesheet that produces an XML document that itself refers to a CSS stylesheet isn't such a crazy thing to do as it might seem. It often makes sense to do the first stage of processing of an XML file on the server, and the second stage in the browser. The first stage will extract the data that users want to see—and remove any information they are not allowed to see. The second stage applies the detailed rules for output formatting. The second stage can often be done just as easily with CSS as with XSLT, because anything CSS can't cope with, such as adding or reordering textual content, can be done in the first stage with XSLT.

One point to watch out for in generating an

processing instruction, and which might well apply to other processing instructions, is the use of pseudo-attributes and pseudo-character and -entity references. The text
href=“housestyle.css”
in the above example is designed to look like an XML attribute, but it is not actually an XML attribute; it is purely part of the processing instruction data. It is parsed by the application, not by the XML parser. As it is not a true XML attribute, you cannot generate it as an attribute node using the

instruction; rather, it is generated as text.

The rules for the

processing instruction are defined in a short W3 C Recommendation called
Associating Style Sheets with XML Documents
, available at
http://www.w3.org/TR/xml-stylesheet
. In addition to defining the data part of this processing instruction in the form of pseudo-attributes, the rules also allow the use of numeric character references such as

and predefined entity references such as
>
and
&
. Again, these are not true character references and entity references that the XML parser will recognize, and as a result they will not be generated by the XSLT processor either. If you want to include

as part of the data of the processing instruction, you can write, for example:


  href=“housestyle.css” type=“text/css” 

  title=“A title containing ₤” 


Another way of generating this processing instruction, which might be more suitable if the contents are highly variable, is to write a general template that takes the required information as a parameter. This parameter might be supplied in the form of an element:


  housestyle.css

  text/css

  a title


and the processing instruction might be generated by the template:


  

  

      select=“for $att in $pi-data/* return

              concat(name($att), ‘="’, string($att), ‘"’)”/>


This does not attempt to deal with the problems that arise if there are special characters in the data that need to be escaped. Note that the space that is needed between pseudo-attributes is generated automatically, because each pseudo-attribute is produced as one item in the result of the
select
attribute.

Other books

Confessions of a Teenage Psychic by Pamela Woods-Jackson
The Wicked West by Victoria Dahl
Spellbound by Larry Correia
The Harrowing of Gwynedd by Katherine Kurtz