Use namespace to control the scope of XML extensibility
Use namespaces in documents
Use namespaces in DTD
Extensibility up to a certain point is not a good thing
Need some way to avoid conflicts in a distributed environment
Need a way of doing conflict resolution
Example 1:
<?xml version="1.0"?> <stock> <name>IBM</name> <rating>B</rating> <name>Cisco</name> <rating>C</rating> <name>Microsoft</name> <rating>B</rating> <name>Oblix Inc.</name> <rating>A</rating> <name>HP</name> <rating>C</rating> </stock> -----
<?xml version="1.0"?> <mutualfund> <name>AFBA Five Star Equity</name> <rating>4 morning stars</rating> <name>AFBA Five Star USA Glo</name> <rating>4 morning stars</rating> <name>AHA Diversified Equity</name> <rating>5 morning stars</rating> <name>AIM Blue Chip A</name> <rating>3 morning stars</rating> <name>AIM Blue Chip B</name> <rating>3 morning stars</rating> </mutualfund>
-----
The "first name tag" means stock name and the "second name tag" means mutual fund name.
Same problem goes for rating.
Another example
<?xml version="1.0"?> <book> <name> Introduction to XML </name> <rating>4 stars</rating> <name> Battleships </name> <rating>3 stars</rating> <name> My First Storybook </name> <rating>5 stars</rating> <name> Building a Business </name> <rating>2 stars</rating> <name> Computer Keyboard </name> <rating>4 stars</rating> </book>
-----
<?xml version="1.0"?> <book> <name> Introduction to XML </name> <rating>Adult</rating> <name> Battleships </name> <rating>PG13</rating> <name> My First Storybook </name> <rating>G</rating> <name> Building a Business </name> <rating>Adult</rating> <name> Computer Keyboard </name> <rating>G</rating> </book>
(Common problem when two people extend the same document in incompatible ways.)
Example Solved
<?xml version="1.0"?> <book> <name> Introduction to XML </name> <review-rating>4 stars</rating> <age-rating>Adult</rating> <name> Battleships </name> <review-rating>3 stars</rating> <age-rating>PG13</rating> <name> My First Storybook </name> <review-rating>5 stars</rating> <age-rating>G</rating> <name> Building a Business </name> <review-rating>2 stars</rating> <age-rating>Adult</rating> <name> Computer Keyboard </name> <review-rating>4 stars</rating> <age-rating>G</rating> </book>
Namespace is a mechanism to identify (define) XML elements
It maps the name of an element to a global catalog (context): the namespace
Information under: http://www.w3.org/TR/REC-xml
Namespace under: http://www.w3.org/TR/REC-xml-names/
The definition from REC-xml-names-19990114 is:
[Definition:] An XML namespace is a collection of names, identified by a URI reference [RFC2396], which are used in XML documents as element types and attribute names. XML namespaces differ from the "namespaces" conventionally used in computing disciplines in that the XML version has internal structure and is not, mathematically speaking, a set.
Think about namespace as a registry that prevents conflicts of tags and associated attributes and definition.
One possible solution is to build a global registry that requires all the XML document authors to register with the registry before they use any tag. Then, this is too restricted.
<?xml version="1.0"?>
<book xmlns:re="http://www.edatamirror.com/review/1.0"
xmlns:age="http://www.openloop.com/agerating/1.5"
xmlns="http://www.richardsinn.net/ref/xmlns/1.0">
<name>Introduction to XML</name>
<re:rating>4 stars</re:rating>
<age:rating>Adult</age:rating>
<name>Battleships</name>
<re:rating>3 stars</re:rating>
<age:rating>PG13</age:rating>
<name>My First Storybook</name>
<re:rating>5 stars</re:rating>
<age:rating>G</age:rating>
<name>Building a Business</name>
<re:rating>2 stars</re:rating>
<age:rating>Adult</age:rating>
<name>Computer Keyboard</name>
<re:rating>4 stars</re:rating>
<age:rating>G</age:rating>
</book>
A colon separates tag name and prefix.
Prefix is mapped into URI
URI is unique
Domain names are registered to maintain uniqueness
The last line defines the default tag without prefix
XML application compares elements by URI, not the prefix
Warning: URI is case sensitive. So, http://www.openloop.com is not equal to http://www.OPENLOOP.com. They might point to the same document
URL
sinn.openloop.com
.com - Top Level Domain
sinn - Sub Domain
Can register from networksolution.com
Use DNS to control where it goes
Tips: short, good description such as version and description
URN - No address, no location error. Location transparent. ISBN number is a good example
There are scoping in namespace
<?xml version="1.0"?>
<bk:book xmlns:bk="http://www.richardsinn.net/ref/xmlns/1.0">
<bk:name>Introduction to XML</bk:name>
<bk:description>Computer text for college students</bk:description>
<re:rating xmlns:re="http://www.edatamirror.com/review/1.0">4 stars</re:rating>
<age:rating xmlns:age="http://www.openloop.com/agerating/1.5">Adult</age:rating>
<bk:author>Richard Sinn</bk:author>
</bk:book>
bk is valid for the whole doc
re and age is only valid for the element in question
Valid document implies that you have a DTD
Prefix and attributes must be declared in DTD (thus, same for namespace)
<?xml version="1.0" standalone="yes"?> <!DOCTYPE ccf:company [ <!ELEMENT ccf:company (ccf:name,ccf:description)+> <!ATTLIST ccf:company xmlns:ccf CDATA #REQUIRED> <!ELEMENT ccf:name (#PCDATA)> <!ELEMENT ccf:description (#PCDATA)> ]> <ccf:company xmlns:ccf="http://www.openloop.com/ns/ref/2.5"> <ccf:name>IBM</ccf:name> <ccf:description>Computer hardware and software</ccf:description> <ccf:name>Cisco</ccf:name> <ccf:description>Networking</ccf:description> <ccf:name>Microsoft</ccf:name> <ccf:description>Software</ccf:description> <ccf:name>Oblix Inc.</ccf:name> <ccf:description>Web Security Infrastructure</ccf:description> <ccf:name>HP</ccf:name> <ccf:description>Computer Hardware</ccf:description> </ccf:company>
Could declare ns inline in DTD as
<!ATTLIST ccf:company xmlns:ccf CDATA #FIXED "http://www.openloop.com/ns/ref/2.5">
No recommended as parser could ignore it. Do it in document is the best way to go
Application of namespace
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/TR/REC-html40"> <xsl:output method="html"/> <xsl:template match="/"> <html> <head> <title>Demo for XSL with Namespaces</title> </head> <body> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="h1"> <p><strong><xsl:apply-templates/></strong></p> </xsl:template> <xsl:template match="p"> <p><xsl:apply-templates/></p> </xsl:template> </xsl:stylesheet>
-----
This is a very general way of using namespace. We will cover more in our XSL section
Copyright 1996-2001 OpenLoop Computing. All rights reserved.