Note that the XML Namespaces specification only
standardizes the "qualified name" (qName)
terminology; it doesn't standardize terminology for universal
names.
Because of this, you will also see other terms,
such as "expanded names" (the term used by XPath)
or "namespace-style names" (used to talk about that style of naming).
Since ContentHandler.startElement()
callbacks now have to deal with three different kinds of
name strings, the code can get rather complicated.
Plus, even if you're expecting only universal names,
you'll need to notice when elements or attributes don't
have universal names and use qualified names to work
with them.
Element names are identified in method parameters (the same
as in ContentHandler.endElement()),
while attribute names show up in accessor methods for
Attributes objects.
We'll use the following XML text to illustrate
these different types of names:
<big:animals xmlns="http://www.example.com/dog">
xmlns:big="http://www.example.com/big">
<wolfhound cat='no' big:dog='yes' />
<greyhound big:dog='yes' xmlns=""/>
</big:animals>
SAX2 calls names in XML text "Qualified Names."
These are the same thing as "XML 1.0 names" except that
XML 1.0 names have no restrictions on the use of colons.
When you disable namespace processing in a SAX2 parser, it
will deliver "qualified names" that are really XML 1.0 names,
without those restrictions.
With namespace processing enabled, many qualified names
(including every name with a prefix) will correspond to
a namespace-style name.
Element names without a prefix might not have a
corresponding universal name. Unprefixed attribute
names will never have a universal name.
In those cases, applications must use the qualified name
along with non-namespace context, such as the enclosing
element, to figure out what the name is supposed to mean.
There are no universally accepted policies for such cases. Yes, all that confuses other people as well.
2.6.2.2. Attribute naming
The identifiers for the attribute names are accessed
using Attributes methods such as
getQName(),
getLocalName(),
and getURI() when you
iterate over an element's attributes with a "for" loop.
You can access attribute values directly if you use
either XML 1.0-style names (qName)
or XML Namespace-style names
(namespaceURI
and localName).
SAX2 parsers handle attribute names from the example
text as shown in Table 2-2.
This table shows the "mixed mode" behavior,
described later; in the default SAX2 parser mode,
the xmlns and
xmlns:big attributes won't appear.
You'd have to set the namespace-prefixes
feature flag (as described later in this chapter, in Section 2.6.3, "Namespace Feature Flags") to see these attributes.
Note that according to the namespaces specification there is no such thing as a default namespace
for attribute names, so that namespace declaration
attributes don't go into any namespace.
Table 2-2. Attributes methods to access attribute names
getURI() |
getLocalName() |
getQName() |
empty |
empty |
xmlns |
empty |
empty |
xmlns:big |
empty |
empty |
cat |
http://www.example.com/big |
dog |
empty or
big:dog |
So if you wanted to write some code that ignored
elements without a big:dog attribute
(that is, the URI is
http://www.example.com/big/
and the local name is dog) with value "yes",
it might look like this:
public void startElement (String uri, String local, String qName,
Attributes atts)
throws SAXException
{
String value;
value = atts.getValue ("http://www.example.com/big", "dog");
if (!"yes".equals (value)) {
// arrange to ignore text and elements until this finishes
return;
}
... process the element
}