XML 2nd EDITION Tutorial 2 Working With Namespaces
XP Combining XML Vocabularies A document that combines several vocabularies is known as a compound document
XP Working With Namespaces Name collision occurs when elements from two or more documents share the same name. Name collision is not a problem if you are not concerned with validation. The document content only needs to be well-formed. However, name collision will keep a document from being validated.
XP Name Collision This figure shows name collision Parts vocabulary Model vocabulary
XP Declaring a Namespace A namespace is a defined collection of element and attribute names. Names that belong to the same namespace must be unique. Elements can share the same name if they reside in different namespaces. Namespaces must be declared before they can be used.
XP 6 Declaring a Namespace A namespace can be declared in the prolog or as an element attribute. The syntax for an attribute used to declare a namespace in the prolog is: xmlns:prefix=“URI” where URI is a Uniform Resource Identifier that assigns a unique name to the namespace prefix is a string of letters “Short hand name”Full, unique name
XP Declaring a Namespace For example, xmlns:mod=“ declares a namespace with the prefix “mod” and the URI The URI is not a Web address. A URI (Uniform Resource Identifier) identifies a physical or an abstract resource. URI (Uniform Resource Identifier) URL (Uniform Resource Locator) Physical Resource: file, a Web page, or an e- mail address URN (Uniform Resource Name) Persistent name for a resource
XP URIs, URLs, and URNs URN (Universal Resource Name) is a persistent resource identifier, meaning the user need only know the name of a resource. URNs take the form: urn:NID:NSS NID = namespace identifier NSS = text string specific to that the namespace Example – urn: isbn:
XP Applying a Namespace to an Element Use prefix instead of the URI to qualify element names General form: content For example xmlns:mod= Laser4C (PR205)
XP Applying a Namespace to an Element Prefixed names are called qualified names Example: Laser4C (PR205) A name without a namespace prefix is called an unqualified name. Example: Laser4C (PR205)
XP Declaring a Namespace as an Element Attribute For example, the code: Laser4C (PR205) Entry level color laser printer color laser 320 …applies the namespace to the model element and all of its child elements.
XP Declaring a Default Namespace You can specify a default namespace by omitting the prefix in the namespace declaration. Example: Laser4C (PR205) Entry level color laser printer color laser 320 All elements including the model element, are considered to be part of the name space :mod removed
XP Using Namespaces with Attributes Attributes, like elements, can become qualified by adding the namespace prefix to the attribute name. The syntax is: … For example: <mod:model xmlns:mod= mod:id=“pr205”> …. Attributes inherit their namespace from their parent element: For example: <mod:model xmlns:mod= id=“pr205”> …..
XP Adding a Namespace to a Style Sheet: Declaring a Namespace To declare a namespace in a style sheet, you add the following rule to the style sheet prefix url(uri); Where prefix is the namespace prefix and uri is the URI of the namespace mod url(
XP Applying a Namespace to a Selector Once you’ve declared a namespace in a style sheet, you can associate selectors with that namespace using the syntax: prefix|selector {attribute1:value1; attribute2:value2;…} For example: mod|title {width: 150px} You also can use the wildcard symbol (*) to apply a style to any element within a namespace or to elements across different namespaces
XP Applying a Namespace to a Selector You also can use the wildcard symbol (*) to apply a style to any element within a namespace or to elements across different namespaces mod | title {width: 15px} applies width style to all title elements in the models namespace mod | * {font-size: 12pt} applies the font-size style to any element within the models namespace title {width: 15pt} applies a width of 150 pixels to any element named title Not supported by Firefox, Opera, and Netscape
XP Combining Standard Vocabularies Standard vocabularies may be combined within single documents
XP A compound XHTML and MathML document
XP XHTML
XP Summary Combining vocabularies in a single document can result in name collisions Use namespaces to resolve collisions Namespaces use URI to provide unique identification for each vocabulary in a document CSS style sheets can be modified to accommodate namespaces You can create an XHTML document that combines features from XHTML and other XML vocabularies