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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.