O'Reilly logo

Effective XML: 50 Specific Ways to Improve Your XML by Elliotte Rusty Harold

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required