16
www.monash.edu.au CSE3201 XML Namespace

Www.monash.edu.au CSE3201 XML Namespace. 2 What is a namespace? An XML namespace is a collection of element type and attribute names

Embed Size (px)

Citation preview

Page 1: Www.monash.edu.au CSE3201 XML Namespace.  2 What is a namespace? An XML namespace is a collection of element type and attribute names

www.monash.edu.au

CSE3201

XML Namespace

Page 2: Www.monash.edu.au CSE3201 XML Namespace.  2 What is a namespace? An XML namespace is a collection of element type and attribute names

www.monash.edu.au

2

What is a namespace?

• An XML namespace is a collection of element type and attribute names.

• It defines a way to distinguish between duplicate element type and attribute names.

Page 3: Www.monash.edu.au CSE3201 XML Namespace.  2 What is a namespace? An XML namespace is a collection of element type and attribute names

www.monash.edu.au

3

Example

<?xml version=“1.0”?><address>

<street>Dandenong Rd </street><city>Caulfield East</city><state>Victoria</state><country>Australia</country><postcode>3145</postcode>

</address>

<?xml version=“1.0”?><server>

<name>aWebServer</name><address>130.194.225.1</address>

<server>

Page 4: Www.monash.edu.au CSE3201 XML Namespace.  2 What is a namespace? An XML namespace is a collection of element type and attribute names

www.monash.edu.au

4

Example

<?xml version=“1.0”?>

<department>

<name>Computer Science</name>

<addr:address xmlns:addr=“http://www.csse.monash.edu.au/xml/address”>

<addr:street>Dandenong Rd </addr:street>

<addr:city>Caulfield East</addr:city>

<addr:state>Victoria</addr:state>

<addr:country>Australia</addr:country>

<addr:postcode>3145</addr:postcode>

</addr:address>

<serv:server xmlns:serv=“http://www.csse.monash.edu.au/xml/servers”>

<serv:name>aWebServer</serv:name>

<serv:address>130.194.225.1</serv:address>

</serv:server>

</department>

Page 5: Www.monash.edu.au CSE3201 XML Namespace.  2 What is a namespace? An XML namespace is a collection of element type and attribute names

www.monash.edu.au

5

The Purpose of Namespace

• Combine fragments from different document without naming conflict.

• Write reusable code modules that can be invoked for specific elements and attributes. Universally unique names guarantee that such modules are invoked only for the correct elements and attributes.

• Define elements and attributes that can be re-used in a number of different schema or instance document without naming conflict

XML Namespace FAQ (http://www.)

Page 6: Www.monash.edu.au CSE3201 XML Namespace.  2 What is a namespace? An XML namespace is a collection of element type and attribute names

www.monash.edu.au

6

Declaring a Namespace

• A namespace is declared using the xmlns attribute. • The value of the attribute is the name of the namespace

being declared.

<addr:address xmlns:addr=“http://www.csse.monash.edu.au/xml/address”>

• Default namespacexmlns=URI or URL

• Target namespacexmlns:prefix=URI or URL

• Namespace can be used in XML instance, DTD, Schema, XSLT.

Page 7: Www.monash.edu.au CSE3201 XML Namespace.  2 What is a namespace? An XML namespace is a collection of element type and attribute names

www.monash.edu.au

7

Namespace in XML Schema

<schema xmlns=“http://www.w3.org/2001/XMLSchema”xlmns:addr=“http://www.csse.monash.edu.au/xml/address”targetNamespace:=“http://www.csse.monash.edu.au/xml/address”elementFormDefault=“unqualified”attributeFormDefault=“unqualified”>

<complexType name=“addressType”> <sequence> <element name=”street” type=“string”/> <element name=“city” type=“string”/> <element name=“state” type=“string”/> <element name=“country” type=“string”/> </sequence>

</complexType>

<element name=“department”> <complexType> <sequence>

<element name=“name” type=“string”/> <element name=“address” type=“addr:addressType”/></sequence>

</complexType></element>

</schema>

Page 8: Www.monash.edu.au CSE3201 XML Namespace.  2 What is a namespace? An XML namespace is a collection of element type and attribute names

www.monash.edu.au

8

XML Instance

<?xml version="1.0"?><adept:department xsi:schemaLocation="http://www.csse.monash.edu.au/xml/address file:namespaceUnqualified.xsd" xmlns:adept="http://www.csse.monash.edu.au/xml/address" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<name>CSSE</name><address>

<street>900 Dandenong Rd</street><city>Caulfield East</city><state>Victoria</state><country>Australia</country>

</address></adept:department>

• The prefix “adept” is applied only to the global element as the schema is declared with unqualified elements and attributes.

Page 9: Www.monash.edu.au CSE3201 XML Namespace.  2 What is a namespace? An XML namespace is a collection of element type and attribute names

www.monash.edu.au

9

Qualified Elements/Attributes

<schema xmlns=“http://www.w3.org/2001/XMLSchema”xlmns:addr=“http://www.csse.monash.edu.au/xml/address”targetNamespace:=“http://www.csse.monash.edu.au/xml/address”elementFormDefault=“qualified”attributeFormDefault=“unqualified”>

<complexType name=“addressType”> <sequence> <element name=”street” type=“string”/> <element name=“city” type=“string”/> <element name=“state” type=“string”/> <element name=“country” type=“string”/> </sequence>

</complexType>

<element name=“department”> <complexType> <sequence>

<element name=“name” type=“string”/> <element name=“address” type=“addr:addressType”/></sequence>

</complexType></element>

</schema>

Page 10: Www.monash.edu.au CSE3201 XML Namespace.  2 What is a namespace? An XML namespace is a collection of element type and attribute names

www.monash.edu.au

10

Qualified – XML Instance

• Explicit used of namespace.

<?xml version="1.0"?>

<adept:department xsi:schemaLocation="http://www.csse.monash.edu.au/xml/address

file:namespaceQualified.xsd"

xmlns:adept="http://www.csse.monash.edu.au/xml/address"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<adept:name>Computer Science</adept:name>

<adept:address>

<adept:street>900 Dandenong Rd</adept:street>

<adept:city>Caulfield East</adept:city>

<adept:state>Victoria</adept:state>

<adept:country>Australia</adept:country>

</adept:address>

</adept:department>

Page 11: Www.monash.edu.au CSE3201 XML Namespace.  2 What is a namespace? An XML namespace is a collection of element type and attribute names

www.monash.edu.au

11

Qualified – XML Instance

• Implicit used of namespace => default namespace

<?xml version="1.0"?><department xsi:schemaLocation="http://www.csse.monash.edu.au/xml/address file:namespaceQualified.xsd" xmlns="http://www.csse.monash.edu.au/xml/address" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<name>CSSE</name><address>

<street>900 Dandenong Rd</street><city>Caulfield East</city><state>Victoria</state><country>Australia</country>

</address></department>

Page 12: Www.monash.edu.au CSE3201 XML Namespace.  2 What is a namespace? An XML namespace is a collection of element type and attribute names

www.monash.edu.au

12

Multiple Schemas

• It is sometime desirable to divide the schema into a number of small parts when a schema is large.

• The division will help:– Maintenance– Readability– Access control

Page 13: Www.monash.edu.au CSE3201 XML Namespace.  2 What is a namespace? An XML namespace is a collection of element type and attribute names

www.monash.edu.au

13

Include

• Two or more schemas from the same namespace can be combined using the include declaration.

• <xs:include schemaLocation=address of the schema>

• All the included parts of the schema have to declared with the same target namespace.

Page 14: Www.monash.edu.au CSE3201 XML Namespace.  2 What is a namespace? An XML namespace is a collection of element type and attribute names

www.monash.edu.au

14

Examples

• Check the bottom.xsd, department.xsd, server.xsd and faculty.xsd.

Page 15: Www.monash.edu.au CSE3201 XML Namespace.  2 What is a namespace? An XML namespace is a collection of element type and attribute names

www.monash.edu.au

15

Import

• The import declaration can be used to combine two or more schemas from different namespaces.

• Only global elements and named Types can be imported.

• <import namespace=namespace to be imported schemaLocation=“schema to be imported”>

Page 16: Www.monash.edu.au CSE3201 XML Namespace.  2 What is a namespace? An XML namespace is a collection of element type and attribute names

www.monash.edu.au

16

Example

• See the newFaculty.xsd