Download ppt - Using Namespaces in XML

Transcript
Page 1: Using Namespaces in XML

Using Namespaces in XML

COIN 78Lecture 7

Page 2: Using Namespaces in XML

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• URL may point to a DTD or schema, but

that is not required

Page 3: Using Namespaces in XML

Sample code

http://www.domain.com/ns/rivers/1.0• opening protocol• your domain name• optional namespaces directory

information• short description of the namespace• optional version number

Page 4: Using Namespaces in XML

Declaring Default Namespaces

• To 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

Page 5: Using Namespaces in XML

Sample code - in No Namespace

<endangered_species><animal><name language=“English”>Giant River

Otter</name> …<source sectionid=“122” newspaperid=“21”

contentid=“630”xmlns=“http://www.cookwood.com/ns/

end_species1” /><picture filename=“otter.jpg” x=“200” y=“197” />

Page 6: Using Namespaces in XML

Sample Code

<endangered_speciesxmlns=“http://www.cookwood.com/ns/

end_species1”>…..</endangered_species>• With the namespace declaration in the root

element, all of the elements in the document are associated with the http://www.cookwood.com/ns/end_species1 namespace

Page 7: Using Namespaces in XML

Namespaces for Individual Elements

• To declare a prefix for a namespace name:

• In the document’s root element, type xmlns:prefix where prefix will be the nickname for this namespace

• Then type =“URL” where URL is the name of the namespace to which the prefix will refer

Page 8: Using Namespaces in XML

Sample Code

<endangered_species xmlns=“http://www.cookwood.com/ns/endspecies1”

xmlns:rivers=“http://www.cookwood.com/ns/rivers1” …

</endangered_species>• xmlns:rivers points to rivers1 as the

namespace for the named prefix rivers

Page 9: Using Namespaces in XML

How Namespaces Affect Attributes

• You could associate an attribute with a specific namespace, however...

• By prefixing it with appropriate prefix, it’s almost never necessary, since...

• Attributes are already made unique by the element that contains them...

Page 10: Using Namespaces in XML

Sample Code

<source sectionid=“122” newspaperid=“21” contentid=“630” />

<orange sectionid=“3a” />• “sectionid” does not require a separate

name space• The two elements that contain it, source

and orange, have no overlap

Page 11: Using Namespaces in XML

Namespaces, Schemas, and Valid Documents

• An element is more than its name alone• XML processors consider the element to be

called prefix:element• If you want to validate the document against a

schema, you must declare each prefix element in the schema

• Additionally, it is necessary to declare the attribute with which you declare the namespace, either xmlns or xmlns:prefix

Page 12: Using Namespaces in XML

Summary

• Namespaces create super labels that can distinguish elements of similar names when two or more documents are combined.

• The super label is called the namespace name• The 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.

Page 13: Using Namespaces in XML

Namespaces, Schemas, and Validation

COIN 78Lecture 8

Page 14: Using Namespaces in XML

Schemas and Namespaces

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

Page 15: Using Namespaces in XML

Populating a Namespace

• You 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=“URL”• Where URL is the namespace with which you

want to associate the components defined in this schema: called “populating a namespace”

Page 16: Using Namespaces in XML

Adding All Locally Declared Elements

• To add all the locally declared elements elements to the target namespace:

• In the xsd:schema element, type elementFormDefault=“qualified”

Page 17: Using Namespaces in XML

Adding Particular Locally Declared Elements

• To add a particular locally declared declared element to the target namespace

• In the element’s declaration, type form=“qualified”

• To keep a particular locally declared element from being associated with a target namespace (despite the default)

• In the element's declaration type: form=“unqualified”

Page 18: Using Namespaces in XML

Referencing Components with Namespaces

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

Page 19: Using Namespaces in XML

The Schema of Schemas as the Default

• You can declare the schema of schemas as the default namespace for a schema

• Type <schema (not xsd:schema)• Type

xmlns=“http://www.w3.org/2001/XMLSchema• Type targetNamespace=“URL”• Type xmlns:prefix=“URL” (repeat for each name)• Type > to complete the schema tag

Page 20: Using Namespaces in XML

Namespaces and Validating XML

• You 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”) or

• Declaring a namespace with a prefix (xmlns:prefix=“URL”). It is sometimes necessary to indicate where the schema

Page 21: Using Namespaces in XML

Indicating Where a Schema Is

• In the root element of the XML document, after the declaration of the namespace type:

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” then:

xsi:schemaLocation=“URL (of the namespace)then type file.xsd”where file.xsd is the actual file that contains the

schema that defines the namespace used in this XML document.

Page 22: Using Namespaces in XML

Schemas in Multiple Files

• You can divide a schema’s components into various individual files. Each file should be text only and saved with the .xsd extension.

• Directly after xsd:schema type <xsd:include schemaLocation=“included-file.xsd” />

• Where includedfile.xsd is the URL of the schema document that contains the components you wish to include

Page 23: Using Namespaces in XML

Importing Components

• To 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 <xsd:import

• Then type namespace=”URL”• Then, if needed, xsi:schemaLocation=“URL”• Then type file.xsd then /> to close xsd:import

Page 24: Using Namespaces in XML

Summary

• XML namespaces can be imported• Create and “qualify” schema definitions• Allows for independent development • Compartmentalization of schema files• Nomenclature in the schema document

declarations is critical, keep references• Validation is still a -very- tricky issue


Recommended