XML namespace: Difference between revisions

Content deleted Content added
Tags: Reverted section blanking
Rescuing 1 sources and tagging 0 as dead.) #IABot (v2.0.9.5
 
(8 intermediate revisions by 8 users not shown)
Line 1:
{{short description|Method of providing unique elements and attributes in an XML document}}
'''XML namespaces''' are used for providing uniquely named [[Data element|elements]] and attributes in an [[XML]] document. They are defined in a [[World Wide Web Consortium|W3C recommendation]].<ref>{{cite web|last=Bray|first=Tim|title=Namespaces in XML 1.0|url=http://www.w3.org/TR/xml-names/|publisher=[[W3C]]|access-date=9 October 2010|author2=Dave Hollander |author3=Andrew Layman |author4=Richard Tobin |author5=Henry S. Thompson |date=December 2009}}</ref><ref name="timelinehistory">{{cite web |url=http://www.dblab.ntua.gr/~bikakis/XML%20and%20Semantic%20Web%20W3C%20Standards%20Timeline-HistoryXMLSemanticWebW3CTimeline1.2.pdf |title=XML and Semantic Web W3C Standards Timeline (v.1.2) |access-date=2022-04-02 |archive-date=2022-12-29 |archive-url=https://web.archive.org/web/20221229130856/http://www.dblab.ntua.gr/~bikakis/XMLSemanticWebW3CTimeline1.2.pdf |url-status=dead }}</ref> An XML instance may contain element or attribute names from more than one XML vocabulary. If each vocabulary is given a [[namespace]], the ambiguity between identically named elements or attributes can be resolved.
 
A simple example would be to consider an XML instance that contained references to a customer and an ordered product. Both the customer element and the product element could have a child element named '''id'''. References to the '''id''' element would therefore be ambiguous; placing them in different namespaces would remove the ambiguity.
Line 6:
== Namespace names ==
 
A ''namespace name'' is a [[uniform resource identifier]] (URI). Typically, the URI chosen for the namespace of a given XML vocabulary describes a resource under the control of the author or organization defining the vocabulary, such as a [[uniform resource locator|URL]] for the author's Web server. However, the namespace specification does not require nor suggest that the namespace URI be used to retrieve information; it is simply treated by an XML parser as a string. For example, the document at http://www.w3.org/20211999/xhtml itself does not contain any code. It simply describes the [[XHTML]] namespace to human readers. Using a URI (such as <nowiki>"http://www.w3.org/20211999/xhtml"</nowiki>) to identify a namespace, rather than a simple string (such as "xhtml"), reduces the probability of different namespaces using duplicate identifiers.
 
Although the term ''namespace URI'' is widespread, the W3C Recommendation refers to it as the ''namespace name''. The specification is not entirely prescriptive about the precise rules for namespace names (it does not explicitly say that parsers must reject documents where the namespace name is not a valid [[Uniform Resource Identifier]]), and many XML parsers allow any character string to be used. In version 1.1 of the recommendation, the namespace name becomes an [[Internationalized Resource Identifier]], which licenses the use of non-ASCII characters that in practice were already accepted by nearly all XML software. The term ''namespace URI'' persists, however, not only in popular usage, but also in many other specifications from W3C and elsewhere.
Line 12:
Following publication of the Namespaces recommendation, there was an intensive debate about how a relative URI should be handled, with some intensely arguing that it should simply be treated as a character string, and others arguing with conviction that it should be turned into an absolute URI by resolving it against the base URI of the document.<ref>{{cite web|title=News from the trenches|first=Leigh|last=Dodds|date=24 May 2000|url=https://www.xml.com/pub/a/2000/05/24/deviant/index.html |publisher=O'Reilly Media, Inc.}}</ref> The result of the debate was a ruling from W3C that relative URIs were deprecated.<ref>{{citation|title=W3C XML Plenary decision on relative URI references in namespace declarations|first=Dan|last=Connolly|date=11 Sep 2000}}</ref>
 
The use of URIs taking the form of URLs in the http scheme (such as ''http://www.w3.org/20211999/xhtml'') is common, despite the absence of any formal relationship with the HTTP protocol. The Namespaces specification does not say what should happen if such a URL is dereferenced (that is, if software attempts to retrieve a document from this ___location). One convention adopted by some users is to place an [[RDDL]] document at the ___location.<ref>{{Cite web|first=Elliotte Rusty|last=Harold|title=RDDL Me This: What Does a Namespace URL Locate?|date=20 Feb 2001|url=http://www.oreillynet.com/pub/a/oreilly/xml/news/xmlnut2_0201.html|archive-url=https://web.archive.org/web/20170507150520/http://archive.oreilly.com/pub/a/oreilly/xml/news/xmlnut2_0201.html|archive-date=7 May 2017}}</ref> In general, however, users should assume that the namespace URI is simply a name, not the address of a document on the Web.
 
== Namespace declaration ==
Line 19:
For example, the following declaration maps the "xhtml:" prefix to the XHTML namespace:
 
<pre><nowiki>xmlns:xhtml="http://www.w3.org/20211999/xhtml"</nowiki></pre>
 
Any element or attribute whose name starts with the prefix "xhtml:" is considered to be in the XHTML namespace, if it or an ancestor has the above namespace declaration.
Line 25:
It is also possible to declare a default namespace. For example:
 
<pre><nowiki>xmlns="http://www.w3.org/20211999/xhtml"</nowiki></pre>
 
In this case, any element without a namespace prefix is considered to be in the XHTML namespace, if it or an ancestor has the above default namespace declaration.
Line 33:
Attributes are never subject to the default namespace. An attribute without an explicit namespace prefix is considered not to be in any namespace.
 
==<span lang="en" dir="ltr">Namespaces</span> in APIs and XML object models ==
 
Different specifications have taken different approaches on how namespace information is presented to applications.
Line 47:
==See also==
*[[Namespace]]
 
==References==
<references/>
 
==External links==