Explore model answers categorized by subjects like Java, HTML, DBMS, and more.
DTD is stands for Document Type Defintion. We use DTD in XML document to define the legal building blocks.
Using DTD we can also define the structure of an document with name of legal attributes and elements.
DTD has two types of declarations internal and external.I have given you brief information of internal and external DTD.
1. Internal DTD Declaration : I have given you a syntax using them you can declare internal DTD in our XML file.
Syntax:
<!DOCTYPE root-element [element-declarations]>
Example: Now I used internal DTD in XML file like that,
<?xml version=\"1.0\"?>
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
<to>Abhi</to>
<from>Sudd</from>
<heading>Message</heading>
<body>Recently R4R given a VoiceBox facility using them you can chat with R4R</body>
</note>
Some main reasons to use DTD are given Below:
1. Using DTD XML files can write its description into its format.
2. DTD is an most acceptable standard using DTD different group members can interchange data each other.
3. Also, using DTD we can check the validity of our own data and the data that comes from different groups(other persons).
I have given you some building blocks that are used for making an XML and HTML document.The list are:
1. Elements
2. Attributes
3. Entities
4. CDATA
5. PCDATA
Main building block of an XML and HTML document are elements.
1. Elements : Elements may contain text, other elements or empty.Some empty HTML elements are \"hr\",\"img\" and \"br\".
Some HTML elements are \"head\",\"body\" and \"title\" etc.
Some XML elements are \"main\",\"from\" and \"to\" etc.
Example:
<title>write here some text</title>
<body>write here some text</body>
<message>write here some text</message>
<to>write here some text</to>
2. Attributes : Attributes is used to give us more information about elements. We use attribute inside the open tag of element.
Example:
<img src=\"R4Rlogo.gif\" />
In this example name of element and attribute is \"img\" and \"src\" respectively.And the value of this src attribute is \"R4Rlogo.gif\".
3. Entities : Entities are those special characters that we used in XML.We use this special characters to perform some specific task.
Some entities that we used in XML are:
Some entity reference are:
<
>
'
&
"
Some characters are:
<
>
\'
\"
&
4. PCDATA : Firstly I told you PCDATA is stands for parsed character data.
We can defined PCDATA as PCDATA is an text that is parsed through a parser.And we can analysed PCDATA text through the parer used for entities and markup.
5. CDATA : CDATA stands for character data.We can\'t parse the CDATA text through parser.Means that tags used inside CDATA text will not handle
CDATA text as markup and also we can\'t expend the entities.
We declare attribute in an DTD by using an keword called ATTLIST.
Now. I show you how to define a attribute in a DTD.
Syntax:
<!ATTLIST element-name attribute-name attribute-type default-value>
We declare attribute in DTD like that:
<!ATTLIST status type CDATA \"true\">
We declare attribute in XML like that:
<status type=\"true\" />
Default Attribute Value : You can also set the default attribute value like that:
DTD:
<!ELEMENT rectangle EMPTY>
<!ATTLIST rectangle width CDATA \"10\">
Valid XML:
<rectangle width=\"50\" />
This example set default value of rectangle is 10.
# Required : We use # Required keyword if you don\'t want to set default value of an attribute.Syntax:
<!ATTLIST element_name attribute_name attribute_type #REQUIRED>
Example:
DTD:
<!ATTLIST SSN number CDATA #REQUIRED>
Valid XML:
<SSN number=\"0007\" />
Invalid XML:
<SSN />
Where, SSN stands for Social Security Number.
#IMPLIED : Use this when don\'t want user to include attribute and also don\'t want to set default value.
Syntax
<!ATTLIST element-name attribute-name attribute-type #IMPLIED>
Example:
DTD:
<!ATTLIST contact fax CDATA #IMPLIED>
Valid XML:
<contact fax=\"091-123456\" />
Valid XML:
<contact />
#FIXED : If you want that attribute value not be changed by the users in future.Than we can perform that task with the help of FIXED keyword.
Syntax:
<!ATTLIST element_name attribute_name attribute_type #FIXED \"value\">
Example:
DTD:
<!ATTLIST indian company CDATA #FIXED \"Microsoft\">
Valid XML:
<indian company=\"R4R\" />
Invalid XML:
<indian company=\"abc\" />
Enumerated Attribute Values : Use this when we want to use some fixed values.
Syntax:
<!ATTLIST element_name attribute_name (en1|en2|..) default_value>
Example:
DTD:
<!ATTLIST payment type (check|DD|cash) \"cash\">
XML example:
<payment type=\"check\" />
or
<payment type=\"DD\" />
or
<payment type=\"cash\" />
XML doesn\'t has specific rules about where we use child elements and where we use attributes.So, we can place them as our requirement.
In support of that statement I have given you example.Which definitily suggest you where we use child elements and attributes.
Example:
<person sex=\"male\">
<firstname>Sumit</firstname>
<middlename>kumar</middlename>
<lastname>Garg</lastname>
</person>
In this example we use sex as an attribute and attribute value is \"male\".
<person>
<sex>male</sex>
<firstname>Sumit</firstname>
<middlename>Kumar</middlename>
<lastname>Garg</lastname>
</person>
In this example sex is an child element and element value is \"male\".
But I advise you to use attribues in HTML and use child elemens in case of XML.
We can say that entities are nothing other than variable we use entities to define shortname of big standard text and also use to define special character.Entity consist of these three terms &(ampersand), entitiy name and ;(semicolon).
To declare entity internally :
Syntax:
<!ENTITY entity-name \"entity-value\">
Example:
DTD Example:
<!ENTITY writer \"Micky Mouse\">
<!ENTITY copyright \"Copyright WaltDisney\">
XML example:
<author>&writer;©right;</author>
To declare entity externally :
Syntax:
<!ENTITY entity_name SYSTEM \"URI/URL\">
Example:
A DTD Example:
<!ENTITY writer SYSTEM \"http://www.r4r.co.in/entities.dtd\">
<!ENTITY copyright SYSTEM \"http://www.r4r.co.in/entities.dtd\">
A XML example:
<author>&writer;©right;</author>
We can perform validation using DTD in XML with internet explorer 5 and its higher version.
To validate XML document using XML parser : We can perorm validation using with XML parser in XML document.Vaidation performa is like that suppose that when you try to open XML document than may be XML parser can generate error.
You can findout error code, error text, or line that cause error by accessing the parseError object.
We can use load() method to validate files and use loadXML() method to validate strings.
Example:
var xmlDoc = new ActiveXObject(\"Microsoft.XMLDOM\");
xmlDoc.async=\"false\";
xmlDoc.validateOnParse=\"true\";
xmlDoc.load(\"note_dtd_error.xml\");
document.write(\"<br />Error Code: \");
document.write(xmlDoc.parseError.errorCode);
document.write(\"<br />Error Reason: \");
document.write(xmlDoc.parseError.reason);
document.write(\"<br />Error Line: \");
document.write(xmlDoc.parseError.line);
To turnoff validation : We can turnoff validation in XML document by using an code in XML Parser like: validateOnParse=\"false\".
Example:
var xmlDoc = new ActiveXObject(\"Microsoft.XMLDOM\");
xmlDoc.async=\"false\";
xmlDoc.validateOnParse=\"false\";
xmlDoc.load(\"document_dtd_error.xml\");
document.write(\"<br />Error Code: \");
document.write(xmlDoc.parseError.errorCode);
document.write(\"<br />Error Reason: \");
document.write(xmlDoc.parseError.reason);
document.write(\"<br />Error Line: \");
document.write(xmlDoc.parseError.line);