When a namespace prefix is declared, it remains in scope for: Attributes of the element where it is declared2. Child elements (and their attributes) of the element where it is declared.[r]
Trang 1Welcome to:
XML Namespaces
Trang 2Unit Objectives
After completing this unit, you should able to:
Describe the reasons for using namespaces
Describe the syntax used in namespaces
Define and illustrate an example using namespaces
Define problems with namespaces
List and define the best practices to use when using namespacesDescribe the status of namespaces in the industry
Trang 3Consider the following XML document:
How does an application know that:
The first occurrence of title is a book title
The second occurrence of title is a person's title
Need a way to eliminate the ambiguity for the purpose of processing
Problem: Element and Attribute Names Can Be Ambiguous
Trang 4No industry is an island, industries interact: who decides?
Naming standards down to the element/attribute level are too brittle
Use verbose element names, that is, bookTitle, courtesyTitle
Problem: naming becomes fundamentally difficult, there is no way to know if a name is already in use, further, the data and/or its model may not belong to the consuming application
Trang 5Namespaces: The Big Idea
In concept, each element name and attribute name could be
expressed as: URI+name, for example, <title> might become:
<http://www.library.com/books:title>
There are two problems with this format:
1 It is not well-formed XML under the 1.0 specification
2 It is a lot of typing
If it were possible to create a synonym for the URI and replace occurrences of the URI with that synonym, the amount of typing would be reduced and, if handled correctly, the result would be compatible with XML 1.0
For example, specify books="http://www.library.com/books", and code the element as <books:title>
This concept forms the basis of the XML Namespace specification.
These URI qualifiers are called
Namespaces
Trang 6XML Namespaces
The Namespace specification refers to these two-part names
as Qualified Names or QNames
For the purposes of XML namespaces, URIs are considered
identical when they match character for character If URIs are
different, they represent different Namespaces
Note: There is no network lookup associated with the use of URIs
in this specification, it is a lexical convention only
URIs are not checked by the processor to ensure they exist.
The Namespace specification deals with the mechanics of
associating a URI qualifier (aka namespace) with element and
attribute names to create two-part names that are unique and free of ambiguity
Trang 7Qualified Names (QNames)
QNames are used in place of element and attribute names.QNames have a prefix and a local part - they look like this:
Trang 8The syntax of a namespace declaration is:
< prefix : elementName xmlns: prefix = 'URI' />
The following example declares the namespace
http://www.library.com/books, assigns it a prefix of 'books'
and identifies the book element as a member of that namespace.
Trang 10Unless the prefix is redefined on a nested element.
QNames are still required, the namespace is not assumed
Applying this technique, the previous example becomes:
Trang 11The default may be respecified for each element scope
Nesting is respected, that is, respecification does not influence the outerscope containing the nested elements
Default namespaces don't apply to attributes Attributes have no namespace unless their names are qualified
Trang 12Example - Default Namespaces
Trang 13Documents with Multiple Namespaces
Document with three namespaces:
< book
xmlns= 'http://www.library.com/books'
xmlns: amazon='http://www.amazon.com/products' > < title >Tom Sawyer</ title >
Trang 14Elements with No Namespace
What happens to the previous example with no default
The xmlns="" syntax resets the default namespace for the scope in
which it occurs The <isbn> element is not in a namespace.
There is no default null namespace
Trang 15Attributes and Namespaces
Attributes are not affected by a default namespace declaration.Attributes on a single element must be unique
<invalid att="1" att="2" />
<invalid ns1:att ="1" ns2:att ="2" />
Trang 16How does an XML parser deal with namespaces?
Needs the right API
SAX2
DOM Level 2
The parser simply reports the prefix, localName, and URI
associated with the element or attribute
It's up to your application to decide what to do
There are no validation rules associated with Namespaces - it depends on XMLSchema, DTD, or whatever grammar description language you are using
Namespace Processing
Trang 17Example: Use of Namespaces
Composition of a particular airplane in an airline fleet:
<united:airplanes xmlns="http://www.wingco.com"
xmlns:united="http://www.ual.com"
xmlns:boeing="http://www.boeing.com"
xmlns:pratt="http://www.prattandwhitney.com" xmlns:goodyear="http://www.goodyear.com" xmlns:airbus="http://www.airbus.com"
Trang 18Problems with Namespaces
Namespace recommendation after XML 1.0
DTDs don't integrate well
Must use QNames as element names in DTDs (remember, ":" is legal in an element name)
If the prefix changes, the DTD must also change
Testing the equality of namespaces is not handled by the parser.You have to test equality of URIs, you cannot just test equality of prefixes
Trang 19Best Practices
When to use namespaces
When the data requires uniqueness for application processing.When the need to combine a schema [TBD] with other grammars
is necessary
Performance implications
Namespace processing may slow down the parser and/or
increases memory use
Don't use relative URIs for namespace identifiers
Pick the default namespace carefully
Don't declare more than one prefix for a namespace URI
Be careful with attributes when using namespaces
Collect the namespace declarations in one place, preferable near the top of the document
Trang 22Checkpoint Questions
1 Which is true of XML namespaces?
(Select all that apply)
a They are stored in an Internet-based registry
b They are associated with URIs
c They are integrated with DTDs
d They are integrated with XML Schema
2 An XML namespace prefix (Select all that apply):
a Links to a schema definition
b Is scoped to the element where it is defined
c Is short hand for a URI
d Can stand for more than one namespace
3 Default namespaces apply to:
a Elements
b Attributes
c Elements and attributes
d Neither elements nor attributes
Trang 23Unit Summary
Having completed this unit, you should understand:The reasons for using namespaces
The syntax used in namespaces
The use of default namespaces
The interaction between namespaces and attributesProblems with namespaces
Best practices regarding namespaces
Status of namespace technology