Why namespace in xml is important




















Software Engineering. Web Technology. Cyber Security. C Programming. Control System. Data Mining. Data Warehouse. Javatpoint Services JavaTpoint offers too many high quality services.

Let's see the example of XML file. How to get rid of name conflict? It is not used by parser to look up information. It is only used to provide a unique name to the Namespace identifier.

The Default Namespace The default namespace is used in the XML document to save you from using prefixes in all the child elements. 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. A default declaration declares a namespace to be used for all elements within its scope, and a prefix is not used. For example, traditional namespaces are used to contain the names identifiers of element types in an XML document, the names of class variables in a Java class, and the names of tables in a relational database.

Traditional namespaces are useful in this regard because of their requirement that each name in the namespace be unique. Thus, when a new name identifier is added to the namespace, the uniqueness of the identifier can be verified by checking that the name does not already exist in the namespace. Note that just because a set of objects draws its names from some traditional namespace does not mean that those names uniquely identify the objects.

For example, two different people can share the same name, as can two different element nodes in a DOM tree, which use element type names as their names. For the names in a traditional namespace to uniquely identify the objects in a set, the objects in the set must draw their names only from that namespace and no name can be applied to more than one object.

In practice, the names from a single traditional namespace are only used to identify the objects in a single set; otherwise, additional information must be stored stating which names apply to which set.

For example, consider the following two XML documents:. Each document uses a different XML language and each language defines an Address element type. Each of these Address element types is different -- that is, each has a different content model, a different meaning, and is interpreted by an application in a different way. This is not a problem as long as these element types exist only in separate documents. But what if they are combined in the same document, such as a list of departments, their addresses, and their Web servers?

How does an application know which Address element type it is processing? One solution is to simply rename one of the Address element types -- for example, we could rename the second element type IPAddress. However, this is not a useful long term solution. One of the hopes of XML is that people will standardize XML languages for various subject areas and write modular code to process those languages.

By reusing existing languages and code, people can quickly define new languages and write applications that process them. If we rename the second Address element type to IPAddress, we will break any code that expects the old name.

A better answer is to assign each language including its Address element type to a different namespace. This allows us to continue using the Address name in each language, but to distinguish between the two different element types. The mechanism by which we do this is XML namespaces. Note that by assigning each Address name to an XML namespace, we actually change the name to a two-part name consisting of the name of the XML namespace plus the name Address.

This means that any code that recognizes just the name Address will need to be changed to recognize the new two-part name. However, this only needs to be done once, as the two-part name is universally unique. For more information about the uniqueness of universal name, see question An XML namespace is a collection of element type and attribute names.

The collection itself is unimportant; what is important is its name -- a URI. This allows XML namespaces to provide a two-part naming system for element types and attributes. The second part is the element type or attribute name itself -- the local part , also known as the local name. Together, they form the universal name. For example, the following document uses XML namespaces to distinguish between two different element types named Address. Thus, each universal name is unique, meeting the requirement that each element type in an XML document have a unique name.

XML namespaces are collections of names, nothing more. That is, they contain the names of element types and attributes, not the elements or attributes themselves. For example, consider the following document.

The element type name B and the attribute name D are not in any XML namespace because no prefix maps them there.

This is because XML namespaces contain names , not elements or attributes. XML namespaces also do not contain the definitions of the element types or attributes. This is an important difference, as many people are tempted to think of an XML namespace as a schema, which it is not. For more information, see myth 8 in "Namespace Myths Exploded".

If an element type or attribute name is not specifically declared to be in an XML namespace -- that is, it is unprefixed and in the case of element type names there is no default XML namespace -- then that name is not in any XML namespace.

XML namespaces apply only to element type and attribute names. Furthermore, in an XML document that conforms to the XML namespaces recommendation, entity names, notation names, and processing instruction targets must not contain colons.

Anybody can create an XML namespace -- all you need to do is assign a URI to identify it and decide what element type and attribute names are in it. The URI must be under your control and should not be being used to identify a different XML namespace, such as by a coworker.

In practice, most people that create XML namespaces also describe the element types and attributes whose names are in it -- their content models and types, their semantics, and so on. However, this is not part of the process of creating an XML namespace, nor does the XML namespace include or provide a way to discover such information. If you don't have any naming conflicts in the XML documents you are using today, as is often the case with documents used inside a single organization, then you probably don't need to use XML namespaces.

However, if you do have conflicts today, or if you expect conflicts in the future due to distributing your documents outside your organization or bringing outside documents into your organization, then you should probably use XML namespaces.

The first form of the attribute xmlns: prefix declares a prefix to be associated with the XML namespace. The second form xmlns declares that the specified namespace is the default XML namespace. They just happen to look like attributes. This has important consequences for XML application writers. Instead, they appear as namespace declaration information items. Similarly, SAX 2. The namespace is in scope for that element and all its descendants unless it is overridden see question 4.

For more information about scope, see section 6. For example, suppose the preceding DTD was placed in an external entity foo. This would result in an error because the foo prefix was never declared:.

Instead, these defaults declarations take effect only when the attribute is instantiated on an element. For example:. For more information, see question 7. Note that Internet Explorer 5. For more information, see question 8. To override the prefix used in an XML namespace declaration, you simply declare another XML namespace with the same prefix.

Using multiple default XML namespaces can lead to documents that are confusing to read and should be done carefully. For more information, see question 5. You cannot "undeclare" an XML namespace prefix. It remains in scope until the end of the element on which it was declared unless it is overridden.

Trying to undeclare a prefix by redeclaring it with an empty zero-length URI results in an error. Within the scope of this declaration, unprefixed element type and attribute names do not belong to any XML namespace. I don't know the answer to this question, but the likely reason is that they simplify the process of moving fragments of one document to another document.

While these were simple to read and process, they weren't easy to move to other documents. Attributes, on the other hand, were intimately attached to the elements being moved. Make sure you have declared the prefix see question 4.

All you need to do then is prefix the local name of an element type or attribute with the prefix and a colon. The result is a qualified name see question Note that the prefix is used on both the start and end tags.

Make sure you have declared the default XML namespace see question 4. All you need to do then is use the local name of an element type. Even though it is not prefixed, the result is still a qualified name see question For information about how to use the default XML namespace with attribute names, see question 5. The default XML namespace only applies to element type names, so you can refer to attribute names that are in an XML namespace only with a prefix.

In the following, the type attribute name does not refer to that namespace, although the Address element type name does. For information about how to use the default XML namespace with element type names, see question 5. However, the same namespace declaration would have to be repeated many times, making the XML document difficult to follow. In this type of situation, it is best to make a collective set of namespace declarations in the highest-order element, as shown in the LIST1 example. Attributes can either belong to the same namespace of the element for which the attribute is defined, or belong to a completely different namespace than that to which the element belongs.

The definition for an attribute that belongs to a namespace is the same as that for elements, with the "namespacePrefix:" notation coming before the attribute name. If no namespace prefix is provided for the attribute name, the attribute will not belong to any namespace. What must be noted here is that under the XML 1. However, for XML documents conforming to the Namespaces in XML specification, the combination of namespace identifier and attribute name is what determines whether an attribute is duplicated.

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.



0コメント

  • 1000 / 1000