| ||||||||
XML and DTD'sWhat is a DTD?
Why do we have DTD's?
Internal DTD'sThis is an XML document with a internal Document Type Definition.
The DTD is interpreted like this:
External DTDThis is an XML document with an external DTD:
This is what you find in "note.dtd" containing the Document Type Definition:
Why use a DTD?
DTD - XML building blocksXML documents (and HTML documents) are made up by the following building blocks:
Elements
Tags
Attributes
<img src="computer.gif" />
Entities
PCDATA
CDATA
DTD - ElementsDeclaring an Element
<!ELEMENT element-name (element-content)> Empty elements
<!ELEMENT element-name (EMPTY)> example: <!ELEMENT img (EMPTY)>
|
| Elements with data are declared with the data type inside parentheses: |
<!ELEMENT element-name (#CDATA)> or <!ELEMENT element-name (#PCDATA)> or <!ELEMENT element-name (ANY)>
example:
<!ELEMENT letter (#PCDATA)>
| #CDATA means the element contains character data that is not supposed to be parsed by a parser. | |
| #PCDATA means that the element contains data that IS going to be parsed by a parser. | |
| The keyword ANY declares an element with any content. | |
| If a #PCDATA section contains elements, these elements must also be declared. |
| Elements with one or more children are defined with the name of the children elements inside the parentheses: |
<!ELEMENT element-name (child-element-name)> or <!ELEMENT element-name (child-element-name,child-element-name,.....)>
example:
<!ELEMENT letter (to,from,heading,body)>
| When children are declared in a sequence separated by commas, the children must appear in the same sequence in the document. | |
| In a full declaration, the children must also be declared, and the children can also have children. | |
| The full declaration of the letter document will be: |
<!ELEMENT letter (to,from,subject,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT subject (#PCDATA)> <!ELEMENT body (#PCDATA)>
| If the DTD is to be included in your XML source file, it should be wrapped in a DOCTYPE definition with the following syntax: |
<!DOCTYPE root-element [element-declarations]>
example: <?xml version="1.0"?> <!DOCTYPE letter [ <!ELEMENT letter (to,from,subject,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT subject (#PCDATA)> <!ELEMENT body (#PCDATA)> ]> <letter> <to>John</to> <from>Bob</from> <subject>Something important</subject> <body>See me in the morning for the next assignment...</body> </letter>
Declaring only one occurrence of the same element
<!ELEMENT element-name (child-name)>
example
<!ELEMENT letter (message)>
| The example declaration above declares that the child element message can only occur one time inside the letter element. |
Declaring minimum one occurrence of the same element
<!ELEMENT element-name (child-name+)>
example
<!ELEMENT letter (message+)>
| The + sign in the example above declares that the child element message must occur one or more times inside the letter element. |
Declaring zero or more occurrences of the same element
<!ELEMENT element-name (child-name*)>
example
<!ELEMENT letter (message*)>
| The * sign in the example above declares that the child element message can occur zero or more times inside the letter element. |
Declaring zero or one occurrences of the same element
<!ELEMENT element-name (child-name?)>
example
<!ELEMENT letter (message?)>
| The ? sign in the example above declares that the child element message can occur zero or one times inside the letter element. |
Declaring mixed content
example
<!ELEMENT letter (to+,from,subject,message*,#PCDATA)>
| The example above declares that the element letter must contain at least one to child element, exactly one from child element, exactly one header, zero or more message, and some other parsed character data as well. |
Declaring Attributes
| In the DTD, XML element attributes are declared with an ATTLIST declaration. An attribute declaration has the following syntax: |
<!ATTLIST element-name attribute-name attribute-type default-value>
| As you can see from the syntax above, the ATTLIST declaration defines the element which can have the attribute, the name of the attribute, the type of the attribute, and the default attribute value. | |
| The attribute-type can have the following values: |
| Value | Explanation |
|---|---|
CDATA |
The value is character data |
(eval|eval|..) |
The value must be an enumerated value |
ID |
The value is an unique id |
IDREF |
The value is the id of another element |
IDREFS |
The value is a list of other ids |
NMTOKEN |
The value is a valid XML name |
NMTOKENS |
The value is a list of valid XML names |
ENTITY |
The value is an entity |
ENTITIES |
The value is a list of entities |
NOTATION |
The value is a name of a notation |
xml: |
The value is predefined |
| The attribute-default-value can have the following values: |
| Value | Explanation |
|---|---|
#DEFAULT value |
The attribute has a default value |
#REQUIRED |
The attribute value must be included in the element |
#IMPLIED |
The attribute does not have to be included |
#FIXED value |
The attribute value is fixed |
Attribute declaration example
DTD example: <!ELEMENT square EMPTY> <!ATTLIST square width CDATA "0"> XML example: <square width="100"></square>
| In the above example the element square is defined to be an empty element with the attributes width of type CDATA. The width attribute has a default value of 0. |
Default attribute value
Syntax: <!ATTLIST element-name attribute-name CDATA "default-value"> DTD example: <!ATTLIST payment type CDATA "check"> XML example: <payment type="check">
| Specifying a default value for an attribute, assures that the attribute will get a value even if the author of the XML document didn't include it. |
Implied attribute
Syntax: <!ATTLIST element-name attribute-name attribute-type #IMPLIED>
DTD example: <!ATTLIST contact fax CDATA #IMPLIED> XML example: <contact fax="555-667788">
| Use an implied attribute if you don't want to force the author to include an attribute and you don't have an option for a default value either. |
Required attribute
Syntax: <!ATTLIST element-name attribute_name attribute-type #REQUIRED>
DTD example: <!ATTLIST person number CDATA #REQUIRED> XML example: <person number="5677">
| Use a required attribute if you don't have an option for a default value, but still want to force the attribute to be present. |
Fixed attribute value
Syntax: <!ATTLIST element-name attribute-name attribute-type #FIXED "value">
DTD example: <!ATTLIST sender company CDATA #FIXED "Microsoft"> XML example: <sender company="Microsoft">
| Use a fixed attribute value when you want an attribute to have a fixed value without allowing the author to change it. If an author includes another value, the XML parser will return an error. |
Enumerated attribute values
Syntax: <!ATTLIST element-name attribute-name (eval|eval|..) default-value>
DTD example: <!ATTLIST payment type (check|cash) "cash"> XML example: <payment type="check"> or <payment type="cash">
| Use enumerated attribute values when you want the attribute values to be one of a fixed set of legal values. |
Entities
| Entities as variables used to define shortcuts to common text. | |
| Entity references are references to entities. | |
| Entities can be declared internal. | |
| Entities can be declared external |
Internal Entity Declaration
Syntax:
<!ENTITY entity-name "entity-value"> DTD Example:
<!ENTITY writer "Jan Egil Refsnes."> <!ENTITY copyright "Copyright XML101.">
XML example:
<author>&writer;©right;</author>
External Entity Declaration
Syntax:
<!ENTITY entity-name SYSTEM "URI/URL"> DTD Example:
<!ENTITY writer SYSTEM "http://www.xml101.com/entities/entities.xml"> <!ENTITY copyright SYSTEM "http://www.xml101.com/entities/entities.dtd">
XML example:
<author>&writer;©right;</author>
Validating with the XML Parser
| If you try to open an XML document, the XML Parser might generate an error. | |
| By accessing the parseError object, the exact error code, the error text, and even the line that caused the error can be retrieved: |
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)
Turning Validation off
| Validation can be turned off by setting the XML parser's validateOnParse="false". |
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.validateOnParse="false"
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)
By David Moisan. Copied from: http://www1.shore.net/~dmoisan/
<!DOCTYPE TVSCHEDULE [
<!ATTLIST TVSCHEDULE NAME CDATA #REQUIRED> ]> |
By Richard Erlander. Copied from: http://pdbeam.uwaterloo.ca/~rlander/
|
Copied from http://www.vervet.com/
|
Copied from http://www.vervet.com/
<!DOCTYPE CATALOG [
]> |
|
Copyright © 1999
- 2005 |