Xml Namespace

  • Uploaded by: Harsha
  • 0
  • 0
  • December 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Xml Namespace as PDF for free.

More details

  • Words: 3,541
  • Pages: 9
XML Namespace

1 of 9

http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_ns_...

XML Namespace Abstract A namespace is a set of names in which all names are unique. Any logically related set of names where each name should be unique is a namespace. Namespaces make it easier to come up with unique names. An XML namespace is a collection of names, identified by a URI reference [RFC2396], which are used in XML documents as element types and attribute names. XML namespaces differ from the "namespaces" conventionally used in computing disciplines in that the XML version has internal structure and is not, mathematically speaking, a set.

Introduction What is a Namespace? A namespace is a set of names in which all names are unique. Any logically related set of names where each name should be unique is a namespace. Namespaces make it easier to come up with unique names. Before a new name is added to a namespace, a namespace authority ensures that the new name doesn?t already exist in the namespace. Namespaces themselves must also be given names in order to be useful. Once a namespace has a name, it is possible to refer to its members. For example, Micosoft.RadioService and AOL.RadioService. The names in the namespace should be unique. If this cannot be guaranteed, then the actual namespace names themselves could also be placed into a name space of their own. For example,US.Microsoft.RadioService and CHINA.Microsoft.RadioService. In order to guarantee the uniqueness of namespace names, this pattern can be iterated as many times as necessary. More than one namespace may appear in a single XML document, to allow a name to be used more than once. Each reference can declare a prefix to be used by each name.

Why use namespace in XML? Many developers feel that the XML 1.0 specification was incomplete because it didn't offer namespace support. As a result, all names used in XML documents belonged to one global namespace, making it very difficult to come up with unique names. Most developers, including the XML 1.0 authors themselves, knew that this would eventually cause too much ambiguity in large XML-based distributed systems. Since element names in XML are not fixed, very often a name conflict will occur when two different documents use the same names describing two different types of elements. This XML document contains information in a HTML table:
Apples Bananas


This XML document contains information about a table (a piece of furniture): African Coffee Table <width>80120


If these two XML documents were added together, there would be an element name conflict because both documents contain a element with different content and definition. The Namespaces in XML Recommendation is the W3C's solution to the XML 1.0 naming woes. This specification defines how to extend the XML 1.0 concrete syntax to support namespaces. Because most developers consider this addition fundamental and absolutely necessary, it is often given the respect of an official XML 1.0 addendum, even

08/12/2008 22:42

XML Namespace

2 of 9

http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_ns_...

though it isn't one. In fact, today many developers refuse to refer to XML 1.0 alone but rather as "XML 1.0 + Namespaces" for the same reason. The Namespaces in XML Recommendation defines the syntax for naming XML namespaces as well as the syntax for referring to something in an XML namespace. It doesn't address, however, the syntax for defining what's in the XML namespace. This was left to another specification, namely XML Schema. There are more details in the XML Schema section. XML schema provides a "namespace" mechanism, which uses additional names to distinguish elements with the same name but different meaning in different contexts. The solution is to prefix a unique string to elements or attributes that need to be distinguished. This XML document contains information in a HTML table: Apples Bananas

This XML document contains information about a piece of furniture: African Coffee Table 80 120

Now the element name conflict is gone because the two documents use a different name for their
element ( and ). By using a prefix, we have created two different types of
elements. The XML namespaces provides a way to distinguish between duplicate element types and attribute names. Duplicates can occur when an XML document contains element types and attributes from more than one different XML schemas (or DTD).

XML Namespace

Definition An XML namespace is a collection of names, identified by a URI reference [RFC2396], which are used in XML documents as element types and attribute names. XML namespaces differ from the "namespaces" conventionally used in computing disciplines in that the XML version has internal structure and is not, mathematically speaking, a set. URI references which identify namespaces are considered identical when they are exactly the same characterfor-character. Note that URI references which are not identical in this sense may in fact be functionally equivalent. Examples include URI references which differ only in case, or which are in external entities which have different effective base URIs. Names from XML namespaces may appear as qualified name, which contain a single colon, separating the name into a namespace prefix and a local part. The prefix, which is mapped to a URI reference, selects a namespace. The combination of the universally managed URI namespace and the document's own namespace produces identifiers that are universally unique. Mechanisms are provided for prefix scoping and defaulting. URI references can contain characters not allowed in names, so cannot be used directly as namespace prefixes. Therefore, the namespace prefix serves as a proxy for a URI reference. An attribute-based syntax described below is used to declare the association of the namespace prefix with a URI reference; software which supports this namespace proposal must recognize and act on these declarations and prefixes.

Namespaces in XML

08/12/2008 22:42

XML Namespace

3 of 9

http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_ns_...

Any element or attribute in an XML namespace is uniquely identified by two parts: a namespace name (typeically a URI) and a local name. Such a two-part name is known as a qualified name or QName. In an XML document we use a namespace prefix to qualify the local names of both elements and attributes. A prefix is really just an abbreviation for the namespace identifier (URI) which is typically quite long and is a painful job to add it to element and attribute names in order to make them unique. ? The prefix is first mapped to a namespace identifier through a namespace declaration. ?The syntax for a namespace declaration is: xmlns:<prefix>='' A namespace declaration looks just like an attribute (on an element), but they're not officially considered attributes in terms of the logical document structure (that is, they won't appear in an element's attributes collection when using the DOM). An example namespace declaration, which associates the namespace prefix soap with the namespace name http://schemas.xmlsoap.org/soap/envelope: <soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope'>

Although a namespace usually looks like a URL, that doesn't mean that one must be connected to the Internet to actually declare and use namespaces. Rather, the namespace is intended to serve as a virtual "container" for vocabulary and undisplayed content that can be shared in the Internet space. In the Internet space URLs are unique?hence you would usually choose to use URLs to uniquely identify namespaces. Typing the namespace URL in a browser doesn't mean it would show all the elements and attributes in that namespace; it's just a concept. Note that the address used to identify the namespace, is not used by the parser to look up information. The only purpose is to give the namespace a unique name. However, very often companies use the namespace as a pointer to real Web page containing information about the namespace. The namespace attribute is placed in the start tag of an element. A namespace prefix is considered in-scope on the declaration element as well as on any of its descendant elements. Once declared, the prefix can be used in front of any element or attribute name separated by a colon (such as s:student). This complete name including the prefix is the lexical form of a qualified name (QName): QName = <prefix>: The prefix associates the element or attribute with the namespace identifier mapped to the prefix currently in scope. The following XML document describes about a room element. With two different namespaces defined for prefix 'h' and 'f', the 'table' elements are not conflict inside one XML document. Apples Bananas African Coffee Table 80 120

Since all XML documents must be well-formed, the start-tag to every element must have a matching end-tag. If a namespace declaration is the attribute to an element, it must go in an element start-tag. A namespace declaration is "in scope" for the entire content of that element, including the other attributes in the start-tag itself.

08/12/2008 22:42

XML Namespace

4 of 9

http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_ns_...

Since namespaces "h" and "f" were declared in the document element of the room document, any element or attribute in the document with the prefix "h:" is identified with the given http://www.w3.org/TR/html4 URI. It is not possible to get ride of the prefix and qualify an XML element directly with the namespace name identifier. Let?s rewrite the above XML document: Apples Bananas African Coffee Table 80 120

Such an XML document is unreadable regardless of the fact that the syntax is not valid, as there are two colons (":") in the qualifier.

Naming Namespaces XML namespace identifiers must follow the generic syntax for Uniform Resource Identifier (URI) defined by RFC 2396. Just like when you define a namespace in a programming language like C++, there are restrictions on the characters that may be used in the name. A (Uniform Resource Identifier) URI is defined as a compact string of characters for identifying an abstract or physical resource. In most situations, URI references are used to identify physical resources (Web pages, files to download, and so on), but in the case of XML namespaces, URI references identify abstract resources, specifically, namespaces. According to the URI specification, there are two general forms of URI: Uniform Resource Locators(URL) and Uniform Resource Names (URN). Either type of URI may be used as a namespace identifier. Here is an example of two URLs that could be used as namespace identifiers: http://www.develop.com/student http://www.ed.gov/elementary/students

And here are a few examples of URNs that could also be used as namespace identifiers: urn:www-develop-com:student urn:www.ed.gov:elementary.students urn:uuid:E7F73B13-05FE-44ec-81CE-F898C4A6CDB4

The most important attribute of a namespace identifier is that it is unique. Authors can guarantee the uniqueness of a URL by registering a domain name with an Internet naming authority. Then it's the author's responsibility to make sure that all strings used after the domain name are unique. URNs work the same way. The following is basic URN syntax: urn:: To guarantee the uniqueness of a URN, authors must again register their namespace identifier with an Internet naming authority. The author is then responsible for following a scheme for generating unique namespace-specific strings. Organizations defining XML namespaces should develop a consistent scheme for creating new namespace names. The W3C, for example, is constantly defining new XML namespaces. They use a fairly intuitive heuristic that uses the current year and the name of the working group.

08/12/2008 22:42

XML Namespace

5 of 9

http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_ns_...

By definition, a URI is unique, so there is never a need to layer additional namespaces on top of XML namespace identifiers. As long as the namespace author guarantees the uniqueness of the namespace identifier, it's always possible to uniquely identify something in XML with only a single namespace qualifier. This greatly simplifies the job of working with namespaces in XML. XML processors treat namespace identifiers as opaque strings and never as resolvable resources. Let me repeat: namespace identifiers are just strings! Two namespace identifiers are considered identical when they are exactly the same, character for character. In the end, it really doesn't matter which type of URI reference you choose to use. Many developers like to use URLs because they are easier to read and remember, while others prefer URNs because of their flexibility. Whichever type you choose, make sure you know how to ensure uniqueness.

Declaring Namespaces Definiation [Definition:] A namespace is declared using a family of reserved attributes. Such an attribute's name must either be xmlns or have xmlns: as a prefix. These attributes, like any other XML attributes, may be provided directly or by default.

Attribute Names for Namespace Declaration

[1]

NSAttName

::=

PrefixedAttName | DefaultAttName

[2]

PrefixedAttName

::=

'xmlns:' NCName

[3]

DefaultAttName

::=

'xmlns:'

[4]

NCName

::=

(Letter |'_') (NCNameChar)*

[5]

NCNameChar

::=

Letter | Digit | '.' | '-' | '_' | CombiningChar | Extender

[

NSC: Leading "XML]

/*

An XML Name, minus the ":" */

[Definition:] The attribute's value, a URI reference, is the namespace name identifying the namespace. The namespace name, to serve its intended purpose, should have the characteristics of uniqueness and persistence. It is not a goal that it be directly usable for retrieval of a schema (if any exists). An example of a syntax that is designed with these goals in mind is that for Uniform Resource Names [RFC2141]. However, it should be noted that ordinary URLs can be managed in such a way as to achieve these same goals. [Definition:] If the attribute name matches PrefixedAttName, then the NCName gives the namespace prefix, used to associate element and attribute names with the namespace name in the attribute value in the scope of the element to which the declaration is attached. In such declarations, the namespace name may not be empty. [Definition:] If the attribute name matches DefaultAttName, then the namespace name in the attribute value is that of the default namespace in the scope of the element to which the declaration is attached. In such a default declaration, the attribute value may be empty. An example namespace declaration, which associates the namespace prefix edi with the namespace name http://ecommerce.org/schema:

<x xmlns:edi='http://ecommerce.org/schema'>

08/12/2008 22:42

XML Namespace

6 of 9

http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_ns_...

Namespace Constraint: Leading "XML" Prefixes beginning with the three-letter sequence x, m, l, in any case combination, are reserved for use by XML and XML-related specifications.

Default Namespaces A default namespace declaration uses the following syntax: xmlns ='' Notice that there is not prefix. Defining a default namespace for an element saves us from using prefixes in all the child elements. All unqualified child element names of the element which defines the default namespace are automatically associated with the specific namespace identifier. The default namespace declarations, however, have absolutely no effect on attributes. To apply a namespace to an attribute, the only way is through a prefix (i.e., explicit qualified). This XML document contains information in a table:
Apples Bananas


In the above document, the default namespace identifier for the , , and
elements is "http://www.w3.org/TR/htm14/?. The default namespace can be set to the empty string. This has the same effect, within the scope of the declaration, of there being no default namespace.

NONAMESPACE (undeclaring namespace) declaration The following namespace declaration is known as the NONAMESPACE declaration: xmlns ="" The containing element and its child elements are declared to be in no namespace. An element that is in the NONAMESAPCE namespace has its namespace prefix and URI set to empty strings. John Smith C# <score>A

Here, "student" is from the http://www.develop.com/student namespace while "name" and "score" are from the default namespace urn:foo. 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 in XML 1.0). As a result, the language element also doesn't belong to a namespace. Here's an invalid example of unbinding a prefix per Namespaces in XML 1.0 spec, but a valid example per Namespaces in XML 1.1:

From this point on, the prefix lang cannot be used in the XML document because it is now undeclared as long as you

08/12/2008 22:42

XML Namespace

7 of 9

http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_ns_...

are in the scope of element language. Of course, you can definitely re-declare it. No namespace exists when there is no default namespace in scope. A {default namespace} is one that is declared explicitly using xmlns. When a {default namespace} has not been declared at all using xmlns, it is incorrect to say that the elements are in {default namespace}. In such cases, we say that the elements are in {no namespace}. {no namespace} also applies when an already declared {default namespace} is undeclared. If no default namespace is explicitly declared, as in the following XML document, then the URI associated with the default namespace is the current document. <mydoc> <X:a xmlns:X="http://www.mycorp.com/X.dtd"> this is a test

It is possible, however, to set up a default namespace whose URI is some other document. To do that, use a namespace declaration with a URI but no prefix.

Namespace Scope The scope of an XML namespace declaration declaring a prefix extends from the beginning of the start-tag in which it appears to the end of the corresponding end-tag, excluding the scope of any inner declarations with the same xmlns: <prefux> or xmlns (i.e., default namespace) declaration (in such case, the namespace name identifier has been changed). In other words, the namespace declaration is considered to apply to the element where it is specified and to all elements within the content of that element, unless overridden by another namespace declaration with the same NSAttName part. For example: <school:school xmlns:school='http://www.theschool.com/'> <school:department>Computer Science <school:building>Bill Gates Hall <school:lab>210 <school:classroom>212, 214, 216

Multiple namespace prefixes can be declared as attributes of a single element, as shown in this example: Cheaper by the Dozen 1568491379

Re-declaring Namespaces Namespace prefixes can be overridden by redeclaring the prefix at a nested scope, but it is not possible to undeclare a namespace prefix. Correct: 3235329 John Smith C# A

08/12/2008 22:42

XML Namespace

8 of 9

http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_ns_...

Incorrect: 3235329 ......

Uniqueness of Attributes The W3C "Namespaces in XML" specification places restrictions on setting the name and namespace of an attribute. No tag can contain two attributes with identical names, or with qualified names that have the same local name and have prefixes that are bound to identical namespace names. >For example, each of the bad start-tags is illegal in the following: <x xmlns:n1="http://www.w3.org" xmlns:n2="http://www.w3.org" >

However, each of the following good tag is legal, because the default namespace does not apply to attribute names: <x xmlns:n1="http://www.w3.org" xmlns="http://www.w3.org" >

Uniqueness of Attributes The W3C "Namespaces in XML" specification places restrictions on setting the name and namespace of an attribute. No tag can contain two attributes with identical names, or with qualified names that have the same local name and have prefixes that are bound to identical namespace names. >For example, each of the bad start-tags is illegal in the following: <x xmlns:n1="http://www.w3.org" xmlns:n2="http://www.w3.org" >

However, each of the following good tag is legal, because the default namespace does not apply to attribute names: <x xmlns:n1="http://www.w3.org" xmlns="http://www.w3.org" >

Summary

08/12/2008 22:42

XML Namespace

9 of 9

http://www.xyzws.com/TOPRINT?article=/scdjws/studyguide/xml_ns_...

A namespace is used to qualify XML element and attribute. In other words, both elements and attributes may be namespace-qualified. A prefix is not used to qualify an element. What effectively qualifies the element is the namespace to which the prefix is mapped. The scope of a declared namespace begins at the element where it is declared and applies to all the elements within the content of that element, unless overridden by another namespace declaration with the same prefix name. A tag with the form: <prefix:tagname> is called a ?Qualified Name? or ?QName? A {default namespace} exists only when you have declared it explicitly. It is incorrect to use the term {default namespace} when you have not declared it. Both prefixed and {default namespace} can be undeclared in XML 1.1, but prefixed can not be undeclared in XML 1.0. Both prefixed and {default namespace} can be overridden. {default namespace} does not apply to attributes directly. No namespace exists when there is no default namespace in scope.

08/12/2008 22:42

Related Documents

Xml Namespace
December 2019 7
Namespace Producto.docx
November 2019 3
Xml
June 2020 21
Xml
November 2019 35
Xml
May 2020 25
Xml
November 2019 45

More Documents from "api-26084493"

Xml Namespace
December 2019 7
Info.docx
November 2019 2
Sr_review[1].docx
October 2019 24
Services Marketing 2
December 2019 19
Acceptance Sampling
November 2019 25