What does namespace mean in xml
Namespaces can be declared either explicitly or by default. With an explicit declaration, you define a shorthand, or prefix, to substitute for the full name of the namespace. You use this prefix to qualify elements belonging to that namespace.
Explicit declarations are useful when a node contains elements from different namespaces. The previous example shows the syntax for referring to elements within a namespace. Processors can now distinguish between XSLT 1. If the XSLT 1. In essence, each element now has a two-part name, a namespace identifier and a local name. The combination of these two names is often referred to as the namespace name note: this is different from the QName, which is the combination of the prefix and the local name.
As another example, the following XML document shows how to use the elements from the XML Schema definition shown earlier in this column:. Notice that regardless of how namespaces are defined, the syntax for referring to them is the same. When documents use elements or attributes from more than one namespace, it's common to have multiple namespace declarations on a given element, as illustrated by the following example:.
Here, student and rating are both from the same namespace, while id and language are each from a different namespace, but name doesn't belong to a namespace. Namespace prefixes can also be overridden by redeclaring the prefix at a nested scope, as shown here:.
In this example, everything is from the same namespace except for the name element, which is from the urn:names-r-us namespace. While it is possible to re-declare a namespace prefix, it's not possible to undeclare a namespace prefix. For example, the following is illegal:. The prefix-oriented syntax for referring to things in an XML namespaces is fairly intuitive for most software developers.
Had the Namespaces in XML Recommendation stopped here, namespaces would have been much less confusing. There is one more type of namespace declaration that can be used to associate namespace identifiers with element names. This is known as a default namespace declaration, which uses the following syntax:. Notice that there is no prefix. When a default namespace declaration is used on an element, all unqualified element names within its scope are automatically associated with the specified namespace identifier.
Default namespace declarations, however, have absolutely no effect on attributes. The only way to associate an attribute with a namespace identifier is through a prefix. Consider the following example:. The id attribute doesn't belong to a namespace since attributes aren't automatically associated with the default namespace identifier. This example also illustrates that you can undeclare a default namespace by simply setting the default namespace identifier back to the empty string, as shown in the language element remember you cannot do this with prefix declarations.
As a result, the language element also doesn't belong to a namespace. The syntax for default namespaces was designed for convenience, but they tend to cause more confusion than they're worth. The confusion typically stems from the fact that elements and attributes are treated differently and it's not immediately apparent that nested elements are being assigned the default namespace identifier. Nevertheless, in the end, choosing between prefixes and default namespaces is mostly a matter of style, except when attributes come into play.
Dealing with namespaces from the abstract view of an XML document is much simpler than dealing with the lexical issues just described. The XML Information Set Infoset defines the abstract structure of an XML document, which shields developers from the complexities of the underlying serialization format, like the namespace syntax just described. According to the Infoset, each element or attribute has two name properties: a namespace identifier and a local name.
Figure 5 illustrates the logical structure of an XML document that contains namespace-qualified names. Notice that student, id, and language are all from the same namespace, while ratings is from a different namespace and name belongs to no namespace.
This document could be serialized out using either of the techniques described in the previous section. Notice that elements are identified by the combination of their namespace identifier and local name and optionally, the QName. Attributes are also identified through a set of namespace-aware methods on the Attributes interface.
It's up to the SAX parser or any other producer application to provide namespace names as it delivers the document stream. That said, using SAX it's simple to distinguish between different types of student elements programmatically see Figure 6. That does not mean, however, that prefixes can be thrown away once parsed. Consider the following XML document:. Any time a QName is used in element or attribute content, the consuming application is required to deal with it manually.
The only way the consuming application can interpret this value correctly is if it knows what namespace identifier "xsd" is bound to. For this reason, the Infoset also maintains the set of in-scope namespace declarations for each element in the document. It also models the node's QName and prefix through the nodeName and prefix properties. The Java language code in Figure 7 illustrates how you could distinguish between two different student elements using the DOM.
So again, it doesn't matter how the namespaces were declared in the source document once you're working with the logical document structure.
If you're creating the document through the DOM API, then you're responsible for supplying the namespace identifiers for each element and attribute upon creation:.
As you can see, this code allows you to create the logical structure directly. Then it's up to the DOM implementation to figure out how to serialize the namespace declarations into the underlying XML 1. This particular DOM tree could be serialized as follows:. See also translations.
W3C liability , trademark and document use rules apply. XML namespaces provide a simple method for qualifying element and attribute names used in Extensible Markup Language documents by associating them with namespaces identified by URI references.
This section describes the status of this document at the time of its publication. Other documents may supersede this document. The English version of this specification is the only normative version. Known implementations are documented in the Namespaces 1. This third edition incorporates all known errata as of the publication date.
It supersedes the previous edition of 16 August This edition has been widely reviewed. Only minor editorial changes have been made since the 6 August Proposed Edited Recommendation. Please report errors in this document to xml-names-editor w3. This document has been reviewed by W3C Members, by software developers, and by other W3C groups and interested parties, and is endorsed by the Director as a W3C Recommendation.
It is a stable document and may be used as reference material or cited from another document. W3C's role in making the Recommendation is to draw attention to the specification and to promote its widespread deployment. This enhances the functionality and interoperability of the Web. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim s must disclose the information in accordance with section 6 of the W3C Patent Policy.
We envision applications of Extensible Markup Language XML where a single XML document may contain elements and attributes here referred to as a "markup vocabulary" that are defined for and used by multiple software modules.
One motivation for this is modularity: if such a markup vocabulary exists which is well-understood and for which there is useful software available, it is better to re-use this markup rather than re-invent it.
Such documents, containing multiple markup vocabularies, pose problems of recognition and collision. Software modules need to be able to recognize the elements and attributes which they are designed to process, even in the face of "collisions" occurring when markup intended for some other software package uses the same element name or attribute name.
These considerations require that document constructs should have names constructed so as to avoid clashes between names from different markup vocabularies. This specification describes a mechanism, XML namespaces , which accomplishes this by assigning expanded names to elements and attributes.
Note that many of the nonterminals in the productions in this specification are defined not here but in the XML specification [XML]. When nonterminals defined here have the same names as nonterminals defined in the XML specification, the productions here in all cases match a subset of the strings matched by the corresponding ones there.
Accordingly, if the namespace identifier is different, attributes having the same attribute name are considered to be different. A "default namespace" is a namespace declaration that does not use a namespace prefix See Figure 11 for notation method. The scope of the default namespace is the element for which the namespace was declared and the related content, just as with the namespace scope discussed earlier.
The benefit of using a default namespace is that the namespace prefix can be omitted. For example, when adding a new namespace to an existing XML document, writing a namespace prefix for each element to which the new namespace will be applied involves a tremendous amount of tedious work.
The larger the XML document, the greater the labor involved, and the greater the likelihood of notation errors. In this type of situation, adding only a default namespace declaration to the XML document in question eliminates the need to write a namespace prefix for each and every element, saving a lot of time.
On the other hand, there are drawbacks. One drawback is that omitting the namespace prefix makes it more difficult to understand which element belongs to which namespace, and which namespace is applicable. In addition, programmers should remember that when a default namespace is declared, the namespace is applied only to the element, and not to any attributes. A default namespace can be overwritten partially by declaring a completely different default namespace within the scope of the original default namespace.
A default namespace can be canceled using the following notation method:. A namespace using a namespace prefix can be designated within the scope of a default namespace. Select which of the following correctly describes the namespace to which the "ProductInformation" element belongs, given the XML Document "product.
For the "ProductInformation" element, two namespaces have been declared: the namespace "urn:product:Info" with the namespace prefix "inf", and the namespace "urn:product:Stock" with the namespace prefix "stk". The "ProductInformation" element has the namespace prefix "inf", and as such, the "ProductInformation" element belongs to the namespace "urn:product:Info".
Accordingly, the correct answer is A. Select which of the following correctly describes the namespace to which the "Code" attribute of the "ProductName" element belongs, given the XML Document "stock.
First, we confirm the namespace declaration in the XML document. Two namespaces have been declared for the "InventoryInfo" element: the namespace "urn:product;Stock" with the namespace prefix "stk", and the namespace "urn:product:Info" with the namespace prefix "inf".
0コメント