XML - XML Processing Instructions
Introduction
Processing Instructions (PIs) in XML are special instructions placed inside an XML document to provide information to applications that process the document. Unlike XML elements, processing instructions do not describe the actual data or structure of the document. Instead, they communicate application-specific information to software such as XML processors, parsers, rendering applications, or transformation tools.
A processing instruction begins with <? and ends with ?>. Its general syntax is:
<?target instruction-data?>
Here, target identifies the application or processing system that should interpret the instruction, while instruction-data contains information intended for that application.
For example:
<?xml-stylesheet type="text/xsl" href="book.xsl"?>
This processing instruction tells an XML-aware application that the XML document should be associated with the book.xsl stylesheet.
Structure of a Processing Instruction
An XML processing instruction has two main parts:
-
Target
-
Instruction data
Consider:
<?display format="compact"?>
Here, display is the target and format="compact" is the instruction data.
The target name identifies the application or processing mechanism that should interpret the instruction. XML itself does not define the meaning of arbitrary processing instructions. The application receiving the instruction determines what the instruction means.
For example:
<?printer orientation="landscape"?>
A specialized printing application could interpret this instruction as a request to use landscape orientation. Another application might simply ignore it.
XML Declaration and Processing Instructions
The XML declaration is often confused with a processing instruction because both use similar syntax. However, the XML declaration is not a processing instruction.
An XML declaration looks like:
<?xml version="1.0" encoding="UTF-8"?>
It provides information about the XML document itself, such as its XML version and character encoding.
A processing instruction, on the other hand, can provide application-specific information:
<?xml-stylesheet type="text/xsl" href="catalog.xsl"?>
The distinction is important because the XML declaration has specific rules and must appear at the beginning of the document when present, whereas processing instructions can occur at permitted locations within the XML document.
Example of a Processing Instruction
Consider the following XML document:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="catalog.xsl"?>
<catalog>
<book>
<title>XML Fundamentals</title>
<author>John Smith</author>
</book>
</catalog>
The processing instruction is:
<?xml-stylesheet type="text/xsl" href="catalog.xsl"?>
It associates the XML document with an XSLT stylesheet named catalog.xsl.
A browser or another XML-aware application may use this information when presenting or transforming the document.
The xml-stylesheet Processing Instruction
One of the most common practical processing instructions is xml-stylesheet.
For example:
<?xml-stylesheet type="text/xsl" href="catalog.xsl"?>
The type attribute-like value specifies the stylesheet type, while href specifies the stylesheet location.
A more complete example is:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="books.xsl"?>
<books>
<book>
<title>Learning XML</title>
<price>500</price>
</book>
</books>
The XML data remains separate from its presentation rules. The stylesheet can determine how the XML information should be displayed or transformed.
Processing Instructions Inside XML Documents
Processing instructions can appear in several locations where XML permits them. For example, they can appear before the document element:
<?xml version="1.0"?>
<?application mode="preview"?>
<document>
<title>Sample Document</title>
</document>
They can also occur within element content:
<document>
<?application action="highlight"?>
<title>Important Information</title>
</document>
Whether an application actually uses such an instruction depends entirely on that application's implementation.
Processing instructions should therefore not be treated as a replacement for normal XML elements when the information represents actual document data.
Processing Instructions vs XML Elements
Processing instructions and XML elements serve different purposes.
An element represents structured information:
<customer>
<name>John</name>
</customer>
Here, customer and name are part of the document's data structure.
A processing instruction provides information to a processing application:
<?application mode="print"?>
The mode="print" information is not part of the normal XML data model represented by application elements. Instead, it is an instruction intended for software processing the document.
For data that needs to be understood consistently by different XML applications, elements are generally preferable.
Processing Instructions vs Comments
Processing instructions are also different from XML comments.
A comment:
<!-- This document contains customer information -->
is intended primarily for human readers or documentation purposes. XML processors generally do not interpret the content of a comment as an instruction.
A processing instruction:
<?application mode="print"?>
is specifically intended to communicate information to an application.
Therefore:
<!-- Use landscape printing -->
is a comment, whereas:
<?printer orientation="landscape"?>
could be interpreted as an instruction by a printer-related application.
Rules for Processing Instructions
There are several important rules associated with XML processing instructions.
First, the target must follow XML naming rules and cannot be the reserved target xml in any case combination.
For example:
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
is valid because the target is xml-stylesheet, not xml.
The following would not be appropriate as a processing instruction target:
<?xml something?>
The target xml is reserved for XML's own use.
Second, the instruction must end with:
?>
For example:
<?application mode="test"?>
Third, the instruction data must not contain the sequence:
?>
because that sequence terminates the processing instruction.
How XML Parsers Handle Processing Instructions
An XML parser recognizes processing instructions as a specific type of XML node.
For example:
<root>
<?application mode="test"?>
<item>Data</item>
</root>
A parser can report the processing instruction to the application using its target and data.
A DOM-based parser may represent the processing instruction as a processing-instruction node. An application can then inspect its target and content.
For example, conceptually:
Node Type: Processing Instruction
Target: application
Data: mode="test"
The parser is responsible for recognizing the syntax, but it generally does not determine what the instruction actually means. The consuming application decides how to interpret it.
Processing Instructions in DOM
The Document Object Model represents processing instructions as nodes.
For example:
<?application mode="test"?>
<root>
<item>Example</item>
</root>
A DOM implementation can expose the processing instruction through properties such as its target and data.
In programming environments that support DOM, developers can inspect these values and perform application-specific actions.
Conceptually:
Target = application
Data = mode="test"
This makes processing instructions useful when an XML document needs to carry additional information for a particular application.
Advantages of Processing Instructions
Processing instructions provide several benefits.
Application-Specific Communication
They allow XML documents to communicate additional information to specialized applications without changing the main XML data structure.
Separation of Data and Processing Information
The actual data can remain represented through XML elements while application-specific instructions remain separate.
Extensibility
Applications can define their own processing instructions when standard XML elements are not appropriate for a particular processing requirement.
Support for Transformation and Presentation
The xml-stylesheet processing instruction is a practical example of connecting an XML document with a stylesheet.
Parser Recognition
Standard XML parsers understand the syntax of processing instructions and can expose them to applications.
Limitations of Processing Instructions
Processing instructions also have limitations.
The meaning of a processing instruction is generally application-specific. Two applications may interpret the same instruction differently.
They are therefore less suitable for representing business or domain data.
For example, this is better represented using an element:
<customerStatus>Active</customerStatus>
rather than:
<?customerStatus value="Active"?>
The element clearly forms part of the document's data model, whereas the processing instruction is primarily intended to influence processing.
Another limitation is portability. An application that does not understand a particular processing instruction may simply ignore it.
Practical Example
Suppose a company stores product information in XML:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="products.xsl"?>
<products>
<product>
<name>Laptop</name>
<price>65000</price>
</product>
<product>
<name>Monitor</name>
<price>18000</price>
</product>
</products>
The elements contain the actual product information.
The processing instruction:
<?xml-stylesheet type="text/xsl" href="products.xsl"?>
provides additional information about how the XML document can be transformed or presented.
This illustrates the fundamental purpose of processing instructions: they provide instructions to applications while leaving the main XML data structure intact.
Processing Instructions and XSLT
Processing instructions are particularly relevant when XML is used together with XSLT.
An XML document can contain:
<?xml-stylesheet type="text/xsl" href="products.xsl"?>
The instruction identifies a stylesheet associated with the document.
The XML contains the data:
<products>
<product>
<name>Laptop</name>
<price>65000</price>
</product>
</products>
The XSLT stylesheet contains transformation rules.
The processing instruction connects the XML document with the stylesheet so that a suitable XML-aware application can apply the transformation.
When to Use Processing Instructions
Processing instructions are appropriate when:
-
Information is intended specifically for a processing application.
-
The information is not part of the core document data.
-
An application needs additional processing instructions.
-
A standard mechanism such as
xml-stylesheetis appropriate. -
The instruction needs to be recognized by an XML parser and passed to an application.
They should generally not be used simply to store ordinary business data.
Summary
XML Processing Instructions are special XML constructs used to communicate application-specific information. They use the syntax <?target data?>, where the target identifies the processing mechanism and the remaining content contains the instruction data.
The most familiar example is:
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
Processing instructions differ from XML elements because elements represent structured document data, while processing instructions provide additional information to applications. They also differ from comments because comments are primarily descriptive, whereas processing instructions are intended to be acted upon by suitable software.
Understanding processing instructions is important for working with XML parsers, DOM, XSLT, document-processing systems, and other XML-based technologies.