document.createProcessingInstruction() is an instance method that builds a ProcessingInstruction node (see MDN Document: createProcessingInstruction()). Learn target and data, the MDN xml-stylesheet example, validation exceptions, how to insert and serialize the node, and five try-it labs.
01
Kind
Instance method
02
Args
target, data
03
Returns
ProcessingInstruction
04
Looks like
<?target data?>
05
Best in
XML docs
06
Status
Baseline
Fundamentals
Introduction
In XML, a processing instruction (PI) is a special node that carries a message to an application. Serialized, it looks like <?xml-stylesheet href="mycss.css"?> — not an element, not a comment.
doc.createProcessingInstruction(target, data) creates that node in memory. MDN notes you usually insert it into an XML document (for example with insertBefore) so serializers and tools can see it.
💡
Create → insert → serialize
1) const pi = doc.createProcessingInstruction(target, data) 2) doc.insertBefore(pi, doc.firstChild) 3) Inspect with XMLSerializer (MDN pattern)
Everyday HTML pages rarely need PIs. This API shines when you build or edit XML documents (often from DOMParser).
Document.createProcessingInstruction() is Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Document.createProcessingInstruction()
Create ProcessingInstruction nodes for XML documents across all major browsers.
BaselineWidely available
Google ChromeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
Microsoft EdgeSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported (legacy)
Yes
createProcessingInstruction()Wide
Bottom line: Use createProcessingInstruction for XML PIs such as xml-stylesheet. Insert the node into the XML document, then serialize when you need markup.
Wrap Up
Conclusion
document.createProcessingInstruction(target, data) builds an XML processing instruction node. Validate the target name, keep ?> out of the data, insert the node into an XML document, and serialize when you need the <?target data?> form — just like MDN’s stylesheet example.
Prefer this API for XML tooling, not everyday HTML UI
❌ Don’t
Put ?> inside data (MDN)
Use illegal targets like names starting with a digit (MDN)
Expect the detached PI to appear in the document without inserting it
Confuse PIs with HTML comments or elements
Assume HTML authors commonly need processing instructions
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about createProcessingInstruction()
Build XML <?target data?> nodes in the DOM.
5
Core concepts
📝01
Returns
PI node
MDN
📄02
Parts
target + data
two args
⚠️03
Ban
?> in data
throws
➕04
Insert
into XML
MDN
🛡05
Status
Baseline
2015
❓ Frequently Asked Questions
It creates a new ProcessingInstruction node from a target string and a data string (MDN). You usually insert it into an XML document — for example with insertBefore.
No. MDN marks Document.createProcessingInstruction() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
A ProcessingInstruction node. Read pi.target and pi.data (or nodeValue / textContent for the data).
Serialized form is <?target data?>. MDN’s example builds <?xml-stylesheet href="mycss.css"?> before the document element.
MDN: InvalidCharacterError if target is not a valid XML name, or if data contains the closing sequence ?>.
Processing instructions are an XML concept. MDN shows creating them on an XML Document from DOMParser (application/xml). Everyday HTML UIs rarely need them.
Did you know?
The XML declaration <?xml version="1.0"?> looks like a processing instruction, but it is special syntax handled by parsers — you do not create it with createProcessingInstruction("xml", ...) in the same way you create an xml-stylesheet PI for application use.