XML - Understand XML Namespace

xiaoxiao2021-03-06  74

Learn about XML namespace

Release Date: 4/14/2004

| Update Date: 4/14/2004

Aaron Skonnard

Developmentor

Update Date: July 2002

This page

What is a namespace? Namespace in the programming language XML in XML Naming Namespace Defines Namespace Name Space Use Namespace Name Space Abstract Summary

"Understanding XML Name Space" written by Aaron Skonnard, was originally published in MSDN Magazine in July 2001. This update version is licensed. Copyright? 2001 Microsoft Corp. and CMP Media LLC.

Namespace is a source of confusing in XML, and users who begin to learn that this technology is particularly confused. The problems that readers, students and participants often asked are always related to namespace. This actually has certain irony, because Namespaces in XML Recommendation is a streamlined XML specification (excluding appendix, no more than 10 pages). However, this confusion is related to namespace semantics, and is independent of syntax outlined in this specification. In order to fully understand the XML namespace, you must know what is named space, how to define namespaces and how to use them.

The rest of this column will specifically answer these three questions from grammar and theory. After reading this article, you will learn how the namespace affects the XML technical family.

What is a namespace?

Namespace is a set of unique names. For example, you can treat all my children's names as a namespace, just like California's name, the name of the C type identifier or the Internet domain name. Namespace is logically related any set of names, and each name must be unique.

Use a namespace to make the unique name. Imagine if the name must stay in the world, then how difficult it will give you the next child. If you limit uniqueness to a narrower context (for example, all of my children), the situation will be much simpler. When I start name for my next child, I just need to consider not using the name that is renowned with my other children. Another group of parents can choose the name I have used for their children, but these names must belong to different namespaces for easy distinction.

Before adding a new name to a naming space, the namespace mechanism must ensure that there is no new name in the namespace. In some cases, this will be very simple because it belongs to the child naming system. This will be quite complicated in other cases. Many Internet naming agencies today are a real example. However, if this step is ignored, the repeated name will eventually damage the namespace, which makes the name that does not have a polymity. If this happens, this group name will no longer be officially considered named space - according to the definition, namespace must ensure that its members are unique.

In order to make the namespace useful, you must also give the name itself. After the namespace has a name, you can reference its members. For example, consider displaying an example namespace in Figure 1 in Figure 1, the names of these two sample namespaces are Microsoft and Acmehardware, respectively. Note that even the two namespaces contain some of the same local names, there is also a local name without the namespace that is defined by the namespace, as shown in Figure 1.

Figure 1. Non-polynity namespace

Of course, the premise is that these namespace names are also unique. If this is not guaranteed, you can also put the actual namespace name itself in its respective namespaces. For example, if there are multiple ACMEHARDWARE stores (a in California, a in Utah), put this conflict in two different namespaces, as follows: California.acMehadware.paint

Utah.acmehardware.paint

This mode can be repeated multiple times as needed to ensure the uniqueness of the namespace name. This is exactly the same as the Internet Domain Name System (DNS), and DNS is a big namespace consisting of multiple namespaces.

If there is no namespace partition of this type, you will have to use the name of the extreme (unoccupied) to ensure uniqueness:

MicrosoftWindowsOperatingSystemPainTApplication

Imagine, if there is only one global namespace that cannot be partitioned, how complicated, how is it. People rely on naming space in daily socialization, although people don't know this in most cases. However, to use namespaces in software development, you must clear them through specific syntax. Let's take a look at the syntax examples of namespaces in a mainstream programming language today before transferring the namespace in XML.

Back to top

Namespace in the programming language

To use namespaces in a programming language, you must be familiar with the syntax used to define some of the namespaces and reference them. Many of today's languages ​​(including C , Java and C #) provide support for namespaces. In C , the namespace is defined by the namespace block, as shown below.

Namespace foo1

{

Class Bar

{

????????????

}

Class Baz

{

????????????

}

}

Namespace foo2

{

Class Bar

{

????????????

}

Class Baz

{

????????????

}

}

This example defines two namespaces: foo1 and foo2. Both namespaces contain two names: BAR and BAZ (in this case, they are class identifiers).

Foo1 :: bar b1; // Refers to bar class in foo1

Foo2 :: bar b2; // refers to bar class in foo2

To reference a BAR class with a specific namespace, you must use a given namespace identifier to qualify the BAR identifier.

For convenience, you can also do the following statement: use specific namespaces in a given source file. This will essentially make the specified namespace into the default namespace of the source file. As a result, there is no need to limit a specific namespace member, of course, it can be fully limited when it is absolutely necessary to avoid polymity:

Using namespace foo1;

Bar B1; // Refers to bar class in foo1

As you can see, C defines and use the syntax using the namespace. C # work mode is very similar to C , just a few small differences. Namespace syntax in Java is slightly different, but the concept is the same.

In many programming languages, namespaces can be used to help avoid name conflicts, which is the type of solution required to complete the XML 1.0 specification.

Back to top

Namespace in XML

Because the XML 1.0 specification does not provide namespace support, many developers feel that it is incomplete. Therefore, all the names in the XML document belong to a global namespace, which is difficult to implement the unique name. Many developers (including XML 1.0 author itself) know that this will eventually lead to too many polyses in a large-scale distributed system based on XML. For example, consider the following XML document:

3235329

jeff smith

C #

9.5

This document uses several names, each name is quite ordinary. Student elements model students in software training courses. ID, Language and Rating Elements Model the scores of the student's database record number, preferred programming language, and student's score (reference part 10). Each of them will definitely be used in other cases - in these cases they have different meanings.

For example, the following is another XML document, which uses the same name in a completely different way:

534-22-5252

jill smith

Spanish

3.2

In this example, the Student element models the primary school. Now, ID, Language and Rating elements are modeling their social security numbers, this nation language and current grade average score (reference points 4). The authors of these two documents can help ensure uniqueness, but this is ultimately unique, but it is even more difficult to use.

Although people can find out the difference after viewing these two documents, they look exactly the same for software. Imagine, you are responsible for building a student management application that must support many different XML documents related to students (including the two documents just mentioned). When writing code, how do you plan to distinguish between professional students and primary school students or any other type of student? There is no reliable way to make this distinction.

Using elements and properties from different XML vocabulary in the same document or application, naming conflicts are generated anyway. Consider XSLT, it itself is used to define the XML vocabulary of the conversion. In a given conversion, the user-defined text element can be output. Therefore, since the XSLT vocabulary contains an element named template, how do you output a name that is a user-defined text element for Template?