|
XML Interview Questions and Answers
Where can I declare an XML namespace?
You can declare an XML namespace on any element in an
XML document. The namespace is in scope for that element
and all its descendants unless it is overridden.
Can I use an attribute default in a DTD to declare an
XML namespace?
Yes.
For example, the following uses the FIXED attribute
xmlns:google on the A element type to associate the
google prefix with the http://www.google.org/ namespace.
The effect of this is that both A and B are in the
http://www.google.org/ namespace.
<?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 (#PCDATA)>
]>
<!-- google prefix declared through default attribute.
-->
<google:A>
<google:B>abc</google:B>
</google:A>
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)>
<!ATTLIST google:A
xmlns:google CDATA #FIXED "http://www.google.org/">
<!ELEMENT google:B (#PCDATA)>
]>
<google:A> <========== Namespace declaration takes
effect here.
<google:B>abc</google:B>
</google:A> <========= Namespace declaration ends here.
For more information, see question 7.2. (Note that an
earlier version of MSXML (the parser used by Internet
Explorer) did use fixed xmlns attribute declarations as
XML namespace declarations, but that this was removed in
MSXML 4.
How do I override an XML namespace declaration that uses
a prefix?
To override the prefix used in an XML namespace
declaration, you simply declare another XML namespace
with the same prefix. For example, in the following, the
google prefix is associated with 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.
<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 prefix. It remains
in scope until the end of the element on which it was
declared unless it is overridden. Furthermore, trying to
undeclare a prefix by redeclaring it with an empty
(zero-length) name (URI) results in a namespace error.
For example:
<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>
In version 1.1 of the XML namespaces recommendation
[currently a candidate recommendation -- February,
2003], you can undeclare an XML namespace prefix by
redeclaring it with an empty name. For example, in the
above document, the XML namespace declaration
xmlns:google="" is legal and removes the mapping from
the google prefix to the http://www.google.org URI.
Because of this, the use of the google prefix in the
google:D element results in a namespace error.
Page Numbers
: 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Have a Question ?
post your questions here. It
will be answered as soon as possible.
Check
HTML Interview
Questions for more HTML Interview Questions with Answers
Check
JDBC Interview
Questions for more JDBC Interview Questions with Answers
Check
Job Interview Questions
for more Interview Questions with Answers
|