Set - 4

Question 26 :

How do I use XML namespaces with SAX 1.0?

Answer :

The easiest way to use XML namespaces with SAX 1.0 is to use John Cowan's Namespace SAX Filter (see http://www.ccil.org/~cowan/XML). This is a SAX filter that keeps track of XML namespace declarations, parses qualified names, and returns element type and attribute names as universal names in the form:
URI^local-name
For example:
http://www.google.com/ito/sales^SalesOrder
Your application can then base its processing on these longer names. For example, the code:

public void startElement(String elementName, AttributeList attrs)
throws SAXException{
	...
	if (elementName.equals("SalesOrder")){
		// Add new database record.
	}
	...
}

might become:
public void startElement(String elementName, AttributeList attrs)
throws SAXException
{
	...
	if (elementName.equals("http://www.google.com/sales^SalesOrder")){
		// Add new database record.
	}
	...
}

or:
public void startElement(String elementName, AttributeList attrs)
throws SAXException
{
	...
	// getURI() and getLocalName() are utility functions
	// to parse universal names.
	if (getURI(elementName).equals("http://www.foo.com/ito/sales")){
		if (getLocalName(elementName).equals("SalesOrder")){
			// Add new database record.
		}
	}
	...
}

If you do not want to use the Namespace SAX Filter, then you will need to do the following in addition to identifying element types and attributes by their universal names:
* In startElement, scan the attributes for XML namespace declarations before doing any other processing. You will need to maintain a table of current prefix-to-URI mappings (including a null prefix for the default XML namespace).
* In startElement and endElement, check whether the element type name includes a prefix. If so, use your mappings to map this prefix to a URI. Depending on how your software works, you might also check if the local part of the qualified name includes any colons, which are illegal.
* In startElement, check whether attribute names include a prefix. If so, process as in the previous point.


Question 27 :

How do I use XML namespaces with DOM level 2?

Answer :

// Check the local name.
// getNodeName() is a DOM level 1 method.
if (elementNode.getNodeName().equals("SalesOrder"))
{
    // Add new database record.
}

might become the following namespace-aware code:
// Check the XML namespace name (URI).
// getNamespaceURI() is a DOM level 2 method.

String SALES_NS = "http://www.foo.com/ito/sales";
if (elementNode.getNamespaceURI().equals(SALES_NS)){
    // Check the local name.
    // getLocalName() is a DOM level 2 method.
    if (elementNode.getLocalName().equals("SalesOrder")){
        // Add new database record.
    }
}

Note that, unlike SAX 2.0, DOM level 2 treats xmlns attributes as normal attributes.


Question 28 :

Where can qualified names appear?

Answer :

Qualified names can appear anywhere an element type or attribute name can appear: in start and end tags, as the document element type, and in element type and attribute declarations in the DTD. For example:

<!DOCTYPE foo:A [
<!ELEMENT foo:A (foo:B)>
<!ATTLIST foo:A
foo:C CDATA #IMPLIED>
<!ELEMENT foo:B (#PCDATA)>
]>
<foo:A xmlns:foo="http://www.foo.org/" foo:C="bar">
<foo:B>abcd
<foo:A>

Qualified names cannot appear as entity names, notation names, or processing instruction targets.


Question 29 :

Can qualified names be used in attribute values?

Answer :

Yes, but they have no special significance. That is, they are not necessarily recognized as such and mapped to universal names. For example, the value of the C attribute in the following is the string "foo:D", not the universal name {http://www.foo.org/}D.
<foo:A xmlns:foo="http://www.foo.org/">
<foo:B C="foo:D"/>
<foo:A>

In spite of this, there is nothing to stop an application from recognizing a qualified name in an attribute value and processing it as such. This is being done in various technologies today. For example, in the following XML Schemas definition, the attribute value xsd:string identifies the type of the foo attribute as the universal name {http://www.w3.org/1999/XMLSchema}string.

<xsd:attribute name="foo" type="xsd:string" />

There are two potential problems with this. First, the application must be able to retrieve the prefix mappings currently in effect. Fortunately, both SAX 2.0 and DOM level 2 support this capability. Second, any general purpose transformation tool, such as one that writes an XML document in canonical form and changes namespace prefixes in the process, will not recognize qualified names in attribute values and therefore not transform them correctly. Although this may be solved in the future by the introduction of the QName (qualified name) data type in XML Schemas, it is a problem today.


Question 30 :

How are qualified names mapped to names in XML namespaces?

Answer :

If a qualified name in the body of a document (as opposed to the DTD) includes a prefix, then that prefix is used to map the local part of the qualified name to a universal name -- that is, a name in an XML namespace. For example, in the following, the prefix foo is used to map the local names A, B, and C to names in the http://www.foo.org/ namespace: 
<?xml version="1.0" ?>
<foo:A xmlns:foo="http://www.foo.org/" foo:C="bar">
<foo:B>abcd
<foo:A>

If a qualified name in the body of a document does not include a prefix and a default XML namespace is in scope then one of two things happens. If the name is used as an element tag, it is mapped to a name in the default XML namespace. If it is used as an attribute name, it is not in any XML namespace. For example, in the following, A and B are in the http://www.foo.org/ namespace and C is not in any XML namespace:

<?xml version="1.0" ?>
<A xmlns="http://www.foo.org/" C="bar">
<B>abcd</B>
<A>

If a qualified name in the body of a document does not include a prefix and no default XML namespace is in scope, then that name is not in any XML namespace. For example, in the following, A, B, and C are not in any XML namespace: 
<?xml version="1.0" ?>
<A C="bar">
<B>abcd</B>
<A>

Qualified names in the DTD are never mapped to names in an XML namespace because they are never in the scope of an XML namespace declaration.