Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 understanding xml namespaces. 2 understanding namespaces Namespaces are the source of much confusion in XML, especially for those new to the technology.

Similar presentations


Presentation on theme: "1 understanding xml namespaces. 2 understanding namespaces Namespaces are the source of much confusion in XML, especially for those new to the technology."— Presentation transcript:

1 1 understanding xml namespaces

2 2 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.

3 3 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

4 4 Figure 1.1: Sample Namespaces

5 5 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

6 6 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

7 7 // 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 Figure 1.3: Using namespaces in C++

8 8 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

9 9 3235329 Jeff Smith C# 9.5 534-22-5252 Jill Smith Spanish 3.2 Figure 1.4: Example of name collisions in XML

10 10 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

11 11 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

12 12 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 Figure 1.5: Examples of URLs and URNs

13 13 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)

14 14 Figure 1.6: W3C namespace identifier examples

15 15 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

16 16 <d:student xmlns:d='http://www.develop.com/student' xmlns:i='urn:schemas-develop-com:identifiers' xmlns:p='urn:schemas-develop-com:programming-languages' > 3235329 Jeff Smith C# 9.5 Figure 1.7: Sample document using namespace qualified names

17 17 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

18 18 3235329 Jeff Smith C# 35 Figure 1.8: Example of namespace declaration scoping

19 19 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

20 20 <d:student xmlns:d='http://www.develop.com/student' xmlns='urn:foo' id='3235329'> Jeff Smith C# 35 Figure 1.9: Sample document using default namespace declaration

21 21 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

22 22 Figure 1.10: Namespaces in the Infoset

23 23 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


Download ppt "1 understanding xml namespaces. 2 understanding namespaces Namespaces are the source of much confusion in XML, especially for those new to the technology."

Similar presentations


Ads by Google