120 likes | 336 Views
Namespaces. Chapter Four. Introduction to Namespaces. Used to distinguish between elements and attributes that exist in different XML vocabularies. Group related elements and attributes from the same vocabulary so software can recognize the relationship
E N D
Namespaces Chapter Four
Introduction to Namespaces • Used to distinguish between elements and attributes that exist in different XML vocabularies. • Group related elements and attributes from the same vocabulary so software can recognize the relationship • Implemented by attaching a prefix to elements and attributes • Prefixes are mapped to a URI by xmlns:prefix attribute • Elements/attributes attached to the same URI are in the same namespace
The Need for Namespaces • Some documents combine markup from multiple XML applications • An XHTML document can contain both SVG pictures and MathML equations. • Both namespaces have a “Set” element • Validators will be confused by the elements that serve two different purposes. • See example on page 62 that includes duplicate element names. • You can change duplicate names, but this can be inconvenient.
Namespace Syntax • Namespaces eliminate ambiguity by assigning elements and attributes to URIs. • Elements from the same XML application are assigned to the same URI. • URIs are sometimes called namespace names. • Two elements with the same name but different URIs are NOT the same element • Two elements with the same name and the same URI ARE the same element. • Most of the time there is a one-to-one mapping between an application and a namespace. • Some complex applications use multiple namespaces for different functionality aspects.
Qualified Names, Prefixes, and Local Parts • URIs generally contain characters illegal in XML names. • Short prefixes replace the URIs in element and attribute names. • Each prefix is associated with a specific URI. • Composed from any legal XML name except colon. May not begin with “xml” either in upper, lower, or mixed case. • Everything before the colon is the prefix (Identifies the namespace) • Everything after the colon is the local part (Identifies the elements and attributes) • The complete name is the qualified name • Examples: svg:set, mathml:set
Binding Prefixes to URIs • Each prefix in a qualified name must be associated with a URI. • Prefixes are bound to namespace URIs by attaching an xmlns:prefix to the element or one of its ancestors • See example on the top of page 65. • Bindings have scope within the defined element and its contents. Can be declared in the topmost element or any ancestor. • A single namespace can include multiple namespace declarations for different namespaces. • Stylesheets can be used to apply different styles to elements from different namespaces. • Prefixes can be redefined, but this is not a good idea as it leads to confusion.
Namespace URIs • Prefixes are conventions and can easily be changed. • URIs, not prefixes, are standardized • Namespace URIs usually don’t point to an actual page. • They don’t have to use the http scheme • You can add documentation at the URI destination to avoid confusion. • Parsers compare URIs on a character by character basis. Case is taken into account.
Setting a Default Namespace with the XMLNS Attribute • You indicate an unprefixed element and its unprefixed descendants are part of a namespace by attaching an xmlns attribute with no prefix to the top element. • See example on page 67. • Default namespaces apply only to elements, not attributes. • You can change the default namespace within an element by adding an xmlns attribute. Future elements belong to the new default namespace. Previous elements belong to the first default namespace. See example on page 68. • Default namespace does not apply to any prefixed elements or attributes. • An unprefixed child element of a prefixed element belongs to the default namespace.
Attribute Declarations for xmlns • You can add a default namespace to an XML application even if there will be no namespace conflicts. See the example near the bottom of page 68.
How Parsers Handle Namespaces • Namespaces are NOT part of XML 1.0 • A namespace aware parser checks to see that all prefixes are mapped to URIs. • Will reject documents that use unmapped prefixes except for xml and xmlns. It will reject any element or attribute names that contain more than one colon. • Many parsers let you turn namespace processing off or on.
Namespaces and DTDs • Namespaces are independent of DTDs • However, you must include prefixes in the DTD (See example on bottom of page 69) • If a default namespace is used, the elements in the dtd should NOT include prefixes. • This is because parsers that don’t support namespaces can interpret the prefix as part of the name.
Parameter Entity References for Namespace Prefixes • If you define a namespace prefix as an entity, you can easily change the prefix name • You can also define the colon as an entity so it can easily be added and removed. This is useful when you later decide to use default namespaces. • See the examples on page 70-71.