XML Comments and Processing Instructions
Estimated study time: 9 minutes.
Beyond data, XML documents can carry two kinds of non-data content: comments for human readers, and processing instructions for applications that consume the file.
XML Comments
Comments let you annotate a document without affecting how it's parsed as data.
<!-- This section lists enrolled students -->
<students>
<student>Meera Iyer</student>
</students>
Comment Rules
- Comments cannot appear before the XML declaration.
- Comments cannot be nested inside other comments.
- The character sequence
--cannot appear inside a comment's text.
Processing Instructions (PIs)
A processing instruction passes application-specific information to whatever software is reading the XML — commonly used to link a stylesheet.
<?xml-stylesheet type="text/xsl" href="course-style.xsl"?>
<course>
<title>Data Analytics</title>
</course>
PIs vs Comments
- Comments are ignored by parsers — they're purely for humans reading the source.
- Processing instructions are passed through to the application, which decides how to act on them.
Processing Instruction Syntax in Detail
Every processing instruction has the same shape: <?target data?>. The target is a name that tells applications who the instruction is for. Everything after it, up to ?>, is free-form data that the receiving program interprets.
- The target must be a valid XML name and must come right after
<?with no space. - Targets starting with the letters "xml" in any case are reserved for XML standards, which is why
xml-stylesheetis allowed but you should not invent your own "xml..." names. - The data part often looks like attributes, as in
type="text/xsl", but these are not real attributes. The parser does not check them. - PIs may appear in the prolog, inside elements, or after the root element.
The XML declaration looks like a PI but is not one. It is a separate construct with strict rules, and only one is allowed, at the very start.
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="catalog.css"?>
<catalog>
<?page-break?>
<book id="b1">Learning XML</book>
</catalog>
Here, a browser applies the CSS file, while page-break is a custom instruction that only a specific printing tool would understand. Other programs simply skip it.
Reading Comments and PIs in Code
Many parsers drop comments and PIs by default, because most applications only care about data. If you need them, ask the parser to keep them. In Python's ElementTree:
import xml.etree.ElementTree as ET
builder = ET.TreeBuilder(insert_comments=True, insert_pis=True)
parser = ET.XMLParser(target=builder)
root = ET.fromstring(open("catalog.xml", encoding="utf-8").read(), parser=parser)
for node in root.iter():
if node.tag is ET.Comment:
print("Comment:", node.text)
elif node.tag is ET.ProcessingInstruction:
print("PI:", node.text)
In DOM-based libraries in Java or .NET, comments and PIs appear as their own node types, so you can filter for them while walking the tree.
Comments, PIs or Something Else?
| Need | Best choice |
|---|---|
| Explain the file to a human maintainer | Comment |
| Tell a specific program how to treat the file | Processing instruction |
Store text full of < and & as data | CDATA section, not a comment |
| Information every consumer must read | A normal element or attribute |
A useful test: if removing the content would change the meaning of the data, it should not be a comment or a PI.
Mistakes and Good Practice
- Commenting out a block that already contains a comment. Nesting is not allowed, so the inner
-->ends the outer comment early and breaks the file. - Using double hyphens as separators, such as a row of dashes inside a comment. Use another character instead.
- Leaving sensitive details in comments. Comments travel with the file, so never store passwords, keys or internal server names in them.
- Relying on comments being preserved. Transformations and many parsers drop them; keep important information in elements.
- Inventing PIs nobody reads. Add one only when a real tool in your pipeline acts on it.