33
Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Embed Size (px)

Citation preview

Page 1: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 1

Namespace in XML

Cheng-Chia Chen

Page 2: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 2

Abstract

XML namespaces provide a simple method for qualifying element and attribute names used in XML documents by associating them with namespaces identified by URI references.

Page 3: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 3

Table of Contents

0. Quick reviews

1. Motivation and Summary

2. Declaring Namespaces

3. Qualified Names

4. Using Qualified Names

5. Applying Namespaces to Elements and Attributes

5.1 Namespace Scoping

5.2 Namespace Defaulting

5.3 Uniqueness of Attributes

6. Conformance of Documents

Page 4: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 4

0. quick reviews: Motivation

name clashes. Consider an XML language WidgetML which uses XHTML as a sublang

uage for help messages:<widget type="gadget"> <head size="medium"/> <body><subwidget ref="gizmo"/></big> <info> <head><title>Description of gadget</title> </head> <body> <h1>Gadget</h1> A gadget contains a big gizmo </body> </info></widget> Meaning of head and body depends on context!

complicates things for processors and might even cause ambiguities. The solution: multiple namespaces. name = <namespace name, local name>

Page 5: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 5

lexical rules for namespace and local names: require every namespace to be a URIs (Universal Resource

Identifiers) called its [namespace URI]. => name = [namespace URI]:[local part]

http://www.w3.org/TR/xhtml1 : head namespace URI local partsame with attribute names.Notes:

URI = URL URN (extended to IRI (W3c draft) at 1.1). URI here used only for identification - doesn't have to point

at anything. Not all URIs are legal Attribute/element names. (XML name

s do not allow/restrict the use of special characters: (.:_- ok) (/,#,%,… no) use namespace prefix as a proxy for namespace URI.

Qualifying names

Page 6: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 6

Namespace declarations

indirection: namespaces are declared by special namespace attributes (xmlns:* or xmlns) and associated prefixes.

Example:<... xmlns:foo="http://www.w3.org/TR/xhtml1"> ... <foo:head>...</foo:head> ...</...> xmlns:prefix1="URI1" declares a namespace with a prefix: prefix1 a

nd a URI: URI1. Scope rule: lexical

the element containing the declaration and all descendants can be overridden by nested declaration

Both element and attribute names can be qualified with namespaces.

Note: the prefix is just a proxy - applications should use only the URI for identification.

Page 7: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 7

The default namespace

for backward compatibility and simplicity.

declaration: xmlns=“aURI"

Unprefixed element names are assigned the default namespace aURI.

default value: "" (means: disable default namespace)

or xmlns= ""

(means: disable default namespace)

Page 8: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 8

Example<ex xmlns=“http://test.com/” att1=“…”

xmlns:s=“http://test.com/” >

<ex att1=“abc” > … </ex>

<ex xmlns=“” s:att1=“abc”>…</ex> </ex>

Default namespace affects element names only; it does not apply to attributes. in Ex: 1. the 1st and 2nd <ex> belong to the same namespace (http;;

//test.com/) but the 3rd <ex> belongs to no namespace. 2. Global attribute belongs to namespace: http;//test.com. 3. Both <att1> attributes are local in the sense that they bel

ong to the local namespace of ex and are different from s:att1.

4. note the asymmetry of default namspace on elements and attributes

Page 9: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 9

An example: WidgetML with namespaces

<widget xmlns="http://www.widget.org"

xmlns:xhtml="http://www.w3.org/TR/xhtml1"

type="gadget">

<head size="medium"/>

<body><subwidget ref="gizmo"/></big>

<info><xhtml:head>

<xhtml:title>Description of gadget</xhtml:title>

</xhtml:head>

<xhtml:body> <xhtml:h1>Gadget</xhtml:h1>

A gadget contains a big gizmo

</xhtml:body> </info></widget> The main part of WidgetML uses the default namespace which has t

he URI http://www.widget.org; XHTML uses the namespace prefix xhtml which is assigned the URI

http://www.w3.org/TR/xhtml1.

Page 10: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 10

Consequences for other XML technologies

- namespace awareness.XML languages and applications should consider Names

paces as an inherent part of XML. DTD should take namespace into considerations

parsers need an extra processing layer on top to resolve namespa

ce declarations and usesschemas

schemas should be used to declare all possible element/attribute names of a namespace.

namespace URIs could be tied together with schemas …

...

Page 11: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 11

1. Summary

[Definition:] An XML namespace Is a collection of names, used in XML documents as element types and attribute name

s. has a namespace name identified by a URI reference [

RFC2396],

[Definition:] URI references which identify namespaces are considered identical when they are exactly the same character-for-character. E.g.

1. http://a.b.c/~wine/d , 2. http://a.B.c/%7Ewine/d,

3. http://a.b.c/%7ewine/d , 4. d (relative URI deprecated)

All 4 URIs are treated as equal in URI spec, but are seen as different namespace URIs in xml namespace.

Note: Relative URI should not be used.

Page 12: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 12

1. Summary (cont’d)

Names from XML namespaces appear as qualified names (= [ namespace prefix ’:’] local part )

URI references can contain characters not allowed in na

mes, so cannot be used directly as namespace prefixes. Therefore, the namespace prefix serves as a proxy for a URI reference.

Special attributes (xmlns & xmlns:xxx) used to declare the association of the namespace prefix with a URI reference.

Page 13: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 13

2. Declaring Namespaces

A namespace is declared using two types of reserved attributes. 1. xmlns = “default_URI”

2. xmlns:aPrefix = “aURI”

Notes: default_URI and aURI are URI references used to identify a

namespace [name].

Type 1 is used to bind default_URI to all unprefixed elements.

Type 2 is used to bind aURI to all elements/attributes whose common prfix part is aPrfix.

Namespace declarations may occur directly (in document instance) or by default (in DTD).

Page 14: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 14

Example

<?xml versin=“1.0” ?>

<!DOCTYPE x [

<!ATTLIST x xmlns CDATA FIXED “http://test.com”>

]>

<x xmlns:edi='http://ec.org/schema'>

<edi:order >… </edi:order> …

</x>

Notes: Two namespace declarations in <x …>

1. the one occurring indirectly at DTD is a default namespace, while

2. the one inside <x …> declares all elements inside <x>…</x> with the prefix : edi be assocaited with the namepspace : http://ec.org/schema.

Page 15: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 15

Attribute Names for Namespace Declaration

[1] NSAttName ::= PrefixedAttName | DefaultAttName

[2] PrefixedAttName ::= 'xmlns:' NCName

[ NSC: Leading "XML" ]

[3] DefaultAttName ::= 'xmlns'

[4] NCName ::= (Letter | '_') (NCNameChar)*

/* An XML Name, minus the ":" */

[5] NCNameChar ::= Letter | Digit | '.' | '-' | '_'

| CombiningChar | Extender Note: NCName is any XML Name containing no char ‘:’.

<x xmlns:edi='http://ec.org/schema'>

<!-- the "edi” prefix is bound to http://ec.org/schema for the "x" element and contents -->

</x>

Page 16: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 16

2. Declaring Namespaces (cont’d)

Notes on the properties of a namespace name: Must be a URI reference,

should be unique and persistent.

Need not directly usable for retrieval of a schema )

syntax designed with these goals: Uniform Resource Names [RFC2141].

Ordinary URLs can be managed in such a way as to achieve these same goals.

Page 17: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 17

3. Qualified Names

Qualified Name:

[6] QName ::= (Prefix ':')? LocalPart

[7] Prefix ::= NCName

[8] LocalPart ::= NCName

The Prefix provides the namespace prefix part of the qualified name, and must be associated with a namespace URI reference in a namespace declaration.

Note that the prefix functions only as a placeholder for a namespace name.

Applications should use the namespace name, not the prefix,in constructing names whose scope extends beyond the containing document.

Page 18: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 18

4. Using Qualified Names [for element type]

Element types are given as qualified names, as follows: Element Types :

[9] STag ::= '<' QName (S Attribute)* S? '>'

[10] ETag ::= '</' QName S? '>'

[11] EmptyElemTag ::= '<' QName (S Attribute)* S? '/>'

[NSC: Prefix Declared ]Example:

<x xmlns:edi='http://ecommerce.org/schema'>

<!-- the 'price' element's namespace is http://ecommerce.org/schema -->

<edi:price units='Euro'>32.18</edi:price>

</x>

Page 19: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 19

4. Using Qualified Names [for attribute names]

Attributes are either namespace declarations or their names are given as qualified names:

Attribute:

[12] Attribute ::= NSAttName Eq AttValue

| QName Eq AttValue

[ NSC: Prefix Declared ]

Example of a qualified name serving as an attribute name:

<x xmlns:edi='http://ecommerce.org/schema'>

<!-- the 'taxClass' attribute's namespace is http://ecommerce.org/schema -->

<lineItem edi:taxClass="exempt">Baby food</lineItem>

</x>

Page 20: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 20

4. Using Qualified Names [ in DTD]

Element names and attribute types are also given as qualified names in the DTD:

Qualified Names in Declarations

[13] doctypedecl ::= '<!DOCTYPE' S QName (S ExternalID)?

S? ('[' (markupdecl | PEReference | S)* ']' S?)? '>'

[14] elementdecl ::= '<!ELEMENT' S QName S contentspec

S? '>'

[15] cp ::= (QName | choice | seq) ('?' | '*' | '+')?

[16] Mixed ::= '(' S? '#PCDATA' (S? '|' S? QName)* S? ')*'

| '(' S? '#PCDATA' S? ')'

[17] AttlistDecl ::= '<!ATTLIST' S QName AttDef* S? '>'

[18] AttDef ::= S (QName | NSAttName) S AttType S

DefaultDec

Page 21: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 21

5. Applying Namespaces to Elements and Attributes

5.1 Namespace Scoping:The namespace declaration is considered to apply to the

element where it is specified and to all elements within the content of that element, unless overridden by another namespace declaration with the same NSAttName part.

Example:<?xml version="1.0"?>

<!-- all elements here are explicitly in the HTML namespace -->

<html:html xmlns:html='http://www.w3.org/TR/REC-html40'>

<html:head><html:title>Frobnostication</html:title></html:head>

<html:body><html:p>Moved to <html:a href='http://frob.com'>here.</html:a></html:p></html:body>

</html:html>

Page 22: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 22

5.1 Namespace Scoping:

Multiple different namespace prefixes can be declared in the same start tag.

Example:

<?xml version="1.0"?>

<!-- both namespace prefixes are available throughout -->

<bk:book xmlns:bk='urn:loc.gov:books'

xmlns:isbn='urn:ISBN:0-395-36341-6'>

<bk:title>Cheaper by the Dozen</bk:title>

<isbn:number>1568491379</isbn:number>

</bk:book>

Page 23: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 23

5.2 Namespace Defaulting

A default namespace is applied to the element where it is declared (if that element ha

s no namespace prefix), and to all elements with no prefix within the content of th

at element. If the URI reference in a default namespace decla

ration is empty, then unprefixed elements are not considered to be in any namespace.

Note :

Default namespaces do not apply directly to attributes !!!

Page 24: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 24

5.2 Namespace Defaulting: examples

<?xml version="1.0"?><!-- elements are in the HTML namespace, by default --> <html xmlns='http://www.w3.org/TR/REC-html40'> <head><title>Frobnostication</title></head> <body><p>Moved to <a href='http://frob.com'>here</a>.</p></body> </html> <?xml version="1.0"?> <!-- unprefixed element types are from "books" --> <book xmlns='urn:loc.gov:books' xmlns:isbn='urn:ISBN:0-395-36341-6'> <title>Cheaper by the Dozen</title> <isbn:number>1568491379</isbn:number> </book>

Page 25: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 25

5.2 Namespace Defaulting: a larger example

<?xml version="1.0"?> <!-- initially, the default namespace is "book" --> <book xmlns='urn:loc.gov:books' xmlns:isbn='urn:ISBN:0-395-36341-6'> <title>Cheaper by the Dozen</title> <isbn:number>1568491379</isbn:number> <notes> <!-- make HTML the default namespace for some commenta

ry --> <p xmlns='urn:w3-org-ns:HTML'> This is a <i>funny</i> book! </p> </notes> </book>

Page 26: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 26

5.2 Namespace Defaulting:

The default namespace can be set to the empty string to disable default namespace.

<?xml version='1.0'?> <Beers>

<!-- the default namespace is now that of HTML -->

<table xmlns='http://www.w3.org/TR/REC-html40'>

<th><td>Name</td><td>Origin</td><td>Description</td></th>

<tr> <!-- no default namespace inside table cells -->

<td> <brandName xmlns="” >Huntsman</brandName> </td>

<td> <origin xmlns="” >Bath, UK</origin> </td>

<td> <details xmlns="” ><class>Bitter</class><hop>Fuggles</hop>

<pro>Wonderful hop, light alcohol, good summer beer</pro>

<con>Fragile; excessive variance pub to pub</con>

</details> </td> </tr>

</table> </Beers>

Page 27: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 27

5.3 Uniqueness of Attributes

In XML documents conforming to this specification, no tag may contain two attributes which: 1.have identical names, or // for well-formedness 2.have qualified names with the same local part and with pr

efixes which have been bound to namespace names that are identical. // for namespace conformity

Example, each of the following bad start-tags is illegal:

<!-- http://www.w3.org is bound to n1 and n2 -->

<x xmlns:n1="http://www.w3.org"

xmlns:n2="http://www.w3.org" >

<bad a="1" a="2" />

<bad n1:a="1" n2:a="2" />

</x>

Page 28: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 28

5.3 Uniqueness of Attributes (cont’d)

Each of the following is legal, the second is so because the default namespace does not apply to attribute names!:

<!-- http://www.w3.org is bound to n1 and is the default -->

<x xmlns:n1="http://www.w3.org"

xmlns="http://www.w3.org" >

<good a="1" b="2" />

<good a="1" n1:a="2" />

</x>

Page 29: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 29

6. Conformance of Documents

In XML documents which conform to this specification, element types and attribute names must match the product

ion for QName and must satisfy the "Namespace Constraints".

all other tokens [in the document which are required to match the XML production for Name] match this specification's production for NCName.

The effect of conformance is that in such a document: All element types and attribute names contain either zero o

r one colon. No entity names, PI targets, or notation names contain any

colons.

Page 30: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 30

A.2 XML Namespace Partitions (removed in 1.1)

Each XML namespace contains disjoint partitions::

1. The All Element Types Partition combination of the namespace name and the local part

uniquely identifies the element type.

2. The Global Attribute Partition The combination of the namespace name and the attribute

name uniquely identifies the global attribute.

3. The Per-Element-Type (local attribute) Partitions The combination of the attribute name with the element's

type and namespace name uniquely identifies each unqualified attribute.

Page 31: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 31

Examples of namespace partitions

<!-- 1 --> <section xmlns='urn:com:books-r-us'>

<!-- 2 --> <title>Book-Signing Event</title>

<!-- 3 --> <signing>

<!-- 4 --> <author title="Mr" name="Vikram Seth" />

<!-- 5 --> <book title="A Suitable Boy" price="$22.95" />

</signing> </section>

Note: Titles in 4 and 5 belong to different partitions<!-- 1 --> <RESERVATION xmlns:HTML="http://www.w3.org/TR/REC-html40">

<!-- 2 --> <NAME HTML:CLASS="largeSansSerif">Layman, A</NAME>

<!-- 3 --> <SEAT CLASS="Y" HTML:CLASS="largeMonotype">33B</SEAT>

<!-- 4 --> <HTML:A HREF='/cgi-bin/ResStatus'>Check Status</HTML:A>

<!-- 5 --> <DEPARTURE>1997-05-24T07:55:00+1</DEPARTURE>

</RESERVATION>

Notes: 1. two namespace: default and HTML.

Page 32: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 32

Partition of a namespace

Global elements:

html:

body:

a:

table: …

Global attributes:

class:

stylesheet:

Local attributes for a

href

name

Local attributes for

body:

Local attributes for html:

Local attributes for

table:

Page 33: Namespace in XML Transparency No. 1 Namespace in XML Cheng-Chia Chen

Namespace in XML

Transparency No. 33

Links to more information

http://www.w3.org/TR/REC-xml-names

The W3C XML Namespace Recommendation v1.0

http://www.w3.org/TR/xml-names11/

Namespaces in XML 1.1 W3C Candidate Recommendation 18 Decembe

r 2002

IRI draft

Internationalized Resource Identifiers (IRIs). Available at http://www.w

3.org/International/iri-edit/draft-duerst-iri-02.txt

RFC2396

IETF (Internet Engineering Task Force) RFC 2396: Uniform Resource Id

entifiers (URI): Generic Syntax, eds. T. Berners-Lee, R. Fielding, L. Mas

inter. August 1998. Available at ftp://ftp.ietf.org/rfc/rfc2396.txt