XML Processing Instructions
XML files carry data. But sometimes the file also needs to send special instructions to the application reading it — instructions like "use this stylesheet to display me" or "this file was created by this software." Processing instructions handle exactly that job.
A processing instruction is a message inside an XML file aimed at the application, not at the data itself. The XML parser passes this message to the application without treating it as regular XML content.
The Everyday Analogy
Think of a letter inside an envelope. The letter is the data. The envelope has a note on the outside: "Handle with care — fragile contents." The post office reads that note and acts on it, but the note is not part of the letter's message. Processing instructions work the same way — they carry instructions for the handler, not content for the reader.
Processing Instruction Syntax
A processing instruction starts with <? and ends with ?>. Between these markers, the first word is the target — it identifies which application the instruction belongs to. Everything after the target is the instruction content.
<?target instruction-content?>
Structure Breakdown
<? = Start of processing instruction target = Name of the application this targets content = Data or settings for that application ?> = End of processing instruction
The Most Common Processing Instruction
The XML prolog itself is a processing instruction — the most common one in XML:
<?xml version="1.0" encoding="UTF-8"?>
The target is xml. The content tells parsers which XML version and character encoding to use. Every XML file that has a prolog uses this processing instruction.
Linking a Stylesheet with a Processing Instruction
You attach a CSS or XSLT stylesheet to an XML file using a processing instruction. Browsers read this instruction and apply the stylesheet when displaying the XML file.
Linking a CSS Stylesheet
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/css" href="menu.css"?> <restaurant> <name>The Spice Garden</name> <item>Butter Chicken</item> <item>Mango Lassi</item> </restaurant>
The browser reads the xml-stylesheet processing instruction and applies the menu.css file. Without this instruction, the browser displays raw XML without any styling.
Linking an XSLT Stylesheet
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="transform.xsl"?>
<catalog>
<product id="101">
<name>Blue Notebook</name>
<price>45</price>
</product>
</catalog>
The browser or XSLT processor reads this instruction and applies the transform.xsl file to convert the XML into styled HTML.
Processing Instruction Flow Diagram
XML File | |-- <?xml version="1.0"?> --> Goes to: XML Parser |-- <?xml-stylesheet ...?> --> Goes to: Browser / Renderer |-- <?php ... ?> --> Goes to: PHP Processor |-- <?myapp config="true"?> --> Goes to: Custom Application | +-- Regular XML Elements --> Go to: Data Consumer
Each processing instruction targets a different application. Regular XML elements go to whoever reads the data.
Custom Processing Instructions
Applications can define their own processing instructions. Software tools like Adobe InDesign, Microsoft Word, and database tools sometimes embed processing instructions in XML exports to carry metadata about how the file was created.
Example from a Publishing Tool
<?xml version="1.0" encoding="UTF-8"?> <?publishing-tool version="4.2" author="Priya Sharma"?> <?page-settings margin="2cm" orientation="portrait"?> <document> <chapter>Introduction to XML</chapter> </document>
The publishing-tool and page-settings processing instructions carry information for the publishing software. The XML parser ignores the content of these instructions and passes them to the application.
Rules for Processing Instructions
- Processing instructions start with <? and end with ?>.
- The target name (the first word) must follow XML naming rules — it cannot start with a number or contain spaces.
- The target name xml (in any uppercase or lowercase combination) is reserved for XML itself.
- Processing instructions can appear before the root element, after the root element, or inside elements — but not inside attribute values or tag definitions.
- Processing instructions are not elements, so they do not need closing tags.
Where Processing Instructions Can Appear
Before the root element
<?xml version="1.0"?> <?xml-stylesheet type="text/css" href="style.css"?> <root> <data>content</data> </root>
Inside an element
<root>
<section>
<?section-marker id="chapter-2"?>
<title>Chapter Two</title>
</section>
</root>
After the root element
<root> <data>content</data> </root> <?end-of-document checksum="abc123"?>
Processing Instructions vs Comments
Processing instructions look similar to XML comments, but they serve different purposes. Comments are notes for humans reading the XML code. Processing instructions are instructions for software applications processing the file.
| Feature | Processing Instruction | XML Comment |
|---|---|---|
| Syntax | <?target data?> | <!-- text --> |
| Purpose | Instruction for software | Note for humans |
| Application reads it | Yes | No |
| Contains a target | Yes | No |
Key Points to Remember
- Processing instructions carry messages for applications, not for data readers.
- They use the format <?target content?>.
- The XML prolog <?xml version="1.0"?> is the most common processing instruction.
- The xml-stylesheet processing instruction connects a stylesheet to an XML file.
- The target name cannot begin with xml — that prefix is reserved.
- Processing instructions appear in the DOM, so programs can read and use them.
