Skip to main content

XML error · attribute

Unescaped '<' in an attribute value

A literal < inside an attribute value. XML forbids it there because the parser cannot tell it from the start of a tag, so it must be written &lt;.

Unescaped '<' not allowed in attributes values (line 2)

What it means

Inside an attribute value, < is the one character that cannot appear literally. The parser reads it as the beginning of a new tag and abandons the attribute it was in the middle of.

The ampersand has the same restriction, but its failure is reported as an entity error instead. Both come from the same place: attribute values are parsed content, not opaque strings.

What usually causes it

  • Building XML by string concatenation and escaping only element content, not attributes.
  • A comparison or arithmetic expression stored in an attribute: threshold="a < b".
  • A generator that HTML-escapes text nodes but passes attribute values through untouched.
  • User-supplied input reaching an attribute without escaping — the same gap that produces XML injection.

How to fix it

  • Write &lt; for a literal less-than sign inside an attribute value.
  • Escape & as &amp; in attributes too — it is invalid there for the same reason.
  • Stop concatenating strings and use a serializer, which escapes attribute values correctly by construction.
  • Move the value into element content wrapped in CDATA if it is long or expression-like.

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 value of attribute "title" must not contain the '<' character.
  • expat (Python)

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

    '<', hexadecimal value 0x3C, is an invalid attribute character.
  • fast-xml-parser and similar

    (accepted — no error reported at all)

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 title="a < b"/>
Parses
<?xml version="1.0"?>
<root title="a &lt; b"/>

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.