Lably
Skip to content

Namespace

Container for a set of identifiers

  • 38 min read
  • 3 chapters

Related on Lably

Through Computing, Object (computer science) and Identifier

Namespaces are commonly structured as hierarchies to allow reuse of names in different contexts. As an analogy, consider a system of naming of people where each person has a given name, as well as a family name shared with their relatives. If the first names of family members are unique only within each family, then each person can be uniquely identified by the combination of first name and family name; there is only one Jane Doe, though there may be many Janes. Within the namespace of the Doe family, just "Jane" suffices to unambiguously designate this person, while within the "global" namespace of all people, the full name must be used.

Prominent examples for namespaces include file systems, which assign names to files. Some programming languages organize their variables and subroutines in namespaces. Computer networks and distributed systems assign names to resources, such as computers, printers, websites, and remote files. Operating systems can partition kernel resources by isolated namespaces to support OS-level virtualization containers.

Similarly, hierarchical file systems organize files in directories. Each directory is a separate namespace, so that the directories "letters" and "invoices" may both contain a file "to_jane".

In computer programming, namespaces are typically used to group symbols and identifiers around a given task and to avoid name collisions between multiple identifiers that share the same name.

In computer networking, the Domain Name System organizes websites (and other resources) into hierarchical namespaces.

Name conflicts

Element names are defined by the developer. This often results in a conflict when trying to mix XML documents from different XML applications.

This XML carries HTML table information:

<span id="mwQQ"></span><span class="nt" id="mwQg"><table></span> <span class="w" id="mwQw"> </span><span class="nt" id="mwRA"><tr></span> <span class="w" id="mwRQ"> </span><span class="nt" id="mwRg"><td></span>Good<span class="nt" id="mwRw"></td></span> <span class="w" id="mwSA"> </span><span class="nt" id="mwSQ"><td></span>Bad<span class="nt" id="mwSg"></td></span> <span class="w" id="mwSw"> </span><span class="nt" id="mwTA"></tr></span> <span class="nt" id="mwTQ"></table></span>

This XML carries information about a table (i.e. a piece of furniture):

<span id="mwUg"></span><span class="nt" id="mwUw"><table></span> <span class="w" id="mwVA"> </span><span class="nt" id="mwVQ"><name></span>Mahogany<span class="w" id="mwVg"> </span>Coffee<span class="w" id="mwVw"> </span>Table<span class="nt" id="mwWA"></name></span> <span class="w" id="mwWQ"> </span><span class="nt" id="mwWg"><width></span>30<span class="nt" id="mwWw"></width></span> <span class="w" id="mwXA"> </span><span class="nt" id="mwXQ"><length></span>120<span class="nt" id="mwXg"></length></span> <span class="nt" id="mwXw"></table></span>

If these XML fragments were added together, there would be a name conflict. Both contain a <table>...</table> element, but the elements have different content and meaning.

An XML parser will not know how to handle these differences.

Solution via prefix

Name conflicts in XML can easily be avoided using a name prefix.

The following XML distinguishes between information about the HTML table and furniture by prefixing "h" and "f" at the beginning of the elements.

<span id="mwaA"></span><span class="nt" id="mwaQ"><h:table></span> <span class="w" id="mwag"> </span><span class="nt" id="mwaw"><h:tr></span> <span class="w" id="mwbA"> </span><span class="nt" id="mwbQ"><h:td></span>Good<span class="nt" id="mwbg"></h:td></span> <span class="w" id="mwbw"> </span><span class="nt" id="mwcA"><h:td></span>Bad<span class="nt" id="mwcQ"></h:td></span> <span class="w" id="mwcg"> </span><span class="nt" id="mwcw"></h:tr></span> <span class="nt" id="mwdA"></h:table></span> <span class="nt" id="mwdQ"><f:table></span> <span class="w" id="mwdg"> </span><span class="nt" id="mwdw"><f:name></span>Mahogany<span class="w" id="mweA"> </span>Coffee<span class="w" id="mweQ"> </span>Table<span class="nt" id="mweg"></f:name></span> <span class="w" id="mwew"> </span><span class="nt" id="mwfA"><f:width></span>30<span class="nt" id="mwfQ"></f:width></span> <span class="w" id="mwfg"> </span><span class="nt" id="mwfw"><f:length></span>120<span class="nt" id="mwgA"></f:length></span> <span class="nt" id="mwgQ"></f:table></span>

Sign in to keep reading

2 more chapters are free with a Lably account.

More in Computing and communication