XML Namespace Validation — Prefixes, URIs, and Scope

Validate XML namespace declarations, prefixes, and URI bindings. Learn how namespace scope works, common namespace errors, default namespaces, and how to fix undeclared prefix issues.

Validation

Detailed Explanation

XML Namespace Validation

XML namespaces prevent element and attribute name collisions when combining XML vocabularies from different sources. Namespace errors are among the most common issues in complex XML documents.

Namespace Declaration Syntax

<!-- Default namespace -->
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>My Feed</title>
</feed>

<!-- Prefixed namespace -->
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>...</soap:Body>
</soap:Envelope>

Common Namespace Errors

Undeclared prefix:

<!-- ERROR: "ws" prefix not declared -->
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ws:GetUser>42</ws:GetUser>
  </soap:Body>
</soap:Envelope>

The fix is to add xmlns:ws="http://example.com/webservice" to the Envelope or Body element.

Duplicate prefix declarations:

<!-- Confusing: same prefix, different URIs at different levels -->
<root xmlns:ns="http://example.com/v1">
  <child xmlns:ns="http://example.com/v2">
    <ns:element />  <!-- binds to v2 here -->
  </child>
</root>

While technically valid, re-binding a prefix to a different URI within a child scope is confusing and error-prone.

Namespace Scope

A namespace declaration applies to the element where it is declared and all its descendants, unless overridden by a new declaration with the same prefix.

Default Namespace vs. Prefixed

The default namespace (xmlns="...") applies to unprefixed elements but not to unprefixed attributes. This is a common source of confusion:

<root xmlns="http://example.com">
  <child attr="value" />
  <!-- "child" is in http://example.com namespace -->
  <!-- "attr" is in NO namespace -->
</root>

Validating Namespaces

A namespace-aware parser checks that every prefixed element and attribute has a corresponding xmlns declaration in scope. Our formatter highlights undeclared prefixes and helps you add the missing declarations.

Use Case

Namespace validation is essential when working with SOAP web services, XSLT transformations, multi-vocabulary XML documents (SVG embedded in XHTML), XML Schema validation, and any integration scenario where XML from multiple systems is combined into a single document.

Try It — XML Formatter

Open full tool