|Chapter 13 - Gimme Some Space and Other Output Issues|
|XSLT For Dummies|
|by Richard Wagner|
|Hungry Minds 2002|
XML processing instructions provide a way for documents to contain instructions for applications that deal with the output document. Although you can use processing instructions for any custom purpose, probably the most common use for a processing instruction today is to attach an XSLT stylesheet with an XML document (see Chapter 10 for more information on this instruction):
<?xml-stylesheet href="defaultstyles.css" type="text/css"?>
Processing instructions stand apart from other XML elements due to their <? prefix and ?> suffix.
Warning Although it looks like one, XML declarations like <?xml version="1.0"?> are technically not processing instructions. Therefore, you cannot use xsl:processing-instruction to add an XML declaration to your output document. Instead, use the xsl:output instruction with method="xml" defined to have the processor automatically add an XML declaration to the top of your result document.
You can create processing instructions and add them to your result document with the handy xsl:processing-instruction element. It has two parts :
Suppose, for example, you have an application thats using a processing instruction called xsl-my_custom_instruction . To generate the following instruction in the output file:
<? my_custom_instruction lang-"en" customid="kimmers" type="absolute" ?>
You use the following XSLT:
<xsl:processing-instruction name="xsl-my_custom_instruction"> lang="en" customid="kimmers" type="absolute" </xsl:processing-instruction>
Putting xsl:processing-instruction into action, I use the instruction to add an xml-stylesheet reference to the film list stylesheet I use earlier in this chapter:
<xsl:template match="/"> <html><xsl:text> </xsl:text> <xsl:processing-instruction name="xml- stylesheet">href="liststyle.css" type="text/css"</xsl:processing-instruction> <xsl:comment>List created by <xsl:value-of select="@createdby"/></xsl:comment> <xsl:comment>***** Start List *****</xsl:comment> <xsl:apply-templates/> <xsl:comment>***** End List *****</xsl:comment> </html> </xsl:template>
Warning You cant include ?> as part of your processing instruction definition or you generate a processing error. The symbol ?> is reserved for the ending of a processing instruction.
Tip By default, all processing instructions contained in the source document are removed during transformation. However, to override this behavior and copy processing instructions from the source directly to the result document, use the following template rule:
<xsl:template match="processing-instruction()"> <xsl:copy/> </xsl:template>