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:
student>
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:
student>
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?
template>
In a large number of languages (eg, XSLT and XML architectures) that mix the XML vocabulary, the probability of name conflicts is extremely large. However, if XML provides support for named space, it can easily avoid these problems. "Namespaces in XML Recommendation" is a solution provided by W3C for XML 1.0 naming issues. This specification defines how to expand XML 1.0 specific syntax to support namespaces. Because most developers think that this new feature is absolutely necessary to add basic functions, this specification is usually considered formal additions to XML 1.0, although it is not formal. In fact, many developers now refuse to mention XML 1.0 separately, but refer to "XML 1.0 namespace", which is here.
"Namespaces in XML Recommendation" defines the name syntax of the XML namespace and the syntax of some content in XML namespace. However, it does not involve syntax used to define what is in the XML namespace. This is left to another specification (ie, XML architecture). Each of them needs some explanations.
Back to top
Name your namespace
When you define namespaces in programming languages (eg, C ), there are some limits for characters available in this name. The XML namespace identifier must also conform to the syntax of a specific syntax-unified resource identifier (URI). This means that the XML namespace identifier must comply with the common syntax of the URI defined by the RFC 2396.
The URI is defined as a compact string used to identify abstract or physical resources. In most cases, the URI reference is used to identify physical resources (files to be downloaded), but for XML namespaces, the URI reference is used to identify abstract resources (especially namespace).
According to the URI specification, there are two conventional types of URI: Unified Resource Locator (URL) and Unified Resource Name (URN). Both types of URIs can be used as namespace identifiers. Below is an example of two URLs that can be used as a namespace identifier:
http://www.develop.com/student
http://www.ed.gov/elementary/students
Here are examples of the URN that can also be used as the namespace identifier:
URN: WWW-Develop-Com: student
URN: www.ed.gov: Elementary.students
URN: UUID: E7F73B13-05FE-44EC-81CE-F898C4A6CDB4
The most important attribute of the namespace identifier is its uniqueness. The author can guarantee the uniqueness of the URL by registering the domain name to the Internet Naming, and then be responsible for ensuring that all strings used later are unique.
URN works in the same way. Below is the basic URN syntax:
URN:
To ensure the consistency of the URN, the author must reappear to register their namespace identifier to the Internet naming body. The author then is responsible for generating a unique string specific to named space in accordance with a certain scheme.
Organizations defining the XML namespace should set a consistent solution for the creation of the new namespace name. For example, W3c often defines a new XML namespace. These tissues use a quite intuitive test method that uses the current year and the name of the working group. Figure 2 illustrates the mode used by W3C.
Figure 2. W3C URI construct
Depending on the definition, the URI is unique, so there is no need to place other namespaces on the top of the XML namespace identifier. As long as the namespace author guarantees the uniqueness of the namespace identifier, you can always use a single namespace qualifier to uniquely identify the contents in XML. This greatly simplifies this work in the XML to handle namespaces. The XML processor treats the namespace identifier as an opaque string and will never treat them as resource. Reiterates again: The namespace identifier is just a string! They are considered the same when the characters in the two namespace identifiers are identical.
Finally, it does not do with which type of URI reference is used. Many developers like to use them because of the URLs, while other developers like to use them due to the flexibility of URN. No matter which type of choice, you must make sure you know how to guarantee uniqueness.
Back to top
Define namespace
"Namespaces in XML Recommendation" does not provide syntax for defining what is in the XML namespace. In many cases, this type of syntax definition is not necessarily required. Currently, most XML namespaces are defined in the formal specification document, which describes the name of the element, and attributes and its semantics. This means how to formally define all W3C namespaces (see the XSLT 1.0 specification on http://www.w3.org/tr/xslt to view the relevant example).
After defining a naming space, the software developer implements the namespace as outlined in the specification. For example, both MSXML 3.0, Xalan and Saxon are implemented for XSLT 1.0 specification. These implementations are hardcoded to find elements that belong to XSLT 1.0 namespace (http://www.w3.org/1999/xsl/transform). To use these implementations, you need to provide them with an XML document that correctly uses the name of the name of XSLT 1.0 (the next section will introduce this detailed information). If you want to change anything in the XSLT 1.0 namespace, the support software will have to update.
XML Architecture Working Group (http://www.w3.org/xml/schema) has merged a new specification (XML architecture), which defines an XML-based syntax for defining elements, attributes, and types in namespaces. . The XML architecture eventually makes the syntax definition that provides namespaces is possible, as shown below.
Targetnamespace = 'http://www.develop.com/student' ElementFormDefault = 'Qualified' > sequence> complextype> element> schema> This example is the http://www.develop.com/student namespace contains five naming elements: Student, ID, Name, Language and Rating. This architecture not only provides namespaces, but also other metadata, such as the order of the Student sub-elements, and their types. When there is a syntax namespace definition (for example, those defined by the XML schema), you can build more advanced software to utilize names and type information at runtime. The XML architecture still does not define the semantics of the defined element and attribute, so there will still be an attached specification. In the future, most XML namespaces will be defined simultaneously through specifications and architectures. Back to top Use namespace I define the usage process of the namespace as: one or more elements or properties in a given namespace in the XML document. This requires you to know the syntax outlined by "Names In XML Recommendation": Use the namespace identifier to qualify the element name and attribute name. The names of the elements and attributes are actually consisting of two parts: a namespace name and a local name. Such two parts are called so-called definition names or qnames. In an XML document, we use the namespace prefix to qualify the local name of the element and attribute. The prefix is actually an abbreviation of the namespace identifier (URI), and the URI is often quite long. The prefix is first mapped to the namespace identifier by name a spatial declaration. The syntax of the namespace declaration is: XMLns: The namespace declaration looks like (element) attribute, but from the logical structure of the document, they are not officially considered to be attribute (ie, when using the DOM, they will not appear in the properties of the elements). The namespace prefix is considered to be in the scope of declaration elements and any of its sub-generation elements. The prefix declared can be used in front of any element or attribute name (separated by a colon, for example, s: student). This full name including the prefix is the form of a definitive name (QNAME): Qname = The prefix is associated with an element or attribute associated with an element or attribute that is mapped to the prefix currently located in the scope. Let us assume that a developer wants to use the XSLT 1.0 namespace. He will need a namespace declaration to map any prefix to a formal XSLT 1.0 namespace identifier (http://www.w3.org/1999/xsl/transform). Then, only the developer wants to add a corresponding prefix from each element or attribute used in the XSLT 1.0 namespace, as shown in the following example: XMLns: x = 'http://www.w3.org/1999/xsl/transform' > x: Template> x: Transform> The above example shows the syntax of the reference element in the namespace. Each element of "X" is from http://www.w3.org/1999/xsl/transform namespace, without any element (for example, hello_world) from namespace. The processor can now distinguish between XSLT 1.0 programming constructs and will output text elements (for example, hello_world). If there is a character in the XSLT 1.0 namespace, the XSLT 1.0 processor will not be able to identify the document to the vocabulary it understand. In essence, each element now has a name consisting of two parts: a namespace identifier and a local name. These two names are combined together, commonly referred to as namespace name (note: This is different from QNAME, QNAME is combined by the prefix and the local name). The following XML document is another example, which shows how to define the use element from the XML schema displayed in front of this column: d: student> Note that both the syntax of the namespace is the same regardless of how the namespace is defined. When a document uses the elements or properties in multiple namespaces, multiple namespace declarations are typically performed for a given element, as shown in the following example: XMLns: i = 'URN: Schemas-develop-com: Identifiers' XMLns: P = 'URN: Schemas-develop-com: programming-languages' > d: student> Here, Student and Rating come from the same namespace, while ID and Language come from different namespaces, but Name is not named. The namespace prefix can also be rewritten by re-declaring in the nested scope, as shown below: d: student> In this example, in addition to the NAME element, all content comes from the same namespace - URN: Names-R-US Namespace. Although the namespace prefix can be re-declared, the declaration of the namespace prefix cannot be canceled. For example, the following statement is illegal: Please note that there is no prefix here. When a default namespace declaration is used for an element, all non-qualified element names in its scope are automatically associated with the specified namespace identifier. However, the default namespace declaration will never affect attributes. The only method is to use the prefix if you want to associate attributes with namespace identifiers. Consider the example: XMLns = 'URN: foo' id = '3235329' > d: student> Here, "Student" is from http://www.develop.com/student namespace, and "name" and "Rating" come from the default namespace URN: foo. The ID attribute is not a namespace, which is because attributes do not automatically associate with the default namespace identifier. This example also illustrates that you can cancel the declaration of the default namespace (just reset the default namespace identifier as empty string), such as the Language element (remember, for the prefix declaration cannot be done). Therefore, the language element is also not a namespace. The syntax of the default namespace is designed to improve the level of convenience, but they will lead to more confusion, not to bring greater value. This confusion is typically derived from the fact that the elements and properties are treated in different ways, and the nested elements are designated as the default namespace identifier is not immediately visible. However, when the attribute works, the selection between the prefix and the default namespace will eventually be mainly the problem of style. Back to top Namespace abstraction The abstract view of the XML document processing namespace is much simpler than the words that have just been described. The XML information set (Infoset defines the abstract structure of the XML document, which makes the developer no longer processes complex base serialization formats (for example, the namespace syntax just described). Follow INFOSET, each element or attribute has two name properties: a namespace identifier and a local name. Figure 3 illustrates a logical structure of an XML document that contains names defined by named space. Note that both Student, ID and Language come from the same namespace, while Rating comes from another namespace, Name is not named. This document can be serialized by any of the previous sections. Figure 3. XML document defined by named space Consider how today's mainstream API, SAX and DOM implement this abstract data model. SAX is modified to the element through the STARTELEMENT / Endelement method of ContentHandler: Public Interface ContentHandler { ???????????? Void StartElement (String Namespaceuri, String Localname, String Qname, Attributes atts............. Void endelement (String Namespaceuri, String Localname, String Qname). ???????????? } Note that these elements are identified by their namespace identifiers and local names (which can choose to use QNAME). The attribute can also be identified by a group of methods that can identify namespaces for Attributes interfaces. The SAX analyzer (or any other producer app) is responsible for providing a namespace name because it provides a document stream. That is, when using SAX, it will be very simple to programmatically distinguish between different types of Student elements. ???????????? Void StartElement (String Namespaceuri, String Localname, String Qname, Attributes ATTS) { IF (Namespaceuri.equals ("URN: DM: Student" && Localname.equals ("student"))) { // Process Developmentor Student Element Here } Else IF (Namespaceuri.equals ("URN: www.ed.gov: student") && localname.equals ("student"))) { // Process Elementary School Student Element Here } } ???????????? Due to the namespace name (namespace identifier local name) is automatically parsed by the SAX analyzer, what prefix is used in the specific element or attribute in the source document - it is mainly Serialization details. However, this does not mean that the prefix can be thrown away after analysis. Consider the following XML documentation: XMLns: xsi = 'http://www.w3.org/2000/10/xmlschema-instance' > student> Note that in this XML architecture, the XSI: Type property contains a QNAME value. Whenever use QNAME in the element or attribute content, commercial applications must manually process it. Only knowing "XSD" to bind to which namespace identifier is to interpret this value correctly. Therefore, INFOSET also maintains a namespace declaration within a set of scopes for each element in the document. SAX is modeled by startprefixmapping and endprefixmapping method. The DOM API is another implementation of Infoset. The DOM's Node interface is modeled by the basic identity of the element / attribute node through the following two name properties: Namespaceuri and LocalName. It also models the node's QNAME and prefix by NodeName and Prefix properties. The following Java language code explains how to distinguish between two different Student elements. Void ProcessStudent (Node N) { IF (N.GetNameSpaceuri (). Equals ("URN: DM: Student" && n.getlocalname (). Equals ("student"))) { // Process Developmentor Student Element Here } ELSE IF n.getNameSpaceuri (). Equals ("URN: WWW.ED.GOV: Student") && N.GetLocalName (). Equals ("student"))) { // Process Elementary School Student Element Here } } Just as SAX, the XML analyzer used to build a DOM tree is responsible for populating the corresponding namespace properties. Therefore, when using the logical document structure, how to declare the namespace in the source document is not important. If you want to create a document via the DOM API, you need to provide a namespace identifier for each element and attribute when you create a document: Void GenerateStudEntDocument (Document Doc) { Node Docel = Doc.createElementns ("URN: DM: Student", "Student"); Doc.Appendchild (DOCEL); Node n = doc.createElementns ("", "name"); Docel.Appendchild (n); ???????????? As you can see, this code allows you to create a logical structure directly. Then, the DOM implementation is responsible for determining how the namespace declares serialize the basis XML 1.0 document. This DOM tree can be serialized as follows: student> When you (via SAX / DOM API) process an abstract view of an XML document, be sure to pay attention to the concept of no default namespace. In the example previously mentioned, after CreateElementns for "Student", URN: DM: Student is not magic to become a default namespace. If CreateElementns is called for "Name" without namespace, the empty namespace identifier (not URN: DM: Student) is given to the NAME element. This is equally applicable to SAX on a series of StarTelement / endElement methods. Each element / attribute node is always processed in a name information. XPath is another XML specification that defines how to identify nodes in an abstract document structure. The XPath expression allows elements and properties to be identified by named space defined. Since the XPath name test is a simple string expression, only the XPath name test is associated with the namespace identifier by name a space prefix. You can treat XPath node tests as a QNAME type. That is to say, if a node test does not include a prefix, it is like looking for a given name that is not a namespace. For example, please use the XPath expression below: / student / name This expression identifies all NAME elements that do not belong to the namespace, as the child of the root Student element that is not named space. To identify the Student and Name elements belonging to URN: DM: Student Namespace, you must first associate the namespace prefix with the URN: DM: Student. Then you can use the prefix in the XPath expression. Suppose "DM" has been associated with URN: DM: Student in the XPath context, then the following expression will now be identified as a URN: DM: Store namespace, as the root of the URN: DM: Store Namespace Student Element Child's Name element: / DM: Student / DM: Name If the queryable document looks like the code below, the above expression will identify all three NAME elements (unrelated to their prefix) as the STUDENT child, because they come from the same namespace discussed. . s: student> In the XPath context, prefix is mapped in a way related to implementation (see how to do this in MSXML 3.0, see the THE XML Files column in May 2001). XSLT is an example of this, which provides a context using XPath expressions. To use the namespace defined XPath expression in the XSLT document, you can use the standard namespace declaration to map the target namespace identifier to any prefix: XMLns: x = 'http://www.w3.org/1999/xsl/transform' XMLns: D = 'URN: DM: Student' > ? ·? ·? · x: Transform> Note that the first template matches the Student element in the URN: DM: Student namespace. If matching value is just "Student", it will only match the Student element that is not a namespace. Apply-templates Elements then processes all Name child elements, which also belong to URN: DM: Student namespace. As you can see, from the words and theoretical understanding of the model of namespace, it is critical to understanding the entire series of XML specifications. As the XML specification continues to emerge, you will encounter many situations similar to what happened. Back to top summary Namespace is a set of unique names. The namespace in XML allows the unique name to be provided for elements and properties. Although the namespace always becomes a number of confusing sources, you will easily understand them after familiar with their definitions and use methods from grammar and theory. For more information on namespaces, see Namespaces in XML Recommendation and XML Namespaces By Example. Aaron Skonnard is the developmentor's lecturer and researcher, and he is also responsible for developing XML courses. Aaron wrote Essential Wininet (Published in 1998), and with other people with Essential XML (Addison-Wesley Longman, published in 2000). You can contact AARON with AARON through http://staff.develop.com/aarons. Go to the original English link