XML Namespaces

advertisement
XML Namespaces
Week 3
Web site: http://fog.ccsf.edu/~hyip
XML Namespaces
•
•
•
•
•
Element Name Collision: If you want to combine some of your XML documents
with someone else’s, you find out that they have used some of the same names
for elements that you have.
The solution to this problem is to group the element names from each XML
document into its own space. This format would provide a way to distinguish
elements in one group of XML documents from the other.
This group of element names, is actually called a namespace, and the identifier is
called a namespace name. [Namespace names are written in the form of a URI
(Uniform Resource Identifier)]
xmlns="namespace name"
xmlns="http://hills.ccsf.edu/~hyip/cnit131a/some_name"
A namespace name should be in the form of an URI, or URL format. The URL does
not have to (and typically will not), point to an actual file.
NOTE: uri = is a Uniform Resource Identifier (URI) – a string of characters that
uniquely identifies a resource, which in this case is the declared namespace.
Declaring a Default Namespace
• Once you have designed a namespace name,
you can declare it as the default namespace
for your XML document.
• Or you can define at the element level.
<root xmlns="http://hills.ccsf.edu/~hyip/cnit131a/ns1">
<child1>Data1</child1>
<child2 xmlns="http://hills.ccsf.edu/~hyip/cnit131a/ns2">
<child2_child>Data2</child2_child>
</child2>
</root>
Declaring a Namespace Name Prefix
• Declaring a default namespace for an element applies to all the
element’s children. You can also choose to label specific individual
elements in your document with a namespace, and not affect other
elements. To do so, you can declare a special nickname, or prefix, for
the namespace, and then use that prefix to label the individual
elements specifically.
<root xmlns="http://hills.ccsf.edu/~hyip/cnit131a/ns1"
xmlns:pfx="http://hills.ccsf.edu/~hyip/cnit131a/ns2">
<child1>Data1</child1>
<pfx:child2>
<pfx:child2_child>Data2</pfx:child2_child>
</pfx:child2>
</root>
Labeling Elements with a Namespace
Prefix
• Once you have declared a prefix for a namespace name, you
can use that prefix to label individual elements. In this way,
you can assign these elements to different namespaces in
your XML, without affecting the siblings or children of those
elements.
<apfx:root xmlns:apfx="http://hills.ccsf.edu/~hyip/cnit131a/ns1"
xmlns:pfx="http://hills.ccsf.edu/~hyip/cnit131a/ns2">
<apfx:child1>Data1</apfx:child1>
<pfx:child2>
<pfx:child2_child>Data2</pfx:child2_child>
</pfx:child2>
</apfx:root>
How Namespaces Affect Attributes
•
•
While you could associate an attribute with a specific namespace by prefixing it with the
appropriate prefix. It is almost never necessary. Attributes are already made unique by the element
that contains them.
Although quite uncommon, there are caases where you would need to associate an attribute with a
specific namespace, by pre-pending it with the appropriate prefix. For example, you need to
combine two elements from different namespaces into a single element, and both have an
attribute with the same name. In this case, you would need to differentiate each attribute, since a
single element cannot have multiple attributes with the same name. By prefixing each attribute
with their respective namespace prefix, you would be creating unique attributes, because an XML
processor considers the prefix part of an attribute’s name.
XML 1:
<picture file="filename.jpg" w="200" h="200" />
XML 2:
<picture file="../local/filename.jpg" />
Combined XML:
<picture apfx:file="filename.jpg" pfx:file="../local/filename.jpg" apfx:w="200" apfx:h="200"
Additional Information about URI
•
Understanding URIs
 The URI in the namespace looks like a Web address you might use in creating a link to a Web
site; however, that’s not its purpose. The purpose of a URI is to provide a unique string of
characters identifying a resource.
 One version of a URI is Uniform Resource Locator (URL), which is used to identify the location
of a resource on the Web. (such as web page)
 Note that the URI doesn’t actually have to point to a real site on the Web.
 Another type of URI is a Uniform Resource Name or URN. A URN provides a persistent name
for a resource, independent of that resource’s location. URNs take the form:
 urn:NID:NSS
 NID is the namespace identifier and NSS is a text string specific to that namespace. The NID
indicates how to interpret the text string in the NSS. For example, the following URN uniquely
identifies a book by its ISBN number:
 urn:isbn:0-619-01969-7
 NOTE: URL can be thought of as a unique address of a specific location, a URN can be thought
of as a unique name that is associated with a specific item. Currently URNs are rarely used in
place of URLs, but this may change in the future.
 Using URLs or URNs is widely accepted in declaring namespaces, but nothing prevents you
from using almost any unique string identifier. The main requirement is that a URI be unique
so as not to be confused with the URIs of other namespaces.
Download