SourceC
SourceC

Reputation: 3929

Document type declaration and 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

Upvotes: 1

Views: 2844

Answers (2)

bobince
bobince

Reputation: 536379

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.

That is correct. It is possible by extensive use of parameter entities throughout the DTD to allow the prefixes to be used by each part of the compound doctype to be arbitrarily configured:

<!ENTITY % mydoc.prefix "mydoc:">
...
<!ENTITY % mydoc.element.qname "%mydoc.prefix;element">
<!ENTITY % mydoc.foo.qname "%mydoc.prefix;foo">
<!ENTITY % mydoc.bar.qname "%mydoc.prefix;bar">
<!ELEMENT %mydoc.element.qname; (%mydoc.foo.qname; | %mydoc.bar.qname;)>

See the work on XHTML Modularization and the “DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1” doctype for some examples of how this can work. (The answer is “not very well”.)

does that mean all occurrences of this element within XML document will need to include a prefix “pers”?

Yes, unless you jigger the DTD to define the same elements all over again for a different prefix (or with no prefix). Whilst you could do this (and you could even make it possible for elements with one prefix to have a content model including elements with the other prefix), it would be massively painful to write using multiple levels of parameter entities and your DTD would end up unreadable.

+1 to Cumbayah's advice: if you have any choice in the matter (and especially if you are doing anything involving namespaces) don't use creaky old DTD. Any of the alternatives (W3 Schema, RELAX NG, Schematron...) will be better suited to the task today.

Upvotes: 3

user116587
user116587

Reputation:

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.

Upvotes: 5

Related Questions