Skip to main content

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

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.

Malformed repeated attribute
<product id="42" id="43"/>
One authoritative value
<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.

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.