Document syntax
Comment
A <!-- … --> block that carries no document content and is not part of any element's text.
Comments may appear anywhere outside markup — in the prolog, between elements, after the root — but never inside a tag. They are nodes in the data model: XPath selects them with comment(), and a canonicalizer keeps or drops them depending on the mode.
The one syntactic surprise is that -- cannot appear inside a comment. The rule is inherited from SGML, it catches people who draw separator lines out of hyphens, and it is fatal rather than a warning.
- Defined in
- XML 1.0 §2.5
- Category
- Document syntax
- Also written
- <!-- -->
Shown, not asserted
All 2 claims are executed on every test run, against the same engines the tools use. If the behaviour changes, the test fails rather than this page quietly becoming wrong.
A comment is not text: it does not appear in the element's string value.
document.xml <x>a<!-- hidden -->b</x>expression string(/x)What the engine returns
abBut it does split the text around it into two nodes.
document.xml <x>a<!-- hidden -->b</x>expression count(/x/text())What the engine returns
2
Where it bites
A definition on its own is trivia. These are the pages where this term decides whether a document works.
- Parse errorsDouble hyphen within commentThe message a -- inside a comment produces.
- Parse errorsComment not terminatedWhat an unclosed comment reports, usually many lines later.
- XPathstring()string() skips comments — which is how you can see they are not text.
Related terms
- Processing instructionA <?target data?> node carrying instructions for an application, passed through the parser without being interpreted as content.
- Text nodeA run of character data in the tree, as a node in its own right — the model a query language sees rather than the markup you wrote.
- Canonicalization (C14N)Rewriting a document into a single normal form — sorted attributes, expanded empty elements, resolved references, no declaration — so that equivalent documents become byte-identical.
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.