Using Namespaces in XML

  • View

  • Download

Embed Size (px)


Using Namespaces in XML. COIN 78 Lecture 7. Designing a namespace name. Distinguish one similarly titled element from another Unique and permanent name In XML, must have an absolute URL Use your own domain name as a root to keep control over name’s uniqueness - PowerPoint PPT Presentation

Text of Using Namespaces in XML

  • Using Namespaces in XMLCOIN 78Lecture 7

  • Designing a namespace nameDistinguish one similarly titled element from anotherUnique and permanent nameIn XML, must have an absolute URLUse your own domain name as a root to keep control over names uniquenessURL may point to a DTD or schema, but that is not required

  • Sample code protocolyour domain nameoptional namespaces directory informationshort description of the namespaceoptional version number

  • Declaring Default NamespacesTo declare a default namespace for an element and all its children:Within the opening tag of an element, type: xmlns=Then type URL where URL is the name of the namespace

  • Sample code - in No Namespace

    Giant River Otter

  • Sample Code..

    With the namespace declaration in the root element, all of the elements in the document are associated with the namespace

  • Namespaces for Individual ElementsTo declare a prefix for a namespace name:In the documents root element, type xmlns:prefix where prefix will be the nickname for this namespaceThen type =URL where URL is the name of the namespace to which the prefix will refer

  • Sample Code
  • How Namespaces Affect AttributesYou could associate an attribute with a specific namespace, however...By prefixing it with appropriate prefix, its almost never necessary, since...Attributes are already made unique by the element that contains them...

  • Sample Code

    sectionid does not require a separate name spaceThe two elements that contain it, source and orange, have no overlap

  • Namespaces, Schemas, and Valid DocumentsAn element is more than its name aloneXML processors consider the element to be called prefix:elementIf you want to validate the document against a schema, you must declare each prefix element in the schemaAdditionally, it is necessary to declare the attribute with which you declare the namespace, either xmlns or xmlns:prefix

  • SummaryNamespaces create super labels that can distinguish elements of similar names when two or more documents are combined.The super label is called the namespace nameThe namespace itself is the collection of related names identified by the namespace name.Namespaces are located by URLs that point to a file with a given name. Namespaces may apply to all elements in a document or just one.

  • Namespaces, Schemas, and ValidationCOIN 78Lecture 8

  • Schemas and NamespacesNamespaces are a collection of related elements and attributes, identified by a common URL-shaped name.Namespaces are most often used to distinguish similarly named globally declared elements from one another.A schema can define what an XML document should look like, and can simultaneously create or populate a namespace.

  • Populating a NamespaceYou can associate the globally declared (top level) components of a schema with a namespace in order to use those components in other schema documents.To specify a target namespace:Type targetNamespace=URLWhere URL is the namespace with which you want to associate the components defined in this schema: called populating a namespace

  • Adding All Locally Declared ElementsTo add all the locally declared elements elements to the target namespace:In the xsd:schema element, type elementFormDefault=qualified

  • Adding Particular Locally Declared ElementsTo add a particular locally declared declared element to the target namespaceIn the elements declaration, type form=qualifiedTo keep a particular locally declared element from being associated with a target namespace (despite the default)In the element's declaration type: form=unqualified

  • Referencing Components with NamespacesTo specify a default namespace for referenced components and then reference those components in the schema:In the root element of the schema document, type xmlns=URL where URL is the namespace for the referenced components.In the value of the type and ref attributes, type reference where reference is the name of the component associated with the default namespace.

  • The Schema of Schemas as the DefaultYou can declare the schema of schemas as the default namespace for a schemaType to complete the schema tag

  • Namespaces and Validating XMLYou can qualify a set of components (identify them with a namespace) and validate a document that uses those components.You must indicate the namespace of the desired components by either declaring a default namespace (xmlns=URL) orDeclaring a namespace with a prefix (xmlns:prefix=URL). It is sometimes necessary to indicate where the schema

  • Indicating Where a Schema IsIn the root element of the XML document, after the declaration of the namespace type:xmlns:xsi= then:xsi:schemaLocation=URL (of the namespace)then type file.xsdwhere file.xsd is the actual file that contains the schema that defines the namespace used in this XML document.

  • Schemas in Multiple FilesYou can divide a schemas components into various individual files. Each file should be text only and saved with the .xsd extension.Directly after xsd:schema type Where includedfile.xsd is the URL of the schema document that contains the components you wish to include

  • Importing ComponentsTo import components from schemas with different target namespaces:Directly after the xsd:schema element in the schema document into which you are importing components, type to close xsd:import

  • SummaryXML namespaces can be importedCreate and qualify schema definitionsAllows for independent development Compartmentalization of schema filesNomenclature in the schema document declarations is critical, keep referencesValidation is still a -very- tricky issue