110 likes | 226 Views
Compound Documents: Combining XML Vocabularies. Problems with Name Collision. Name collision occurs when elements from different XML vocabularies use the same name within a compound document.
E N D
Problems with Name Collision Name collision occurs when elements from different XML vocabularies use the same name within a compound document 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.
Namespaces: The Way to Resolve Name Collisions 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. To apply namespaces to compound XML documents you must: Declare the namespace Identify which elements in the document belong in the namespace
1. Declaring a Namespace Declaring a namespace consists of adding the attribute xmlnsto an element as follows: xmlns:prefix=“URI” where URI is a Uniform Resource Identifier that assigns a unique name to the namespace, and prefix is a string of letters that associates each element or attribute in the document with the declared namespace. For example, <model xmlns:mod=“http://jacksonelect.com/models”> declares a namespace with the prefix modand the URI http://jacksonelect.com/models
2. Identifying Elements in the Namespace After you declare a namespace, you indicate which elements in the document belong to that namespace by attaching the namespace prefix: <prefix:element> content </prefix:element> Example: <mod:modelxmlns:mod="http://jacksonelect.com/models"> <mod:title>Laser4C (PR205)</mod:title> <mod:description>Printer</mod:description> <mod:type>color laser</mod:type> <mod:ordered>320</mod:ordered> <mod:parts list="chx201,fa100,eng005,450V4,tn01" /> </mod:model>
Default Namespaces You can specify a default namespace by omitting the prefix in the namespace declaration. The element containing the namespace attribute and all of its child elements are assumed to be part of the default namespace. Example: <model xmlns="http://jacksonelect.com/models"> <title>Laser4C (PR205)</title> <description>Printer</description> <type>color laser</type> <ordered>320</ordered> <parts list="chx201,fa100,eng005,450V4,tn01" /> </model>
Adding Namespace Prefixes to Attributes Attributes, like elements, can become qualified by adding the namespace prefix to the attribute name: <prefix:elementprefix:attribute="value">…</prefix:element> Example: <mod:modelxmlns:mod="http://jacksonelect.com/models" mod:id=“123”> This doesn’t happen very often except when an attribute in one namespace is used as an attribute in another namespace.
Using Namespaces in a Style Sheet To declare a namespace in a style sheet, you add the following rule to the style sheet file: @namespace prefix “uri”;Example: @namespace mod “http://jacksonelect.com/models”; Selectors are associated with that namespace using the syntax: prefix|selector {attribute1:value1; …} Example: mod|title {width:150 px}
Combining Standard Vocabularies Standard vocabularies may be combined within a single XML document. Some well-known XML vocabularies are: