XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / processing-instruction

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 6.)

A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z

 

The instruction xsl:processing-instruction creates a processing instruction (processing instruction).

Classification

Instruction

Function group

Creating result document output

Introduction

XSLT 1.0

Position in the stylesheet and permitted contents:

xsl:processing-instruction may occur anywhere inside template rules. Its template body is a sequence constructor producing the literal text forming the processing instruction’s content.

Attributes:

Standard attributes apply. Instruction xsl:processing-instruction also has a required attribute name. XSLT 2.0 adds an optional attribute select.

name

Value

{ncname} (also as an AVT)

Usage

required

Introduction

XSLT 1.0

The name attribute gives the processing instruction’s name, identifying its target application. It is also the node name returned by fn:name().

It must be an NCName (non-colonized name) without a colon, so it cannot be a namespace-prefixed QName.

An attribute value template can compute the value at runtime. It must identify a valid target application (PITarget). An invalid target or lexical name is an error (ERR XTDE0890).

select

Value

xpath-expression

Usage

optional

Introduction

XSLT 2.0

The select attribute is an alternative to a sequence constructor inside xsl:processing-instruction. Its XPath expression produces the processing instruction’s content. With the select attribute present, the element must be empty; otherwise a static error occurs (ERR XTSE0880).

Note: The select attribute does not exist in XSLT 1.0.

Purpose:

The instruction xsl:processing-instruction creates a result processing instruction sending arbitrary commands to a named application that will further process the document.

The command

<xsl:processing-instruction name="pi-name">anweisungen</xsl:processing-instruction>

therefore creates, for output method method="xml", a processing instruction of this form:

<?pi-name anweisungen?>

This indirect route is needed because a literal processing instruction in the stylesheet would be ignored. It cannot simply be copied from the stylesheet into the result. A source-document PI can, however, be copied using xsl:copy.

In HTML, processing instructions end not with ?> but only with >, according to the specification. Output method method="html" correctly produces:

<?pi-name anweisungen>

Generating the processing instruction body:
The instruction’s content forms the PI body, copied as literal text, like instruction xsl:text with disable-output-escaping="yes".

Special characters such as angle brackets require entities in the stylesheet. Processing replaces them with the literal characters, which appear unchanged in the result. Without disabled output escaping, that would not happen:

<xsl:processing-instruction name="pi-name">if $a &lt; $b tu etwas </xsl:processing-instruction>

therefore produces:

<?pi-name if $a < $b tu etwas ?>

The sequence ?&gt; must not occur inside the body: serialized as ?>, it would prematurely close the PI. Processors therefore usually insert a space between question mark and bracket, turning ?> into ? >.

A processor may instead report an error, so avoid the problem from the outset.

No attributes:
Although the result may resemble an element with attributes, it is not one, for example:

<?xml-stylesheet href="mein_style.css" type="text/css" ?>

These are only pseudo-attributes appearing as part of the instruction string.

Supply them as literal text; they cannot be created with xsl:attribute. A PI is not an element and cannot have attributes. No element node is open when it is created anyway.

Unsuitable for creating XML declarations:
The instruction xsl:processing-instruction neither intends nor permits creating an XML declaration, despite identical syntax. Merely supplying target value name="xml" causes an error (Note: this string is reserved and is not a valid PITarget). XML declarations are controlled through xsl:output.

Attention – An XML declaration is not a processing instruction:
By definition, an XML declaration is not a processing instruction and has a special role. It is not an element either and contains only pseudo-attributes inaccessible to the XML parser.

Examples:

Example 1 – Creating a stylesheet PI for CSS:

Stylesheet:

...
<xsl:processing-instruction name="xml-stylesheet">href="mein_style.css" type="text/css" title="Defaultstyle"</xsl:processing-instruction>
...
  

Result:

<?xml-stylesheet href="mein_style.css" type="text/css" title="Defaultstyle"?>

This inserts a processing instruction into an XML result, linking it to a CSS stylesheet.

Example 2 – Creating a stylesheet PI with select:

Instead of a sequence constructor, use the select attribute. Here a literal sequence of three strings is combined into the PI content:

Stylesheet:

...
<xsl:processing-instruction name="xml-stylesheet" select="('href=&quot;mein_style.css&quot;', 'type=&quot;text/css&quot;', 'title=&quot;Defaultstyle&quot;)"/>
...

Result:

<?xml-stylesheet href="mein_style.css" type="text/css" title="Defaultstyle"?>
  

The result matches example 1.

Example 3 – Creating an FO PI for Cocoon:

Stylesheet:

...
<xsl:processing-instruction name="cocoon-format">type="text/xslfo"</xsl:processing-instruction>
...

Result:

<?cocoon-format type="text/xslfo"?>
  

The format PI tells Cocoon to pass the processed document to its integrated FO processor after internal XSLT processing. It must not already appear in the stylesheet at the XSLT stage, since it could not then reach the next FO stage. Create it at that point instead.

Example 4 – Creating PHP with method="html":

Stylesheet:

...
<xsl:output method="html" indent="yes" encoding="UTF-8"/>
<xsl:template match="/">
  ...
  <xsl:processing-instruction name="php">$gruss="Hallo Welt!"; $echo gruss ?</xsl:processing-instruction>
  ...
</xsl:template>
  

Result:

<?php $gruss="Hallo Welt!"; echo $gruss ?>
  

With xsl:processing-instruction , PHP can be inserted into an HTML result. Note:

HTML processing-instruction syntax is not

<?pi-name pi-anweisungen ?>

but

<?pi-name pi-anweisungen >

, matching processing instructions in SGML. Consequently, the XSLT processor correctly omits the question mark before the closing bracket for output method method="html". With method="xhtml" this is not a problem.

This does not produce the desired PHP syntax. A workaround adds the missing question mark at the end of the PHP body.

If you can switch from method="html" to method="xhtml", possible only in XSLT 2.0, this avoids the difficulty and is the most elegant solution.

A less clean alternative replaces xsl:processing-instruction with xsl:text with disable-output-esca­ping="yes"

<xsl:text disable-output-escaping="yes">&lt;?php $gruss="Hallo Welt!"; echo $gruss ?&gt;</xsl:text>
  

. The result is essentially identical.

Element definition:

XSLT 1.0

<!-- Category: instruction -->
<xsl:processing-instruction 
     name = { ncname }>

     <!-- Content: template -->
</xsl:processing-instruction>

XSLT 2.0:

<!-- Category: instruction -->
<xsl:processing-instruction
     name = { ncname }>
     select? = expression

     <!-- Content: sequence-constructor -->
</xsl:processing-instruction>

Note: XSLT 2.0 adds optional attribute select.

Constructors for the other node kinds are xsl:attribute, xsl:comment, xsl:element, xsl:text and, since XSLT 2.0, xsl:namespace.

   

  

back to top