PDA

View Full Version : cant understand the function of DTDs


s_dragons_lair_c
12-26-2005, 02:55 PM
Hi,
I am a beginner to XML and have completed the DTD part. Now from what I have read I have presumed that the function of the DTD is to set rules for the XML document. Which means if I have an element in my XML document but not in my external DTD (or internal) or maybe a child element of some element in my XML document but not in my DTD, the browser should give an error message. So I tried to get an error by doing so but the browser doesn't give any error. It just displays the document as it is. I even tried giving an invalid value to an attribute (I gave some other options in my DTD for the attribute) but the browser does not give any error message and displays the invalid attribute. Even if I give the root element in the <!DOCTYPE> different from the root element in the document still does not give any error message.

So, is what I have understood from my reading wrong or can there be some problem with my coding? :(

ranjan
12-26-2005, 04:04 PM
The DTD specifies the precedence or priority of the rules set. If no rule exists the browsers will just go with the defaults. So as long as the XML is valid the browser will not give you errors

For eg:

If I redine <div> in a custom doctype to have a display of sphere instead of box (as in XHTML). Then the browser will render it as sphere (if the browser is capable of doing so) because prcedence is given to my doctype before XHTML standard doctype

Similarly

If i use the tag <father hewas="great">He provided for us</father> in an XHTML doc without a custom DTD. It will be ingnored, but because the tag is wellformed I will also get no errors

s_dragons_lair_c
12-26-2005, 04:49 PM
OK, so i got the answer to my question. But another question arises in my amateur mind that what is the role of DTDs if some1 can just write well-formed markup and go against the rules specified by him in the DTD. What is its use if I give two available options for an attribute in a DTD and then provide a third different option in my XML document. And another thing about the root element specified in the Doctype. Can the root element in the markup be different from the one specified in the Doctype.
Don't think of it as a stupid question (if it is) since I'm only 16 and am studying XML for school. Thought would gain some extra knowledge apart from the course. Cant understand the teacher. Speaks only Bengali.

ranjan
12-26-2005, 09:27 PM
Great to see you such good questions at 16! I wish I did!! No question is silly, only answers can be silly!!!

DTDs if some1 can just write well-formed markup and go against the rules specified by him in the DTD.

If the original DTD is written by somebody else on whom I have no control (like W3C for XHTML or SVG) Then I can use this feature to extend the Markup Language

Can the root element in the markup be different from the one specified in the Doctype.

No

s_dragons_lair_c
12-27-2005, 11:33 AM
Can the root element in the markup be different from the one specified in the Doctype.



No


So do u mean i should get an error message in my browser(my knowledge is only limited to the markup in a browser). If yes, I don't.

ranjan
12-27-2005, 02:00 PM
Browsers are not very strict XML parsers. Use an XML validator

http://www.google.com/search?q=xml+validat...:en-US:official ('http://www.google.com/search?q=xml+validator&start=0&ie=utf-8&oe=utf-8&client=firefox-a&rls=org.mozilla:en-US:official')

s_dragons_lair_c
12-27-2005, 07:22 PM
thanx u seem 2 clear all my doubts.
another question.
What is the use of the PUBLIC declaration in DOCTYPE.
Thanks again for all the help since I dont have an1 else 2 help.

ranjan
12-28-2005, 02:54 PM
An optional PUBLIC identifier can specify another string for the parser to use when locating a DTD file. These usually use a string similar to the following, which avoids any system-specific information to make the document more portable across different systems:

<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML//EN"
"../dtds/chapter.dtd">

The XML parser should look up this PUBLIC identifier somewhere to find the exact location of the local copy of the DTD file.

s_dragons_lair_c
12-28-2005, 03:23 PM
thanx.
no more questions at this point of time