Skip to main content

Search 412 pages — tools, formats, elements, namespaces, directory, comparisons, XPath, datatypes, glossary, parse errors, schema errors, use cases, guides, blog and product.

XML error · syntax

Invalid element name

An element name breaks XML's naming rules — usually because it starts with a digit, contains a space, or uses punctuation XML reserves.

StartTag: invalid element name (line 2)

What it means

XML names must begin with a letter or underscore, then may contain letters, digits, hyphens, underscores and periods. A leading digit, a space, or most punctuation is invalid.

This is the classic failure of generating element names from data: column headers, JSON keys and year labels become names XML will not accept.

What usually causes it

  • Element names generated from data: <2026-total>, <1st-quarter>.
  • A space or slash in a generated name.
  • Names starting with the reserved sequence xml, in any case.
  • A character that is legal in a JSON key but not in an XML name.

How to fix it

  • Prefix generated names so they start with a letter or underscore: <y2026-total>.
  • Replace illegal characters when mapping data to names.
  • Better still, keep the element name fixed and put the variable part in an attribute: <total period="2026"/>.

The same error elsewhere

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

  • Xerces (Java)

    Element type "2026-total" must be followed by either attribute specifications, ">" or "/>".
  • expat (Python)

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

    Name cannot begin with the '2' 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
<report>
  <2026-total>1200</2026-total>
</report>
Parses
<report>
  <total period="2026">1200</total>
</report>

Where this bites most

The terms behind it

Each of these is defined in one sentence and then demonstrated against the parser.

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.