TExcelTAD
Home › XML › XML Comments & Processing Instructions

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 · try it yourself
<?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.
xml · try it yourself
<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 · try it yourself
<?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-stylesheet is 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:

html · try it yourself
<!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?

  1. <!-- step -- two -->
  2. <!-- step two -->
  3. <!- step two ->
  4. <!-- step two --->

What is the 'target' of <?print-options orientation="landscape"?>?

  1. orientation
  2. landscape
  3. print-options
  4. xml

Should important data be stored in comments?

  1. Yes, comments are always preserved
  2. No — parsers may discard comments; use elements or attributes
  3. Only inside CDATA
  4. Only in the prolog