Wednesday, 23 January 2019

How do I declare an XML namespace in an XML document

How do I declare an XML namespace in an XML document?

Answer:


To declare an XML namespace, you use an attribute whose name has the form:
xmlns:prefix
--OR--
xmlns
These attributes are often called xmlns attributes and their value is the name of the XML
namespace being declared; this is a URI. 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.
For example, the following declares two XML namespaces, named
http://www.google.com/ito/addresses and http://www.google.com/ito/servers. The first
declaration associates the addr prefix with the http://www.google.com/ito/addresses
namespace and the second declaration states that the http://www.google.com/ito/servers
namespace is the default XML namespace.
<Department
xmlns:addr="http://www.google.com/ito/addresses"
xmlns="http://www.google.com/ito/servers">
NOTE: Technically, xmlns attributes are not attributes at all -- they are XML namespace
declarations that just happen to look like attributes. Unfortunately, they are not treated
consistently by the various XML recommendations, which means that you must be
careful when writing an XML application.
For example, in the XML Information Set (http://www.w3.org/TR/xml-infoset), xmlns
"attributes" do not appear as attribute information items. Instead, they appear as
namespace declaration information items. On the other hand, both DOM level 2 and SAX
2.0 treat namespace attributes somewhat ambiguously. In SAX 2.0, an application can
instruct the parser to return xmlns "attributes" along with other attributes, or omit them
from the list of attributes. Similarly, while DOM level 2 sets namespace information
based on xmlns "attributes", it also forces applications to manually add namespace
declarations using the same mechanism the application would use to set any other
attributes.

No comments:

Post a Comment