1 compound documents: combining xml vocabularies

11
1 Compound Documents: Combining XML Vocabularies

Upload: loraine-carr

Post on 18-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Compound Documents: Combining XML Vocabularies

1

Compound Documents: Combining XML Vocabularies

Page 2: 1 Compound Documents: Combining XML Vocabularies

Problems with Name Collision

2

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.

Page 3: 1 Compound Documents: Combining XML Vocabularies

3

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:1. Declare the namespace2. Identify which elements in the document belong in the

namespace

Page 4: 1 Compound Documents: Combining XML Vocabularies

4

1. Declaring a Namespace

• Declaring a namespace consists of adding the attribute xmlns to 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 mod and the URI

http://jacksonelect.com/models

Page 5: 1 Compound Documents: Combining XML Vocabularies

5

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:model xmlns: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>

Page 6: 1 Compound Documents: Combining XML Vocabularies

6

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>

Page 7: 1 Compound Documents: Combining XML Vocabularies

7

Adding Namespace Prefixes to Attributes

• Attributes, like elements, can become qualified by adding the namespace prefix to the attribute name:

<prefix:element prefix:attribute="value">…</prefix:element>

• Example: <mod:model xmlns: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.

Page 8: 1 Compound Documents: Combining XML Vocabularies

8

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}

Page 9: 1 Compound Documents: Combining XML Vocabularies

9

Combining Standard Vocabularies

• Standard vocabularies may be combined within a single XML document. Some well-known XML vocabularies are:

Page 10: 1 Compound Documents: Combining XML Vocabularies

Example 1: Combining XHTML and MathML

Page 11: 1 Compound Documents: Combining XML Vocabularies

11

Example 2: Converting HTML to XHTML