One
of the greatest advantages of XML is the ability to create custom-named
tags. This poses a real challenge when you want to share information
from different sources and merge it into a common document. Often this
results in name collisioin, where elements from two or more documents
share the same name or the documents are created by two or more authors
that use the same tag names for different elements. One solution to
this problem is to declare a namespace in the document prolog as follows:
<?xml:namespace ns="URI" prefix="prefix"?>
Example
<?xml:namespace ns="http://uhosp/patents/ns"
prefix="pat"?>
URI stands for Universal Resource Identifier. The URI looks like a
URL (or Web address), but it's important not to confuse it this way.
The URI is a text string that may refer to a physical or abstract resource.
A URL is a phyiscal type of URI that identifies the location of a resource,
however, a URI specified in a namespace may refer to an abstract refer
in the form of an identifier. You can use any unique text string as
a namespace identifier, however, a URI works especially well when you
want to share the XML document on the Web, which makes the document
unique to a particular domain.
A URN, or Universal Resource Name has also been proposed as an alternative
URI. A URN is a persistent resource identifier. In this case, the user
only needs to know the name of the resource, which can be retrieved
from multiple search sites no matter where it is located. A good example
of a URN is a book's ISBN. This is an area under development, but may
very well be the way of the future.
The prefix in a URI is the string of letters that associates each element
or attribute in the document with the declared namespace. Once a namespace
is declared in the prolog, you insert the prefix into all elements that
belong to the namespace:
<pat:Patients>
<pat:Patient>
<pat:Name>Joe Smith</pat:Name>
</pat:Patient>
</pat:Patients>
Declare a Namespace as Element Attribute
It is common to declare namespaces as element attributes:
<pat:Patients xmlns:pat="URI">
<Patients>
<Patient>
<Name>Joe Smith</Name>
</Patient>
</Patients>
To avoid using prefixes, you can declare a default namespace. All child
elements inherit the parent's namespace.
<Patients xmlns="URI">
Use Namespaces with Attributes
You can qualify element attributes with namespaces as well. No element
can contain two attributes with the same name, whether or not qualified
and no element may contain two qualified alltribute names with the same
local part, pointing to two identical namespaces, even if the prefixes
are different.
prefix:attribute="value"
Namespaces and DTDs
Documents containing namespaces must be valid, so the namespace must
also be included in applicable DTDs. There are issues, however, that
prevent namespaces and DTDs from working well together. Since there
is no way to associate a specific DTD with a namespace, authors must
create a new DTD each time they want to combine two or more documents.
Work
with Namespaces
Download the lab file.
Open UHosptxt.xml, rename it UHosp.xml and edit the code:
<?xml version="1.0" ?>
<!DOCTYPE Report SYSTEM "UHDTD.dtd">
<Report>
<pat:Patients xmlns:pat="http://uhosp/patients/ns">
...
</pat:Patients>
<std:Study xlmn:std="http"//uhosp/studies/ns">
<std:Name>
<std:Title>Tamoxifen Breast Cancer
Study</std:Title>
<std:Subtitle>Randomized Phase 3
Clinical Trial</std:Subtitle>
</std:Name>
<std:PI>Dr. Diane West</std:PI>
</std:Study>
Save the file.
Open UHDTDtxt.dtd, rename it UHDTD.dtd and edit the code:
<!ELEMENT Report (pat:Patients,
std:Study)>
<!ELEMENT pat:Patients (Patient+)>
<!ELEMENT Patient (Name, ID, DOB, Age, Stage, Comment*, Performance)>
<!ELEMENT Name (#PCDATA)>
<!ELEMENT ID (#PCDATA)>
<!ELEMENT DOB (#PCDATA)>
<!ELEMENT Age (#PCDATA)>
<!ELEMENT Stage (#PCDATA)>
<!ELEMENT Comment (#PCDATA)>
<!ELEMENT Performance (#PCDATA)>
<!ATTLIST Performance Scale (Karnofsky | Bell) #REQUIRED>
<!ELEMENT std:Study (std:Name,
std:PI)>
<!ELEMENT std:Name (std:Title,
std:Subtitle)>
<!ELEMENT std:Title (#PCDATA)>
<!ELEMENT std:Subtitle (#PCDATA)>
<!ELEMENT std:PI (#PCDATA)>
<!ATTLIST pat:Patients xmlns:pat CDAT #FIXED "http://uhosp/patients/ns">
<!ATTLIST std:Study xmlns:std CDATA #FIXED "htpp://uhosp/studies/ns">
Save the file. Open UHDTD.dtd in the browser.