views:

94

answers:

2

Hi


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

+3  A: 

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.

Cumbayah
Good advice. If the OP is learning DTDs then he might also find RELAX NG compact syntax fairly familiar as a next step on the road to enlightenment.
Nic Gibson
I apologize for not replying sooner, but I had my PC in repair. Anyways, in case you find the time … I’ve edited my initial post in response to your reply
SourceC
+2  A: 

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.

bobince
thank you for your help
SourceC