XML guide
How to fix a duplicate XML attribute
Find the repeated expanded attribute name, decide which value owns the field, and fix generators that merge attribute collections unsafely.
6 min read · Updated 2026-08-21
The short answer
An element cannot carry two attributes with the same expanded name. Remove the duplicate only after deciding which source owns the field, and repair the serializer or merge step that emitted both values; changing the prefix does not help when both prefixes resolve to the same namespace URI.Identify the duplicated expanded name
The obvious case repeats the same spelling, such as id twice on one start tag. Namespaced attributes can also collide when different prefixes are bound to the same URI and both use the same local name.
<product id="42" id="43"/><product id="42"/>Resolve ownership instead of keeping one arbitrarily
A last-value-wins repair can hide a contract conflict. Trace whether the values came from defaults, inherited metadata, application fields, or two maps combined immediately before serialization.
- Choose a single system of record for each attribute.
- Reject conflicting values during object construction.
- Log the field path and both sources before serialization.
- Keep schema defaults out of emitted XML unless a concrete value is required.
Make the serializer enforce uniqueness
Build attributes through a map keyed by expanded name rather than concatenating fragments. For namespaced attributes, key by namespace URI plus local name; a prefix is only the document's alias for that URI.
Add a regression case where two input layers supply the same attribute, plus one where different prefixes map to the same namespace.
Prove the fix
- Validate the generated XMLRun the complete serialized output rather than a manually corrected fragment.
- Read the parser errorSee duplicate spelling and namespace-equivalent collisions in context.
- Compare two outputsCheck which source layer introduced the second attribute or changed its value.
Related guides
- How to fix an invalid XML entity referenceFind the unescaped ampersand, distinguish named entities from character references, and repair the value without hiding malformed input.
- XML namespace prefix vs URIUnderstand why prefixes are replaceable aliases, why namespace URIs are compared literally, and how expanded names keep documents interoperable.
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.