Item 21. Rely on Namespace URIs, Not Prefixes

Namespaces use URIs to distinguish elements and attributes. The prefix is syntax sugar, nothing more—it's the URI that counts, not the prefix. All code you write should depend on the URI, not the prefix. For example, suppose you're writing a Java program that uses the DOM API to search for XLink elements. The following code fragment is correct because it relies on the URI and the local name.

public boolean isSimpleLink(Element candidate) {
  if (candidate.hasAttributeNS("http://www.w3.org/1999/xlink",
      "type"); {
    return true;
  }
  return false;
}

However, the following code is incorrect because it assumes the prefix is xlink and does not consider the URI.

 public boolean isSimpleLink(Element candidate) ...

Get Effective XML: 50 Specific Ways to Improve Your XML now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.