XSLT - Generating Processing Instructions with <xsl:processing-instruction>?
In XSLT, the <xsl:processing-instruction> instruction is used to create processing instruction nodes dynamically in the output document. Processing instructions are special XML nodes that provide information or instructions to applications processing the XML document.
A processing instruction is different from a normal XML element because it does not have a start tag and end tag. Instead, it has a target name followed by optional data.
1. What Is a Processing Instruction?
A processing instruction (PI) in XML has the following general form:
<?target data?>
For example:
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
Here:
-
xml-stylesheetis the processing instruction target. -
type="text/xsl" href="style.xsl"is the processing instruction data.
Processing instructions are commonly used to provide information to software applications without representing the information as regular XML data.
2. Why Use <xsl:processing-instruction>?
Normally, an XSLT stylesheet produces elements, attributes, text, comments, and other nodes. However, sometimes the transformation must generate a processing instruction whose target or content is determined dynamically.
For example, suppose the source XML contains:
<document>
<title>Student Information</title>
</document>
You may want the output to contain:
<?display-mode print?>
<document>
<title>Student Information</title>
</document>
XSLT provides <xsl:processing-instruction> specifically for creating such nodes.
3. Basic Syntax
The basic syntax is:
<xsl:processing-instruction name="target">
content
</xsl:processing-instruction>
For example:
<xsl:processing-instruction name="display-mode">
print
</xsl:processing-instruction>
The resulting output is:
<?display-mode print?>
The name attribute specifies the target of the processing instruction.
4. Using a Fixed Processing Instruction Name
Consider this XML document:
<book>
<title>XML Fundamentals</title>
</book>
An XSLT stylesheet can generate a processing instruction as follows:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<output>
<xsl:processing-instruction name="document-type">
book
</xsl:processing-instruction>
<xsl:value-of select="book/title"/>
</output>
</xsl:template>
</xsl:stylesheet>
The output will be similar to:
<output><?document-type book?>XML Fundamentals</output>
The processing instruction is inserted inside the output element.
5. Dynamic Processing Instruction Names
One important feature of <xsl:processing-instruction> is that the name can be calculated dynamically.
Suppose the source XML contains:
<document>
<instruction>
<name>display</name>
<value>print</value>
</instruction>
</document>
The stylesheet can use:
<xsl:processing-instruction name="{document/instruction/name}">
<xsl:value-of select="document/instruction/value"/>
</xsl:processing-instruction>
The output becomes:
<?display print?>
The expression inside {} is an attribute value template. It tells XSLT to evaluate the expression and use its result as the processing instruction's target.
6. Understanding the name Attribute
The name attribute is required because XSLT needs to know the target of the processing instruction.
For example:
<xsl:processing-instruction name="application">
report
</xsl:processing-instruction>
produces:
<?application report?>
The target is:
application
and the data is:
report
7. Generating Processing Instruction Content Dynamically
The content does not have to be fixed text. XSLT instructions can be used inside <xsl:processing-instruction>.
For example:
<xsl:processing-instruction name="page">
<xsl:value-of select="book/page-number"/>
</xsl:processing-instruction>
If the source XML contains:
<book>
<page-number>25</page-number>
</book>
the result will be:
<?page 25?>
This allows the processing instruction's data to depend on the source document.
8. Using Multiple Values
Suppose the source XML is:
<document>
<type>report</type>
<language>English</language>
</document>
The stylesheet can generate:
<xsl:processing-instruction name="metadata">
<xsl:text>type=</xsl:text>
<xsl:value-of select="document/type"/>
<xsl:text> language=</xsl:text>
<xsl:value-of select="document/language"/>
</xsl:processing-instruction>
The resulting processing instruction is:
<?metadata type=report language=English?>
This demonstrates how multiple XSLT instructions can be combined to construct the processing instruction's content.
9. Processing Instructions Versus Elements
A processing instruction:
<?status active?>
is not the same as an XML element:
<status>active</status>
An element represents structured document data and can contain attributes and child nodes.
A processing instruction is intended to communicate information to an application or processing system.
For example:
<document>
<title>Annual Report</title>
</document>
represents document information.
Whereas:
<?processor format="pdf"?>
can provide information to a processing application about how the document should be handled.
10. Generating the XML Stylesheet Processing Instruction
One well-known processing instruction is:
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
It associates an XML document with an XSLT stylesheet.
An XSLT transformation can generate such an instruction when the output itself needs to reference another stylesheet.
For example:
<xsl:processing-instruction name="xml-stylesheet">
<xsl:text>type="text/xsl" href="style.xsl"</xsl:text>
</xsl:processing-instruction>
This produces:
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
This can be useful when an XSLT transformation is producing XML that will subsequently be displayed or processed using another stylesheet.
11. Important Restriction on the Processing Instruction Name
The value supplied to the name attribute must be a valid XML processing-instruction target.
For example:
<xsl:processing-instruction name="display">
print
</xsl:processing-instruction>
is valid.
The XML specification also reserves the target xml in any capitalization, so it should not be used as an ordinary processing-instruction target.
12. Whitespace Considerations
Whitespace inside the instruction can become part of the processing instruction's data.
For example:
<xsl:processing-instruction name="mode">
print
</xsl:processing-instruction>
may produce:
<?mode
print
?>
depending on stylesheet whitespace handling and serialization.
When precise output is important, <xsl:text> can be used to control the generated content:
<xsl:processing-instruction name="mode">
<xsl:text>print</xsl:text>
</xsl:processing-instruction>
This makes the intended content explicit.
13. Processing Instructions and XML Serialization
Processing instructions are nodes in the resulting XML tree. The XSLT processor creates the processing-instruction node, and the serializer converts it into XML syntax.
For example, the XSLT instruction:
<xsl:processing-instruction name="status">
active
</xsl:processing-instruction>
creates a processing-instruction node whose serialized representation is:
<?status active?>
This distinction is important because XSLT works primarily with a tree representation of the document rather than simply manipulating strings.
14. Example with a Complete XML Transformation
Source XML:
<product>
<name>Laptop</name>
<status>available</status>
</product>
XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<product-information>
<xsl:processing-instruction name="status">
<xsl:value-of select="product/status"/>
</xsl:processing-instruction>
<name>
<xsl:value-of select="product/name"/>
</name>
</product-information>
</xsl:template>
</xsl:stylesheet>
The resulting output is:
<product-information>
<?status available?>
<name>Laptop</name>
</product-information>
Here, the processing instruction was created from information contained in the source XML.
15. Important Characteristics
The main characteristics of <xsl:processing-instruction> are:
-
It creates a processing-instruction node.
-
The
nameattribute identifies the processing-instruction target. -
Its content becomes the processing-instruction data.
-
The content can be generated dynamically using XSLT instructions.
-
It can be used to create application-specific instructions.
-
It is useful when the output XML must contain processing instructions.
-
It is different from creating an ordinary XML element.
-
The generated instruction becomes part of the output tree.
16. When Should You Use It?
<xsl:processing-instruction> should be used when the output document specifically needs a processing instruction.
Typical situations include:
-
Generating XML documents containing application-specific instructions.
-
Creating stylesheet association instructions.
-
Passing processing information to XML-aware applications.
-
Generating document-processing metadata.
-
Creating instructions whose target or content depends on source XML data.
-
Producing XML for systems that recognize particular processing instructions.
17. Summary
<xsl:processing-instruction> is an XSLT instruction used to generate processing instructions in the output document. A processing instruction has the form:
<?target data?>
In XSLT, it is created using:
<xsl:processing-instruction name="target">
data
</xsl:processing-instruction>
For example:
<xsl:processing-instruction name="display">
print
</xsl:processing-instruction>
produces:
<?display print?>
The instruction becomes particularly useful when the target or data needs to be generated dynamically from the source XML. It provides a way for an XSLT transformation to include application-specific instructions in the resulting XML document without representing those instructions as ordinary XML elements.