1 / 12

Namespaces

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

ori-meadows
Télécharger la présentation

Namespaces

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Namespaces Chapter Four

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

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

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

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

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

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

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

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

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

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

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

More Related