Skip to main content

XML error · syntax

Double hyphen within comment

XML forbids -- inside a comment's body, even when the comment is otherwise closed properly. It is one of the specification's genuinely surprising rules.

Double hyphen within comment: <!-- a (line 2)

What it means

A comment runs from <!-- to the first -->. To keep that unambiguous, the specification bans the sequence -- anywhere inside, so a comment cannot contain a double hyphen even far from its end.

This catches people out because HTML tolerates it and because -- reads as a natural separator. libxml2 quotes the start of the offending comment back to you, which is how you find it in a long file.

What usually causes it

  • Using -- as a visual rule or separator inside a comment block.
  • Commenting out a region that already contained a comment, leaving its -- behind.
  • A commented-out command line or diff that legitimately contains --flag or ---.
  • Auto-generated comment banners drawn with hyphens.

How to fix it

  • Replace -- with a single hyphen, an em dash, or =.
  • Split one comment into two rather than nesting them; XML comments do not nest.
  • Move long commented-out material out of the document entirely — into source control, where it belongs.

The same error elsewhere

Different parsers, same defect. If you arrived with one of these messages, you are in the right place.

  • Xerces (Java)

    The string "--" is not permitted within comments.
  • expat (Python)

    not well-formed (invalid token): line 2, column 12
  • .NET System.Xml

    An XML comment cannot contain '--', and '-' cannot be the last character.

Before and after

Both snippets are re-checked by the test suite against the real parser: the first is confirmed to produce the exact error above, the second to parse cleanly.

Fails
<?xml version="1.0"?>
<root><!-- a -- b --></root>
Parses
<?xml version="1.0"?>
<root><!-- a - b --></root>

Get started

Bring order to the XML your team can't afford to ignore.

Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.