<xsl:processing-instruction> Element
Generates a processing instruction node in the output.
<xsl:processing-instruction name = "pi-name"> </xsl: processing-instruction>
Attributes
- name
- Required. The NCName of the processing instruction.
Element Information
Number of occurrences | Unlimited |
Parent elements | xsl:attribute, xsl:comment, xsl:copy, xsl:element, xsl:fallback, xsl:for-each, xsl:if, xsl:message, xsl:otherwise, xsl:param, xsl:processing-instruction, xsl:template, xsl:variable, xsl:when, xsl:with-param, output elements |
Child elements | xsl:apply-imports, xsl:apply-templates, xsl:call-template, xsl:choose, xsl:copy, xsl:copy-of, xsl:fallback, xsl:for-each, xsl:if, xsl:message, xsl:number, xsl:text, xsl:value-of, xsl:variable |
Remarks
The <xsl:processing-instruction>
element generates a processing instruction node in the output. The name is indicated by the name
attribute. The content of the element provides the rest of the processing instruction.
The XML declaration is not a processing instruction, and should be generated by setting attributes on an <xsl:output>
element.
Example
This example shows a template that generates the XML declaration and a style sheet processing instruction in the output.
XML File (customers.xml)
<?xml version='1.0'?> <?xml-stylesheet type="text/xsl" href="pi.xsl" ?> <customers> <customer> <name>James Smith</name> <address>123 Elm St.</address> <phone>(123) 456-7890</phone> </customer> <customer> <name>Amy Jones</name> <address>456 Oak Ave.</address> <phone>(156) 789-0123</phone> </customer> </customers>
XSLT File (pi.xsl)
<?xml version='1.0'?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:output method='xml' version='1.0'/> <xsl:template match="/"> <xsl:processing-instruction name="xml-stylesheet"> <xsl:text>type="text/xsl" href="style.xsl"</xsl:text> </xsl:processing-instruction> <xsl:apply-templates /> </xsl:template> <xsl:template match="@* | *"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template></xsl:stylesheet>
Try It!
- Copy the code above and save it in appropriate files on your local drive.
- Run the example, using the msxsl.exe utitility from the command prompt, as follows:
msxsl customers.xml pi.xsl -o new-cust.xml
Output
The result of this transformation is the same XML file with a new style sheet embeded in it. The output file, new-cust.xml, should look as follows:
<?xml version="1.0" encoding="UTF-16"?> <?xml-stylesheet type="text/xsl" href="style.xsl" ?> <customers> <customer> <name>James Smith</name> <address>123 Elm St.</address> <phone>(123) 456-7890</phone> </customer> <customer> <name>Amy Jones</name> <address>456 Oak Ave.</address> <phone>(156) 789-0123</phone> </customer> </customers>
See Also
Generating Comments, Processing Instructions, and Elements