XML Interview Questions
XML Interview Questions
1.Donate money.
Please go through the link to donate
http://www.downloadmela.com/donate.html
which identifies the type of document and says where the Document Type Description (DTD) is stored;
The Prolog is followed by the document instance:
1. A root element, which is the outermost (top level) element (start-tag plus end-tag) which encloses
everything else: in the examples below the root elements are conversation and titlepage;
2. A structured mix of descriptive or prescriptive elements enclosing the character data content (text),
and optionally any attributes (‘name=value’ pairs) inside some start-tags.
XML documents can be very simple, with straightforward nested markup of your own design:
<?xml version="1.0" standalone="yes"?>
<conversation><br>
<greeting>Hello, world!</greeting>
<response>Stop the planet, I want to get
off!</response>
</conversation>
Or they can be more complicated, with a Schema or question C.11, Document Type Description (DTD)
or internal subset (local DTD changes in [square brackets]), and an arbitrarily complex nested
structure:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE titlepage
SYSTEM "http://www.google.bar/dtds/typo.dtd"
[<!ENTITY % active.links "INCLUDE">]>
<titlepage id="BG12273624">
<white-space type="vertical" amount="36"/>
<title font="Baskerville" alignment="centered"
size="24/30">Hello, world!</title>
<white-space type="vertical" amount="12"/>
<!-- In some copies the following
decoration is hand-colored, presumably
by the author -->
<image location="http://www.google.bar/fleuron.eps"
type="URI" alignment="centered"/>
<white-space type="vertical" amount="24"/>
<author font="Baskerville" size="18/22"
style="italic">Vitam capias</author>
Or they can be anywhere between: a lot will depend on how you want to define your document type (or
whose you use) and what it will be used for. Database-generated or program-generated XML
documents used in e-commerce is usually unformatted (not for human reading) and may use very long
names or values, with multiple redundancy and sometimes no character data content at all, just values
in attributes:
<?xml version="1.0"?> <ORDER-UPDATE AUTHMD5="4baf7d7cff5faa3ce67acf66ccda8248"
ORDER-UPDATE-ISSUE="193E22C2-EAF3-11D9-9736-CAFC705A30B3"
ORDER-UPDATE-DATE="2005-07-01T15:34:22.46" ORDER-UPDATE-
DESTINATION="6B197E02-EAF3-11D9-85D5-997710D9978F"
ORDER-UPDATE-ORDERNO="8316ADEA-EAF3-11D9-9955-D289ECBC99F3">
<ORDER-UPDATE-DELTA-MODIFICATION-DETAIL ORDER-UPDATE-ID="BAC352437484">
<ORDER-UPDATE-DELTA-MODIFICATION-VALUE ORDER-UPDATE-ITEM="56"
ORDER-UPDATE-QUANTITY="2000"/>
</ORDER-UPDATE-DELTA-MODIFICATION-DETAIL>
</ORDER-UPDATE>
How does XML handle white-space in my documents?
All white-space, including linebreaks, TAB characters, and normal spaces, even between ‘structural’
elements where no text can ever appear, is passed by the parser unchanged to the application (browser,
formatter, viewer, converter, etc), identifying the context in which the white-space was found (element
content, data content, or mixed content, if this information is available to the parser, eg from a DTD or
Schema). This means it is the application's responsibility to decide what to do with such space, not the
parser's:
* insignificant white-space between structural elements (space which occurs where only element
content is allowed, ie between other elements, where text data never occurs) will get passed to the
application (in SGML this white-space gets suppressed, which is why you can put all that extra space in
HTML documents and not worry about it)
* significant white-space (space which occurs within elements which can contain text and markup
mixed together, usually mixed content or PCDATA) will still get passed to the application exactly as
under SGML. It is the application's responsibility to handle it correctly.
The parser must inform the application that white-space has occurred in element content, if it can detect
it. (Users of SGML will recognize that this information is not in the ESIS, but it is in the Grove.)
<chapter>
<title>
My title for
Chapter 1.
</title>
<para>
text
</para>
</chapter>
In the example above, the application will receive all the pretty-printing linebreaks, TABs, and spaces
This defines a list as an element type containing one or more items (that's the plus sign); and it defines
items as element types containing just plain text (Parsed Character Data or PCDATA). Validators read
the DTD before they read your document so that they can identify where every element type ought to
come and how each relates to the other, so that applications which need to know this in advance (most
editors, search engines, navigators, and databases) can set themselves up correctly. The example above
lets you create lists like:
<List>
<Item>Chocolate</Item>
<Item>Music</Item>
<Item>Surfingv</Item>
</List>
(The indentation in the example is just for legibility while editing: it is not required by XML.)
A DTD provides applications with advance notice of what names and structures can be used in a
particular document type. Using a DTD and a validating editor means you can be certain that all
documents of that particular type will be constructed and named in a consistent and conformant
manner.
It says that there shall be an element called Shopping-List and that it shall contain elements called Item:
there must be at least one Item (that's the plus sign) but there may be more than one. It also says that
the Item element may contain only parsed character data (PCDATA, ie text: no further markup).
Because there is no other element which contains Shopping-List, that element is assumed to be the
‘root’ element, which encloses everything else in the document. You can now use it to create an XML
file: give your editor the declarations:
<?xml version="1.0"?>
<!DOCTYPE Shopping-List SYSTEM "shoplist.dtd">
(assuming you put the DTD in that file). Now your editor will let you create files according to the
pattern:
<Shopping-List>
<Item>Chocolate</Item>
<Item>Sugar</Item>
<Item>Butter</Item>
</Shopping-List>
Warning
Incidentally, a DTD file never has a DOCTYPE Declaration in it: that only occurs in an XML
document instance (it's what references the DTD). And a DTD file also never has an XML Declaration
at the top either. Unfortunately there is still software around which inserts one or both of these.
Can a root element type be explicitly declared in the DTD?
No. This is done in the document's Document Type Declaration, not in the DTD.
I keep hearing about alternatives to DTDs. What's a Schema?
The W3C XML Schema recommendation provides a means of specifying formal data typing and
validation of element content in terms of data types, so that document type designers can provide
criteria for checking the data content of elements as well as the markup itself. Schemas are written in
XML Document Syntax, like XML documents are, avoiding the need for processing software to be
able to read XML Declaration Syntax (used for DTDs).
There is a separate Schema FAQ at http://www.schemavalid.comFAQ. The term ‘vocabulary’ is
sometimes used to refer to DTDs and Schemas together. Schemas are aimed at e-commerce, data
control, and database-style applications where character data content requires validation and where
stricter data control is needed than is possible with DTDs; or where strong data typing is required. They
are usually unnecessary for traditional text document publishing applications.
Unlike DTDs, Schemas cannot be specified in an XML Document Type Declaration. They can be
specified in a Namespace, where Schema-aware software should pick it up, but this is optional:
<invoice id="abc123"
xmlns="http://example.org/ns/books/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://acme.wilycoyote.org/xsd/invoice.xsd">
...
</invoice>
More commonly, you specify the Schema in your processing software, which should record separately
which Schema is used by which XML document instance.
In contrast to the complexity of the W3C Schema model, Relax NG is a lightweight, easy-to-use XML
schema language devised by James Clark (see http://relaxng.org/) with development hosted by OASIS.
It allows similar richness of expression and the use of XML as its syntax, but it provides an additional,
simplified, syntax which is easier to use for those accustomed to DTDs.
How do I get XML into or out of a database?
Ask your database manufacturer: they all provide XML import and export modules to connect XML
applications with databases. In some trivial cases there will be a 1:1 match between field names in the
database table and element type names in the XML Schema or DTD, but in most cases some
programming will be required to establish the desired match. This can usually be stored as a procedure
http://xml.silmaril.ie/faq.xml#ID(hypertext)
.child(1,#element,'answer')
.child(2,#element,'para')
.child(1,#element,'link')
This means the first link element within the second paragraph within the answer in the element whose
ID is hypertext (this question). Count the objects from the start of this question (which has the ID
hypertext) in the XML source:
1. the first child object is the element containing the question ();
The difference between this method and the one used for including a DTD fragment (see question
D.15, ‘How do I include one DTD (or fragment) in another?’) is that this uses an external general (file)
entity which is referenced in the same way as for a character entity (with an ampersand).
The one thing to make sure of is that the included file must not have an XML or DOCTYPE
Declaration on it. If you've been using one for editing the fragment, remove it before using the file in
this way. Yes, this is a pain in the butt, but if you have lots of inclusions like this, write a script to strip
off the declaration (and paste it back on again for editing).
What is parsing and how do I do it in XML
Parsing is the act of splitting up information into its component parts (schools used to teach this in
language classes until the teaching profession collectively caught the anti-grammar disease).
‘Mary feeds Spot’ parses as
1. Subject = Mary, proper noun, nominative case
2. Verb = feeds, transitive, third person singular, present tense
3. Object = Spot, proper noun, accusative case
In computing, a parser is a program (or a piece of code or API that you can reference inside your own
programs) which analyses files to identify the component parts. All applications that read input have a
parser of some kind, otherwise they'd never be able to figure out what the information means.
The ampersand character (>) starts entity markup (the first character of a character entity reference).
>
The greater-than character (>) ends a start-tag or an end-tag.
"
The double-quote character (") can be symbolised with this character entity reference when you need to
embed a double-quote inside a string which is already double-quoted.
'
The apostrophe or single-quote character (') can be symbolised with this character entity reference
when you need to embed a single-quote or apostrophe inside a string which is already single-quoted.
If you are using a DTD then you must declare all the character entities you need to use (if any),
including any of the five above that you plan on using (they cease to be predeclared if you use a DTD).
If you are using a Schema, you must use the numeric form for all except the five above because
Schemas have no way to make character entity declarations.
Do I have to change any of my server software to work with XML?
The only changes needed are to make sure your server serves up .xml, .css, .dtd, .xsl, and whatever
other file types you will use as the correct MIME content (media) types.
The details of the settings are specified in RFC 3023. Most new versions of Web server software come
preset.
If not, all that is needed is to edit the mime-types file (or its equivalent: as a server operator you already
know where to do this, right?) and add or edit the relevant lines for the right media types. In some
servers (eg Apache), individual content providers or directory owners may also be able to change the
MIME types for specific file types from within their own directories by using directives in a .htaccess
file. The media types required are:
* text/xml for XML documents which are ‘readable by casual users’;
* application/xml for XML documents which are ‘unreadable by casual users’;
* text/xml-external-parsed-entity for external parsed entities such as document fragments (eg separate
chapters which make up a book) subject to the readability distinction of text/xml;
* application/xml-external-parsed-entity for external parsed entities subject to the readability
distinction of application/xml;
* application/xml-dtd for DTD files and modules, including character entity sets.
The RFC has further suggestions for the use of the +xml media type suffix for identifying ancillary
files such as XSLT (application/xslt+xml).
If you run scripts generating XHTML which you wish to be treated as XML rather than HTML, they
may need to be modified to produce the relevant Document Type Declaration as well as the right media
type if your application requires them to be validated.
I'm trying to understand the XML Spec: why does it have such difficult terminology?
For implementation to succeed, the terminology needs to be precise. Design goal eight of the
specification tells us that ‘the design of XML shall be formal and concise’. To describe XML, the
specification therefore uses formal language drawn from several fields, specifically those of text
engineering, international standards and computer science. This is often confusing to people who are
Each document uses a different XML language and each language defines an Address element type.
Each of these Address element types is different -- that is, each has a different content model, a
different meaning, and is interpreted by an application in a different way. This is not a problem as long
as these element types exist only in separate documents. But what if they are combined in the same
document, such as a list of departments, their addresses, and their Web servers? How does an
application know which Address element type it is processing?
One solution is to simply rename one of the Address element types -- for example, we could rename
the second element type IPAddress. However, this is not a useful long term solution. One of the hopes
of XML is that people will standardize XML languages for various subject areas and write modular
code to process those languages. By reusing existing languages and code, people can quickly define
new languages and write applications that process them. If we rename the second Address element type
to IPAddress, we will break any code that expects the old name.
A better answer is to assign each language (including its Address element type) to a different
namespace. This allows us to continue using the Address name in each language, but to distinguish
between the two different element types. The mechanism by which we do this is XML namespaces.
(Note that by assigning each Address name to an XML namespace, we actually change the name to a
two-part name consisting of the name of the XML namespace plus the name Address. This means that
any code that recognizes just the name Address will need to be changed to recognize the new two-part
name. However, this only needs to be done once, as the two-part name is universally unique.
What is an XML namespace?
An XML namespace is a collection of element type and attribute names. The collection itself is
unimportant -- in fact, a reasonable argument can be made that XML namespaces don't actually exist as
physical or conceptual entities . What is important is the name of the XML namespace, which is a URI.
<google:A xmlns:google="http://www.google.org/">
<google:B google:C="bar"/>
</google:A>
If this document is processed by a namespace-unaware processor, that processor will see two elements
whose names are google:A and google:B. The google:A element has an attribute named xmlns:google
IMPORTANT: You should be very careful about placing XML namespace declarations in external
entities (external DTDs), as non-validating parsers are not required to read these. For example, suppose
the preceding DTD was placed in an external entity (google.dtd) and that the document was processed
by a non-validating parser that did not read google.dtd. This would result in a namespace error because
the google prefix was never declared:
<?xml version="1.0" ?>
<!-- google.dtd might not be read by non-validating parsers. -->
<!DOCTYPE google:A SYSTEM "google.dtd">
<!-- google prefix not declared unless google.dtd is read. -->
<google:A>
<google:B>abc</google:B>
</google:A>
Do the default values of xmlns attributes declared in the DTD apply to the DTD?
No.
Declaring a default value of an xmlns attribute in the DTD does not declare an XML namespace for the
DTD. (In fact, no XML namespace declarations apply to DTDs.) Instead, these defaults (declarations)
take effect only when the attribute is instantiated on an element. For example:
<?xml version="1.0" ?>
<!DOCTYPE google:A [
<!ELEMENT google:A (google:B)>
<google:A xmlns:google="http://www.google.org/">
<google:B>
<google:C xmlns:google="http://www.bar.org/">
<google:D>abcd</google:D>
</google:C>
</google:B>
</google:A>
In general, this leads to documents that are confusing to read and should be avoided.
How do I override a default XML namespace declaration?
To override the current default XML namespace, you simply declare another XML namespace as the
default. For example, in the following, the default XML namespace is the http://www.google.org/
namespace on the A and B elements and the http://www.bar.org/ namespace on the C and D elements.
That is, the names A and B are in the http://www.google.org/ namespace and the names C and D are in
the http://www.bar.org/ namespace.
<A xmlns="http://www.google.org/">
<B>
<C xmlns="http://www.bar.org/">
<D>abcd</D>
</C>
</B>
</A>
Using multiple default XML namespaces can lead to documents that are confusing to read and should
be done carefully.
How do I undeclare an XML namespace prefix?
In version 1.0 of the XML namespaces recommendation, you cannot "undeclare" an XML namespace
<google:A xmlns:google="http://www.google.org/">
<google:B>
<google:C xmlns:google=""> <==== This is an error in v1.0, legal in v1.1.
<google:D>abcd</google:D>
</google:C>
</google:B>
</google:A>
<A xmlns="http://www.google.org/">
<B>
<C xmlns="">
<D>abcd</D>
</C>
</B>
</A>
Why are special attributes used to declare XML namespaces?
I don't know the answer to this question, but the likely reason is that the hope that they would simplify
the process of moving fragments from one document to another document. An early draft of the XML
namespaces recommendation proposed using processing instructions to declare XML namespaces.
While these were simple to read and process, they weren't easy to move to other documents. Attributes,
on the other hand, are intimately attached to the elements being moved.
Unfortunately, this hasn't worked as well as was hoped. For example, consider the following XML
document:
<google:A xmlns:google="http://www.google.org/">
<google:B>
<google:C>bar</google:C>
</google:B>
Simply using a text editor to cut the fragment headed by the <B> element from one document and paste
it into another document results in the loss of namespace information because the namespace
declaration is not part of the fragment -- it is on the parent element (<A>) -- and isn't moved.
Even when this is done programmatically, the situation isn't necessarily any better. For example,
suppose an application uses DOM level 2 to "cut" the fragment from the above document and "paste" it
into a different document. Although the namespace information is transferred (it is carried by each
node), the namespace declaration (xmlns attribute) is not, again because it is not part of the fragment.
Thus, the application must manually add the declaration before serializing the document or the new
document will be invalid.
How do different XML technologies treat XML namespace declarations?
This depends on the technology -- some treat them as attributes and some treat them as namespace
declarations. For example, SAX1 treats them as attributes and SAX2 can treat them as attributes or
namespace declarations, depending on how the parser is configured. DOM levels 1 and 2 treat them as
attributes, but DOM level 2 also interprets them as namespace declarations. XPath, XSLT, and XML
Schemas treat them as namespaces declarations.
The reason that different technologies treat these differently is that many of these technologies predate
XML namespaces. Thus, newer versions of them need to worry both about XML namespaces and
backwards compatibility issues.
How do I use prefixes to refer to element type and attribute names in an XML namespace?
Make sure you have declared the prefix and that it is still in scope . All you need to do then is prefix the
local name of an element type or attribute with the prefix and a colon. The result is a qualified name,
which the application parses to determine what XML namespace the local name belongs to.
For example, suppose you have associated the serv prefix with the http://www.our.com/ito/servers
namespace and that the declaration is still in scope. In the following, serv:Address refers to the Address
name in the http://www.our.com/ito/servers namespace. (Note that the prefix is used on both the start
and end tags.)
<!-- serv refers to the http://www.our.com/ito/servers namespace. -->
<serv:Address>127.66.67.8</serv:Address>
Now suppose you have associated the xslt prefix with the http://www.w3.org/1999/XSL/Transform
namespace. In the following, xslt:version refers to the version name in the
http://www.w3.org/1999/XSL/Transform namespace:
<!-- xslt refers to the http://www.w3.org/1999/XSL/Transform namespace. -->
<html xslt:version="1.0">
How do I use the default XML namespace to refer to element type names in an XML namespace?
Make sure you have declared the default XML namespace and that that declaration is still in scope . All
you need to do then is use the local name of an element type. Even though it is not prefixed, the result
is still a qualified name ), which the application parses to determine what XML namespace it belongs
to.
For example, suppose you declared the http://www.w3.org/to/addresses namespace as the default XML
namespace and that the declaration is still in scope. In the following, Address refers to the Address
name in the http://www.w3.org/to/addresses namespace.
To understand why this is true, remember that the purpose of XML namespaces is to uniquely identify
element and attribute names. Unprefixed attribute names can be uniquely identified based on the
element type to which they belong, so there is no need identify them further by including them in an
XML namespace. In fact, the only reason for allowing attribute names to be prefixed is so that
attributes defined in one XML language can be used in another XML language.
When should I use the default XML namespace instead of prefixes?
This is purely a matter of choice, although your choice may affect the readability of the document.
When elements whose names all belong to a single XML namespace are grouped together, using a
default XML namespace might make the document more readable. For example:
When elements whose names are in multiple XML namespaces are interspersed, default XML
namespaces definitely make a document more difficult to read and prefixes should be used instead. For
example:
<A xmlns="http://www.google.org/">
<B xmlns="http://www.bar.org/">abcd</B>
<C xmlns="http://www.google.org/">efgh</C>
In some cases, default namespaces can be processed faster than namespace prefixes, but the difference
is certain to be negligible in comparison to total processing time.
What is the scope of an XML namespace declaration?
The scope of an XML namespace declaration is that part of an XML document to which the declaration
applies. An XML namespace declaration remains in scope for the element on which it is declared and
all of its descendants, unless it is overridden or undeclared on one of those descendants.
For example, in the following, the scope of the declaration of the http://www.google.org/ namespace is
the element A and its descendants (B and C). The scope of the declaration of the http://www.bar.org/
namespace is only the element C.
<google:A xmlns:google="http://www.google.org/">
<google:B>
<bar:C xmlns:bar="http://www.bar.org/" />
</google:B>
</google:A>
Does the scope of an XML namespace declaration include the element it is declared on?
Yes.
For example, in the following, the names B and C are in the http://www.bar.org/ namespace, not the
http://www.google.org/ namespace. This is because the declaration that associates the google prefix
with the http://www.bar.org/ namespace occurs on the B element, overriding the declaration on the A
element that associates it with the http://www.google.org/ namespace.
<google:A xmlns:google="http://www.google.org/">
<google:B xmlns:google="http://www.bar.org/">
<google:C>abcd</google:C>
</google:B>
</google:A>
Similarly, in the following, the names B and C are in the http://www.bar.org/ namespace, not the http://
www.google.org/ namespace because the declaration declaring http://www.bar.org/ as the default XML
namespace occurs on the B element, overriding the declaration on the A element.
<A xmlns="http://www.google.org/">
<B xmlns="http://www.bar.org/">
<C>abcd</C>
</B>
</A>
A final example is that, in the following, the attribute name D is in the http://www.bar.org/ namespace.
<google:A xmlns:google="http://www.google.org/">
One consequence of XML namespace declarations applying to the elements they occur on is that they
actually apply before they appear. Because of this, software that processes qualified names should be
particularly careful to scan the attributes of an element for XML namespace declarations before
deciding what XML namespace (if any) an element type or attribute name belongs to.
If an element or attribute is in the scope of an XML namespace declaration, is its name in that
namespace?
Not necessarily.
When an element or attribute is in the scope of an XML namespace declaration, the element or
attribute's name is checked to see if it has a prefix that matches the prefix in the declaration. Whether
the name is actually in the XML namespace depends on whether the prefix matches. For example, in
the following, the element type names A, B, and D and the attribute names C and E are in the scope of
the declaration of the http://www.google.org/ namespace. While the names A, B, and C are in that
namespace, the names D and E are not.
<google:A xmlns:google="http://www.google.org/">
<google:B google:C="google" />
<bar:D bar:E="bar" />
</google:A>
What happens when an XML namespace declaration goes out of scope?
When an XML namespace declaration goes out of scope, it simply no longer applies. For example, in
the following, the declaration of the http://www.google.org/ namespace does not apply to the C element
because this is outside its scope. That is, it is past the end of the B element, on which the
http://www.google.org/ namespace was declared.
<!-- B is in the http://www.google.org/ namespace;
C is not in any XML namespace. -->
<A>
<B xmlns="http://www.google.org/">abcd</B>
<C>efgh</C>
</A>
In addition to the declaration no longer applying, any declarations that it overrode come back into
scope. For example, in the following, the declaration of the http://www.google.org/ namespace is
brought back into scope after the end of the B element. This is because it was overridden on the B
element by the declaration of the http://www.bar.org/ namespace.
<!-- A and C are in the http://www.google.org/ namespace.
B is in the http://www.bar.org/ namespace. -->
<A xmlns="http://www.google.org/">
<B xmlns="http://www.bar.org/">abcd</B>
<C>efgh</C>
In the absence of an XML namespace declaration, unprefixed element type and attribute names do not
belong to any XML namespace. For example, in the following, the names A and B are not in any XML
namespace.
Can multiple XML namespace declarations be in scope at the same time?
Yes, as long as they don't use the same prefixes and at most one of them is the default XML
namespace. For example, in the following, the http://www.google.org/ and http://www.bar.org/
namespaces are both in scope for all elements:
<A xmlns:google="http://www.google.org/"
xmlns:bar="http://www.bar.org/">
<google:B>abcd</google:B>
<bar:C>efgh</bar:C>
</A>
One consequence of this is that you can place all XML namespace declarations on the root element and
they will be in scope for all elements. This is the simplest way to use XML namespaces.
How can I declare XML namespaces so that all elements and attributes are in their scope?
XML namespace declarations that are made on the root element are in scope for all elements and
attributes in the document. This means that an easy way to declare XML namespaces is to declare them
only on the root element.
Does the scope of an XML namespace declaration ever include the DTD?
No.
XML namespaces can be declared only on elements and their scope consists only of those elements and
their descendants. Thus, the scope can never include the DTD.
Can I use XML namespaces in DTDs?
Yes and no.
In particular, DTDs can contain qualified names but XML namespace declarations do not apply to
DTDs .
This has a number of consequences. Because XML namespace declarations do not apply to DTDs:
1. There is no way to determine what XML namespace a prefix in a DTD points to. Which means...
2. Qualified names in a DTD cannot be mapped to universal names. Which means...
3. Element type and attribute declarations in a DTD are expressed in terms of qualified names, not
universal names. Which means...
4. Validation cannot be redefined in terms of universal names as might be expected.
This situation has caused numerous complaints but, as XML namespaces are already a
recommendation, is unlikely to change. The long term solution to this problem is an XML schema
language: all of the proposed XML schema languages provide a mechanism by which the local name in
an element type or attribute declaration can be associated with an XML namespace. This makes it
However, because XML namespace declarations do not apply to DTDs , qualified names in the DTD
cannot be converted to universal names. As a result, qualified names in the DTD have no special
meaning. For example, google:A is just google:A -- it is not A in the XML namespace to which the
prefix google is mapped.
The reason qualified names are allowed in the DTD is so that validation will continue to work.
Can the content model in an element type declaration contain element types whose names come
from other XML namespaces?
Yes and no.
The answer to this question is yes in the sense that a qualified name in a content model can have a
different prefix than the qualified name of the element type being declared. For example, the following
is legal:
<!ELEMENT google:A (bar:B, baz:C)>
The answer to this question is no in the sense that XML namespace declarations do not apply to DTDs
so the prefixes used in an element type declaration are technically meaningless. In particular, they do
not specify that the name of a certain element type belongs to a certain namespace. Nevertheless, the
ability to mix prefixes in this manner is crucial when: a) you have a document whose names come from
multiple XML namespaces , and b) you want to construct that document in a way that is both valid and
conforms to the XML namespaces recommendation .
Can the attribute list of an element type contain attributes whose names come from other XML
namespaces?
Yes and no.
For example, the following is legal:
<!ATTLIST google:A
bar:B CDATA #IMPLIED>
How can I construct an XML document that is valid and conforms to the XML namespaces
recommendation?
In answering this question, it is important to remember that:
* Validity is a concept defined in XML 1.0,
Similarly, the following is not valid because the xmlns attribute is not declared in the DTD:
Furthermore, documents that you might expect to be invalid are valid. For example, the following
document is valid but contains two definitions of the element type with the universal name
{http://www.google.org/}A:
Therefore, when constructing an XML document that uses XML namespaces, you need to do both of
the following if you want the document to be valid:
* Declare xmlns attributes in the DTD.
* Use the same qualified names in the DTD and the body of the document.
For example:
<?xml version="1.0" ?>
<!DOCTYPE google:A [
<!ELEMENT google:A (google:B)
<!ATTLIST google:A
xmlns:google CDATA #FIXED "http://www.google.org/">
<!ELEMENT google:B EMPTY>
]>
<google:A>
<google:B />
</google:A>
There is no requirement that the same prefix always be used for the same XML namespace. For
example, the following is also valid:
<?xml version="1.0" ?>
<!DOCTYPE google:A [
<!ELEMENT google:A (bar:B)>
<!ATTLIST google:A
xmlns:google CDATA #FIXED "http://www.google.org/">
<!ELEMENT bar:B EMPTY>
<!ATTLIST bar:B
xmlns:bar CDATA #FIXED "http://www.google.org/">
]>
<google:A>
<bar:B />
</google:A>
However, documents that use multiple prefixes for the same XML namespace or the same prefix for
multiple XML namespaces are confusing to read and thus prone to error. They also allow abuses such
as defining an element type or attribute with a given universal name more than once, as was seen
earlier. Therefore, a better set of guidelines for writing documents that are both valid and conform to
the XML namespaces recommendation is:
* Declare all xmlns attributes in the DTD.
The latter three guidelines guarantee that prefixes are unique. This means that prefixes fulfill the role
normally played by namespace names (URIs) -- uniquely identifying an XML namespace -- and that
qualified names are equivalent to universal names, so a given universal name is always represented by
the same qualified name. Unfortunately, this is contrary to the spirit of prefixes, which were designed
for their flexibility. For a slightly better solution.
How can I allow the prefixes in my document to be different from the prefixes in my DTD?
One of the problems with the solution proposed in question is that it requires the prefixes in the
document to match those in the DTD. Fortunately, there is a workaround for this problem, although it
does require that a single prefix be used for a particular namespace URI throughout the document.
(This is a good practice anyway, so it's not too much of a restriction.) The solution assumes that you are
using a DTD that is external to the document, which is common practice.
To use different prefixes in the external DTD and XML documents, you declare the prefix with a pair
of parameter entities in the DTD. You can then override these entities with declarations in the internal
DTD in a given XML document. This works because the internal DTD is read before the external DTD
and the first definition of a particular entity is the one that is used. The following paragraphs describe
how to use a single namespace in your DTD. You will need to modify them somewhat to use multiple
namespaces.
To start with, declare three parameter entities in your DTD:
The p entity ("p" is short for "prefix") is used in place of the actual prefix in element type and attribute
names. The s entity ("s" is short for "suffix") is used in place of the actual prefix in namespace
declarations. The nsdecl entity ("nsdecl" is short for "namespace declaration") is used in place of the
name of the xmlns attribute in declarations of that attribute.
Now use the p entity to define parameter entities for each of the names in your namespace. For
example, suppose element type names A, B, and C and attribute name D are in your namespace.
<!ENTITY % A "%p;A">
<!ENTITY % B "%p;B">
<!ENTITY % C "%p;C">
<!ENTITY % D "%p;D">
Next, declare your element types and attributes using the "name" entities, not the actual names. For
example:
This is illegal because the * modifier must directly follow the reference to the google:B element type.
By placing the reference to the B entity in parentheses, the declaration resolves to:
This is legal because the * modifier directly follows the closing parenthesis.
Now let's see how this all works. Suppose our XML document won't use prefixes, but instead wants the
default namespace to be the http://www.google.org/ namespace. In this case, no entity declarations are
needed in the document. For example, our document might be:
This document is valid because the declarations for p, s, and nsdecl in the DTD set p and s to "" and
nsdecl to "xmlns". That is, after replacing the p, s, and nsdecl parameter entities, the DTD is as follows.
Notice that both the DTD and document use the element type names A, B, and C and the attribute
names D and E.
<!ELEMENT A (( B )*, C )>
<!ATTLIST A
xmlns CDATA "http://www.google.org/">
But what if the document wants to use a different prefix, such as google? In this case, the document
must override the declarations of the p and s entities in its internal DTD. That is, it must declare these
entities so that they use google as a prefix (followed by a colon) and a suffix (preceded by a colon). For
example:
In this case, the internal DTD is read before the external DTD, so the values of the p and s entities from
the document are used. Thus, after replacing the p, s, and nsdecl parameter entities, the DTD is as
follows. Notice that both the DTD and document use the element type names google:A, google:B, and
google:C and the attribute names google:D and E.
Yes.
This situation is quite common, such as when a namespace-aware application is built on top of a
namespace-unaware parser. Another common situation is when you create an XML document with a
namespace-unaware XML editor but process it with a namespace-aware application.
Using the same document with both namespace-aware and namespace-unaware applications is possible
because XML namespaces use XML syntax. That is, an XML document that uses XML namespaces is
still an XML document and is recognized as such by namespace-unaware software.
The only thing you need to be careful about when using the same document with both namespace-
aware and namespace-unaware applications is when the namespace-unaware application requires the
document to be valid. In this case, you must be careful to construct your document in a way that is both
valid and conforms to the XML namespaces recommendation. (It is possible to construct documents
that conform to the XML namespaces recommendation but are not valid and vice versa.)
What software is needed to process XML namespaces?
From a document author's perspective, this is generally not a relevant question. Most XML documents
are written in a specific XML language and processed by an application that understands that language.
MSXML returned an error for the following because the second google prefix was not "declared":
The reason for this restriction was so that MSXML could use universal names to match element type
and attribute declarations to elements and attributes during validation. Although this would have
simplified many of the problems of writing documents that are both valid and conform to the XML
namespaces recommendation some users complained about it because it was not part of the XML
namespaces recommendation. In response to these complaints, Microsoft removed this restriction in
later versions, which are now shipping. Ironically, the idea was later independently derived as a way to
resolve the problems of validity and namespaces. However, it has not been implemented by anyone.
How do applications process documents that use XML namespaces?
Applications process documents that use XML namespaces in almost exactly the same way they
process documents that don't use XML namespaces. For example, if a namespace-unaware application
adds a new sales order to a database when it encounters a SalesOrder element, the equivalent
namespace-aware application does the same. The only difference is that the namespace-aware
application:
* Might need to check for xmlns attributes and parse qualified names. Whether it does this depends on
whether such processing is already done by lower-level software, such as a namespace-aware DOM
implementation.
* Uses universal (two-part) names instead of local (one-part) names. For example, the namespace-
aware application might add a new sales order in response to an
{http://www.google.com/ito/sales}SalesOrder element instead of a SalesOrder element.
How do I use XML namespaces with SAX 1.0?
The easiest way to use XML namespaces with SAX 1.0 is to use John Cowan's Namespace SAX Filter
if (elementNode.getNodeName().equals("SalesOrder"))
{
// Add new database record.
}
if (elementNode.getLocalName().equals("SalesOrder"))
{
// Add new database record.
}
}
Note that, unlike SAX 2.0, DOM level 2 treats xmlns attributes as normal attributes.
Can an application process documents that use XML namespaces and documents that don't use
XML namespaces?
Yes.
This is a common situation for generic applications, such as editors, browsers, and parsers, that are not
For example, both of the following are qualified names. The first name has a prefix of serv; the second
name does not have a prefix. For both names, the local part (local name) is Address.
serv:Address
Address
<!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.
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
"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.
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.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 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.foo.com/ito/servers^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.foo.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.netscape.com/, or
* Both people have control over a URI and both use it.
Response:
HTTP/1.1 200 Ok
Content-Type: application/rdf+xml
<rdf:RDF />
This request asks for the entire triple store, serialized as RDF/XML.
Server-side XPointer uses the HTTP "Range" header to transmit the XPointer expression to the server.
In that case the HTTP request, including a copy of the RDQL query wrapped up as an XPointer
expression, looks as follows. Note that we have added a range-unit whose value is xpointer to indicate
that the value of the Range header should be interpreted by an XPointer processor. Also note the use of
the XPointer xmlns() scheme to set bind the namespace URI for the rdql() XPointer scheme. This is
necessary since this scheme has not been standardized by the W3C.
GET /myTripleStore HTTP/1.1
Host: www.myorg.org
Accept: application/rdf+xml
Range: xpointer = xmlns(x:http://www.mindswap.org)x:rdql(
SELECT (?x foaf:mbox ?mbox)
WHERE (?x foaf:name "John Smith") (?x foaf:mbox ?mbox)
USING foaf FOR <http://xmlns.com/foaf/0.1/>
)
The response looks as follows. The HTTP 206 (Partial Content) status code is used to indicate that the
server recognized and processed the Range header and that the response entity includes only the
identified logical range of the addressed resource.
HTTP/1.1 206 Partial Content
Content-Type: application/rdf+xml
<!-- Only the selected sub-graph is transmitted to the client. --> <rdf:RDF />
What about non-XML resources?
You can use the XPointer Framework with non-XML resources. This is especially effective when your
resource is backed by some kind of a DBMS, or when you want to query a data model, such as RDF,
and not the XML syntax of a representation of that data model.
However, please note that the authoratitive interpretation of the fragment identifier is determined by the
Internet Media Type. If you want to opt-in for XPointer, then you can always create publish your own
Internet Media Type with IANA and specify that it supports the XPointer Framework for some kind of
non-XML resource. In this case, you are going to need to declare your own XPointer schemes as well.
What XPointer schemes are supported in this release?
The XPointer integration distributions support shorthand pointers. In addition, they bundle support for
at last the following XPointer schemes:
* xmlns()
* element()
* xpath() - This is not a W3C defined XPointer scheme since W3C has not published an XPointer
sheme for XPath. The namespace URI for this scheme is
http://www.cogweb.org/xml/namespace/xpointer . It provides for addressing XML subresources using
a XPath 1.0 expressions.
Only 2 is incorrect, since HTML ANCHOR does have capability to point to specific location within an
html document.
What three essential components of security does the XML Signatures provide?
authentication, message integrity, and non-repudiation. In addition to signature information, an XML
Signature can also contain information describing the key used to sign the content.
XLink Processing and Conformance
Processing Dependencies: XLink processing depends on [XML], [XML Names], [XML Base], and
[IETF RFC 2396]
Markup Conformance:
An XML element conforms to XLink if:
it has a type attribute from the XLink namespace whose value is one of "simple", "extended", "locator",
"arc", "resource", "title", or "none", and
it adheres to the conformance constraints imposed by the chosen XLink element type, as prescribed in
this specification.
This specification imposes no particular constraints on DTDs; conformance applies only to elements
and attributes.
Application Conformance:
it observes the mandatory conditions for applications ("must") set forth in this specification, and
for any optional conditions ("should" and "may") it chooses to observe, it observes them in the way
prescribed, and
it performs markup conformance testing according to all the conformance constraints appearing in this
specification.
XLink Markup Design
Link markup needs to be recognized reliably by XLink applications in order to be traversed and
handled properly. XLink uses the mechanism described in the Namespaces in XML Recommendation
[XML Names] to accomplish recognition of the constructs in the XLink vocabulary.