News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!

Main Menu

XML Interview Questions and Answers - Part 14

Started by VelMurugan, Apr 25, 2008, 06:09 PM

Previous topic - Next topic

VelMurugan

XML Interview Questions and Answers

Where can qualified names appear?


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.itacumens.com/" itacumens:C="bar">
<foo:B>abcd
<foo:A>

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

Can qualified names be used in attribute values?


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 "itacumens:D", not the universal name {http://www.itacumens.com/}D.
<foo:A xmlns:foo="http://www.itacumens.com/">
<foo:B C="itacumens: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="itacumens" 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.

How are qualified names mapped to names in XML namespaces?


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.itacumens.org/ namespace:
<?xml version="1.0" ?>
<itacumens:A xmlns:itacumens="http://www.itacumens.com/" itacumens:C="bar">
<itacumens:B>abcd
<itacumens: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.itacumens.com/ namespace and C is not in any XML namespace:

<?xml version="1.0" ?>
<A xmlns="http://www.itacumens.com/" 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.

How are universal names represented?


There is no standard way to represent a universal name. However, three representations are common.
The first representation keeps the XML namespace name (URI) and the local name separate. For example, many DOM level 1 implementations have different methods for returning the XML namespace name (URI) and the local name of an element or attribute node.
The second representation concatenates the namespace name (URI) and the local name with caret (^). The result is a universally unique name, since carets are not allowed in URIs or local names. This is the method used by John Cowan's Namespace SAX Filter . For example, the universal name that has the URI http://www.google.org/to/servers and the local name Address would be represented as:
http://www.itacumens.com/  ^Address
The third representation places the XML namespace name (URI) in braces and concatenates this with the local name. This notation is suggested only for documentation and I am aware of no code that uses it. For example, the above name would be represented as:
{http://www.itacumens.com/ito/servers}Address

Are universal names universally unique?


No, but it is reasonable to assume they are.
Universal element type and attribute names are not guaranteed to be universally unique -- that is, unique within the space of all XML documents -- because it is possible for two different people, each defining their own XML namespace, to use the same URI and the same element type or attribute name. However, this occurs only if:

* One or both people use a URI that is not under their control, such as somebody outside Netscape using the URI http://www.itacumens.com/, or
* Both people have control over a URI and both use it.

The first case means somebody is cheating when assigning URIs (a process governed by trust) and the second case means that two people within an organization are not paying attention to each other's work. For widely published element type and attribute names, neither case is very likely. Thus, it is reasonable to assume that universal names are universally unique. (Since both cases are possible, applications that present security risks should be careful about assuming that universal names are universally unique.)
For information about the ability of universal names to uniquely identify element types and attributes (as opposed to the names themselves being unique).

What is an XML namespace prefix?

An XML namespace prefix is a prefix used to specify that a local element type or attribute name is in a particular XML namespace. For example, in the following, the serv prefix specifies that the Address element type name is in the http://www.itacumens.com/ito/addresses namespace:
<serv:Addresses xmlns:serv="[url]http://www.itacumens.comito/addresses">[/url%]

What characters are allowed in an XML namespace prefix?


The prefix can contain any character that is allowed in the Name [5] production in XML 1.0 except a colon.

Can I use the same prefix for more than one XML namespace?


Yes.

What happens if there is no prefix on an element type name?

If a default XML namespace declaration is in scope, then the element type name is in the default XML namespace. Otherwise, the element type name is not in any XML namespace.

What does the URI used as an XML namespace name point to?


The URI used as an XML namespace name is simply an identifier. It is not guaranteed to point to anything and, in general, it is a bad idea to assume that it does. This point causes a lot of confusion, so we'll repeat it here:
URIs USED AS XML NAMESPACE NAMES ARE JUST IDENTIFIERS. THEY ARE NOT GUARANTEED TO POINT TO ANYTHING.

While this might be confusing when URLs are used as namespace names, it is obvious when other types of URIs are used as namespace names. For example, the following namespace declaration uses an ISBN URN:
xmlns:xbe="urn:ISBN:0-7897-2504-5"
and the following namespace declaration uses a UUID URN:
xmlns:foo="urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6"
Clearly, neither namespace name points to anything on the Web.

NOTE: Namespace URIs that are URLs may point to RDDL documents, although this does not appear to be widely implemented. For details, see the next question.

NOTE: An early version of the W3C's XML Schemas used namespace URIs to point to an XML Schema document containing the definitions of the element types and attributes named in the namespace. However, this proved very controversial and the idea has been withdrawn. 

What is an XML namespace name?

An XML namespace name is a URI that uniquely identifies the namespace. URIs are used because they are widely understood and well documented. Because people may only allocate URIs under their control, it is easy to ensure that no two XML namespaces are identified by the same URI.

Can I resolve the URI used as an XML namespace name?


Yes.

Can I use a relative URI as a namespace name?

Yes. However, such usage is deprecated, so you should never do it.

What is XPointer?

XPointer is set of recommendations developed by the W3C. The core recommendations are the XPointer Framework which provides an extensible addressing behavior for fragment identifiers in XML media types.
XPointer gains its extensibility through the XPointer Framework, which identifies the syntax and processing architecture for XPointer expressions and through an extensible set of XPointer addressing schemes. These schemes, e.g., element() or xpointer(), are actually QNames. The xmlns() scheme makes it possible for an XPointer to declare namespace bindings and thereby use third-party schemes as readily as W3C defined XPointer schemes.

How do I install the XPointer processor?


Download the latest "cweb-xpointer" release from SourceForge. This project uses Apache Maven and Java 1.4+, so you will need to install those as well. Normally you will also want to download one of the XPointer Framework integrations, such as the xpointer+dom4j or the xpointer+jdom package. These "integration packages" provide support for a specific XML Document model.

The project dependencies are explicitly declared in the Maven POM. This means that Maven can automagically download the required releases of dependent JARs.

There are several release artifacts. The "uberjar" release provides an executable command line utility (see below) and bundles all dependancies (except for Java itself). If you want to integrate into an existing application, then you should use the cweb-xpointer JAR and also download copies of its dependencies. If you are using a Maven project, then this is all very, very easy.