Namespaces, why are they necessary for prefixes?

So I’ve been reading about namespace on w3schools and I understand they are to uniquely identify an element. But what is the point of having namespaces if you have to have prefixes along side them. Are you always required to have prefixes with namespaces? If not are you always required to have namespaces with prefixes? If so why?

For example:

<root xmlns:h="http://www.w3.org/TR/html4/" xmlns:f="http://www.w3schools.com/furniture">

 <h:table>
   <h:tr>
   <h:td>Apples</h:td>
   <h:td>Bananas</h:td>
 </h:tr>

<f:name>African Coffee Table</f:name>
    <f:width>80</f:width>
    <f:length>120</f:length>
  </f:table>
</root>

Doesn’t f and h uniquely identify the different element types?

Thanks for your help. I’m really at a loss at understanding why namespaces exists.

4

An XML namespace is supposed to have a reasonable chance of being universally unique. A prefix, especially a single-letter one, is almost certain not to be. A namespace declaration creates a mapping from an otherwise meaningless prefix to a stable identifier that software can rely on to identify the schemas of XML elements.

You’re right: within a single document, an author can make sure that different prefixes are used for different groups of elements. But without tying those prefixes to unique namespaces, how do you accomplish any of the following:

  • Sending that XML file to an organisation that uses different prefixes for the same groups of elements, and expecting it to be interpreted correctly by (hypothetical) software that only uses prefixes?
  • Any automated processing of XML documents at all? Even something as fundamental as schema validation: how does your HTML parser know to associate the prefix h with the collection of HTML elements, and therefore knows how to render h:table?

There is no central database of prefixes where h is forever and all time associated with HTML elements – that’s the purpose of the namespace declaration mapping the prefix to the namespace within the scope of the document.

Update – clarification of terminology

  • Namespace URI, also just called namespace: A stable name uniquely identifying a collection of XML elements and attributes.
    • This is just an identifier, chosen to be unique.
    • Although it is often a URL, whatever document might be available at this URL is irrelevant and is not consulted during XML processing. Browsing to the URL does not have to produce any valid content, although sometimes the company which produced the XML schema, and chose its namespace URI to have a domain which they control, chooses to publish some information about the schema at that address.
    • Note that this means you cannot expect to download a schema given a namespace URI. You will need to get hold of schemas using some other mechanism, probably documented by whoever produced the schema.
  • Prefix: a document-local representation of a namespace URI within an XML document. Any arbitrary prefix can be mapped to a namespace using xmlns. If you really wanted to, you could have both xmlns:f="http://www.w3schools.com/furniture" and xmlns:argos="http://www.w3schools.com/furniture", and then use the prefixes interchangeably: <f:table> or <argos:table>. The parser doesn’t care whether the prefix is f or argos – it only cares that both of these table elements belong to the namespace http://www.w3schools.com/furniture. Further, the URL http://www.w3schools.com/furniture does not have to be a valid document. When used as an XML namespace URI, its only function is to be a unique identifier. If the parser had available a schema whose targetNamespace was http://www.w3schools.com/furniture, it could validate the table elements and their contents using that schema.

4

The point of namespaces in XML is not just to make it clear for the human reader that <h:table> and <f:table> are different things, but also to tell a (validating) XML parser how they should be parsed (for example, which attributes and sub-elements are valid).

The prefixes are only needed if there can be confusion about which namespace the element comes from. For example, this is also valid, because all elements come from the same namespace:

<root xmlns="http://www.w3schools.com/furniture">
  <table>
    <name>African Coffee Table</name>
    <width>80</width>
    <length>120</length>
  </table>
</root>

The prefixes are mostly a shorthand to indicate a particular namespace, but they don’t have any meaning of their own. It is the namespace URI that tells the XML parser how to interpret the XML elements that are part of the namespace.

3

In your example, h is a shortcut for the fully qualified namespace http://www.w3.org/TR/html4. Now, imagine your document is processed by an XSLT stylesheet that you didn’t write. In this stylesheet, the same http://www.w3.org/TR/html4 namespace is associated with anoter prefix, html.
Without the namespace declaration, you wouldn’t know that html and h refer to the same namespace.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật