XML Comments and Processing Instructions
Besides elements, attributes and text, an XML document can contain two kinds of "side notes": comments, meant for humans, and processing instructions, meant for programs. Neither is part of the data itself.
Comments
Comments use the same syntax as in HTML:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Price list exported from the warehouse system -->
<prices currency="GBP">
<!-- Prices include VAT -->
<price sku="KB-100">49.99</price>
<price sku="MS-220">24.50</price>
<!--
Discontinued:
<price sku="MS-110">19.99</price>
-->
</prices>The playground's element tree skips the comments — they're not elements — and the "commented-out" <price> doesn't appear either.
Comment rules
- Start with
<!--and end with-->. - The two-hyphen sequence
--may not appear inside a comment, and a comment can't end with--->. - Comments can't appear inside a tag (
<price <!-- no --> sku="…">is illegal). - A comment can't come before the XML declaration.
- Comments do not nest.
<log>
<!-- version 2 -- updated by Ana -->
<entry>Started</entry>
</log>Replace -- with - or — and the document becomes well-formed.
Note: Parsers may discard comments entirely, so never put information a program needs in a comment. If it's data, make it an element or attribute.
Processing instructions
A processing instruction (PI) passes a hint to a particular application. It has the form <?target data?>:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="report.css"?>
<?print-options orientation="landscape" margins="narrow"?>
<report>
<title>Quarterly sales</title>
<?page-break?>
<section>Figures for April to June…</section>
</report>- The target (
xml-stylesheet,print-options,page-break) names the application the instruction is for. - The rest is free-form text. It often looks like attributes, but the parser doesn't treat it that way — that is up to the target application.
- Applications ignore PIs whose target they don't recognise.
- Targets beginning with
xml(in any case) are reserved for standards;xml-stylesheetis the best-known one.
The XML declaration is not a PI
<?xml version="1.0"?> looks like a processing instruction but is a special declaration with its own rules: it can only appear at the very start, and only version, encoding and standalone are allowed.
| Part | Example | Meaning |
|---|---|---|
version |
"1.0" |
XML version (almost always 1.0) |
encoding |
"UTF-8" |
Character encoding of the file |
standalone |
"yes" |
The document doesn't depend on an external DTD for its content |
Comments and PIs in the DOM
Both show up as nodes in the DOM tree, with their own node types:
<!DOCTYPE html>
<html>
<body>
<script>
const xml = `<?xml-stylesheet type="text/css" href="report.css"?>
<!-- generated nightly -->
<report><?page-break?><title>Sales</title></report>`;
const doc = new DOMParser().parseFromString(xml, 'application/xml');
for (const node of doc.childNodes) {
console.log('top level:', node.nodeType, node.nodeName, JSON.stringify(node.nodeValue));
}
const pi = doc.documentElement.firstChild;
console.log('inside report:', pi.nodeName, '(target:', pi.target + ', data:', JSON.stringify(pi.data) + ')');
// Adding a comment and a PI from code:
doc.documentElement.append(doc.createComment(' end of report '));
doc.insertBefore(doc.createProcessingInstruction('app-version', 'build="412"'), doc.firstChild);
console.log(new XMLSerializer().serializeToString(doc));
</script>
</body>
</html>| Node | nodeType |
nodeName |
Useful properties |
|---|---|---|---|
| Comment | 8 | #comment |
data / nodeValue |
| Processing instruction | 7 | the target | target, data |
Test yourself
Which comment is legal XML?
<!-- step -- two --><!-- step two --><!- step two -><!-- step two --->
What is the 'target' of <?print-options orientation="landscape"?>?
orientationlandscapeprint-optionsxml
Should important data be stored in comments?
- Yes, comments are always preserved
- No — parsers may discard comments; use elements or attributes
- Only inside CDATA
- Only in the prolog