230 likes | 360 Views
understanding xml namespaces. understanding namespaces.
E N D
understanding namespaces • Namespaces are the source of much confusion in XML, especially for those new to the technology. This confusion dissipates when one understands the meaning of namespaces along with how they are defined and used in XML documents both syntactically and abstractly.
what is a namespace? • A namespace is a set of names in which all names are unique • Namespaces make it easier to develop unique names • Namespace authorities ensure that new names don't already exist in the namespace • Namespaces can also be given names/identifiers
namespace qualified names • Namespace qualified names remove ambiguity • A namespace qualified name is a name scoped by a namespace identifier • If the namespace identifier is unique, so is the namespace qualified name • Unqualified names are ambiguous
namespaces in programming languages • Many programming languages provide syntax for defining and using namespaces • C++, Java, C#, and VB.NET all provide namespace support • Each language provides syntax for defining namespaces • Each language provides syntax for referring to names in a namespace
Figure 1.3: Using namespaces in C++ // syntax for defining namespaces namespace foo1 { class bar { ... }; } namespace foo2 { class bar { ... }; } // syntax for using namespaces foo1::bar b1; // refers to bar class in foo1 foo2::bar b2; // refers to bar class in foo2 using foo1; // sets foo1 as a default namespace bar b1; // refers to bar class in foo1
namespaces in xml • XML requires namespaces to avoid name collisions • XML 1.0 was considered incomplete because it didn't offer namespace support • Working with a single global namespace makes it hard to develop unique names • Mixing XML vocabularies in a namespace-less environment leads to name collisions • The Namespaces in XML Recommendation completed the XML 1.0 spec by defining support for namespaces
Figure 1.4: Example of name collisions in XML <!-- a Developmentor student --> <student> <id>3235329</id> <name>Jeff Smith</name> <language>C#</language> <rating>9.5</rating> </student> <!-- an elementary school student --> <student> <id>534-22-5252</id> <name>Jill Smith</name> <language>Spanish</language> <rating>3.2</rating> </student>
defining namespaces in xml • XML namespaces are defined by formal specification documents as well as schema definitions • Neither XML 1.0 or the namespaces specification codify how to define a namespace • Most XML namespaces are defined in formal specification documents (e.g., XSLT 1.0) • XML namespaces can also be defined in terms of XML schema definitions • All XML namespaces are given unique identifiers
xml namespace identifiers • XML namespaces identifiers are URIs (RFC 2396) • A URI is a compact string of characters for identifying an abstract or physical resource • There are two general types of URI: URL and URN • When a URI is used as a namespace identifier, it only identifies an abstract resource (the namespace) not a physical resource • Namespace identifiers are treated as opaque strings • Namespace identifiers are identical when they are exactly the same character for character
Figure 1.5: Examples of URLs and URNs http://www.develop.com/student http://www.ed.gov/elementary/students urn:www-develop-com:student urn:www.ed.gov:elementary.students urn:uuid:E7F73B13-05FE-44ec-81CE-F898C4A6CDB4
uniqueness of namespace identifiers • XML namespace identifiers must be unique • URIs, by definition, are unique • Internet naming authorities help ensure uniqueness • Organizations are required to ensure uniqueness within their assigned name • Organizations should develop conventions for creating new namespace identifiers (e.g., W3C)
using namespaces in xml documents • Namespaces in XML spec defines syntax for qualifying element/attribute names with a namespace identifier • Element/attribute names can be qualified with a namespace prefix (QName = prefix:local_name) • A namespace prefix is an abbreviation for a namespace identifier (URI) • Namespace prefixes are mapped to namespace identifiers through namespace declarations (xmlns:prefix='namespace identifier') • Namespace declarations are placed within element start tags just like attributes
Figure 1.7: Sample document using namespace qualified names <d:student xmlns:d='http://www.develop.com/student' xmlns:i='urn:schemas-develop-com:identifiers' xmlns:p='urn:schemas-develop-com:programming-languages' > <i:id>3235329</i:id> <name>Jeff Smith</name> <p:language>C#</p:language> <d:rating>9.5</d:rating> </d:student>
namespace declaration scope • A namespace prefix is considered in-scope on the declaration element and on any of its descendant elements • As long as the prefix is in-scope, it can be used in front of any element or attribute name separated by a colon • A prefix associates the element/attribute name with the associated namespace identifier • Prefixes can be overridden via nested declarations
Figure 1.8: Example of namespace declaration scoping <d:student xmlns:d='http://www.develop.com/student'> <d:id>3235329</d:id> <d:name xmlns:d='urn:names-r-us'>Jeff Smith</d:name> <d:language>C#</d:language> <d:rating>35</d:rating> </d:student>
default namespace declarations • Default namespace declarations are also used to associate namespace identifiers with element names • A default namespace declaration doesn't include a prefix (xmlns='namespace identifier') • When a default namespace declaration is used on an element, all un-qualified element names within its scope are automatically associated with the specified namespace • Default namespace declarations have no effect on attributes
Figure 1.9: Sample document using default namespace declaration <d:student xmlns:d='http://www.develop.com/student' xmlns='urn:foo' id='3235329'> <name>Jeff Smith</name> <language xmlns=''>C#</language> <rating>35</rating> </d:student>
namespace abstractions • Dealing with namespaces from the abstract view of an XML document is simpler than dealing with the lexical issues • The Infoset specifies that elements/attributes have two name properties: a namespace identifier and a local name • These two properties together determine the role of an element/attribute within a document • All other XML specifications deal with XML documents from this perspective
summary • A namespace is a set of names in which all names are unique • XML namespaces are defined in specification documents • XML namespaces are identified via URIs • Elements/attributes are associated with a namespace identifier through a namespace prefix or a default namespace declaration • Abstractly, elements/attributes have two name properties: namespace identifier and local name