XML Processing Instruction Syntax and Usage
An XML processing instruction (PI) allows document authors to embed application-specific instructions directly within an XML document. This article explains the standard syntax for defining an XML processing instruction, details the rules governing its target and data components, and provides practical examples of proper usage in well-formed XML.
Standard Syntax Structure
An XML processing instruction begins with the delimiter
<? and ends with the delimiter ?>. The
basic structure follows this format:
<?target data?>The component directly following the opening delimiter is the target, followed by at least one whitespace character, and then the optional data (or instruction content).
Syntax Rules for the Target
The target, formally known as the PITarget, identifies
the application or process for which the instruction is intended. It
must adhere to the following rules:
- Naming Convention: The target must be a valid XML Name (it must start with a letter or underscore and can contain letters, digits, hyphens, underscores, and periods).
- Reserved Names: The target cannot be the string
xmlin any combination of uppercase or lowercase (such asXML,Xml, orxML), as this is reserved for XML declarations and standard XML specifications. - Whitespace: No whitespace is allowed between the
opening
<?delimiter and the target name.
Syntax Rules for the Data
The data component contains the parameters or code sent to the processing application:
- Delimiting: The data must be separated from the target by at least one whitespace character (space, tab, or newline).
- Content: The data can contain any character except
the closing delimiter sequence
?>. - Optional Character: Processing instructions do not
require data; an instruction consisting solely of
<?target?>is syntactically valid. - Pseudo-Attributes: While data often resembles XML
attributes (e.g.,
name="value"pairs), the XML parser treats this entire block as plain text. Parsing pseudo-attributes into key-value pairs is the responsibility of the target application.
Common Examples
XML Stylesheet Association
A standard use case for processing instructions is linking a stylesheet to an XML document:
<?xml-stylesheet type="text/css" href="styles.css"?>- Target:
xml-stylesheet - Data:
type="text/css" href="styles.css"
Custom Application Directives
Custom applications can read bespoke instructions to modify how a document is rendered, exported, or transformed:
<?render-engine page-break="true" orientation="landscape"?>- Target:
render-engine - Data:
page-break="true" orientation="landscape"
Instruction Without Data
<?clear-cache?>- Target:
clear-cache - Data: None