Xml – Document type declaration and namespaces

dtdxmlxml-namespaces

I’m new to XML and am currently learning about Document Type Declaration. Anyways, when declaring elements in the body of DTD, the element name must appear exactly as it will within the XML document, including any namespace prefix, which means users can’t define their own namespace, but must use the prefix defined within DTD.

a) I assume that even though we must use prefixes defined within DTD, we are still able to choose to which URIs these prefixes point to?

b) Assuming we declare ( in DTD ) an element , where pers is a namespace prefix, does that mean all occurrences of this element within XML document will need to include a prefix “pers”? If that is the case, then that would mean that with DTDs we can’t use a default namespace feature?!

thanx

EDIT:

a)

Really, unless there is a particularly good reason to or you have simple syntactical requirements and no need for namespaces, you should consider using XML Schemas instead.

I do plan to use XML Schema instead, but I’d also like to learn the basics of DTDs.

b)

…there is no notion of namespace URIs (nor a default namespace).

If we declare attribute named “xmlns” within DTD:

<!ATTLIST contact xmlns CDATA #REQUIRED> 

then XML document could use the default namespace feature( here child element <name> is in the default namespace):

       ...
<contact xmlns=”www.somewhere.com” … > 
   <name></name>
</contact>       
       ...

thanx

Best Answer

DTDs have no notion of namespaces whatsoever. The namespace concept was introduced after their conception.

For XML namespaces, the important part is the namespace URI and not the prefix; the prefix may be altered freely by the user.

When given namespace "prefixes" in a DTD, on the other hand, the prefix part is simply considered part of the element name (since DTD has no namespace concept). Therefore, the "prefix" canNOT be altered and there is no notion of namespace URIs (nor a default namespace).

Really, unless there is a particularly good reason to or you have simple syntactical requirements and no need for namespaces, you should consider using XML Schemas instead.

The full Schema spec can be daunting, but I find that one learns a certain adequate subset that is not that complicated. The folks at W3Schools have a good primer for the basics.

Related Topic