Saturday, July 24, 2010

DTD

All text are from http://www.w3schools.com/dtd


A Document Type Definition (DTD) defines the legal building blocks of an XML document. It defines the document structure with a list of legal elements and attributes.

A DTD can be declared:
  • inside XML
  • external reference
DTD is not XML document.

DTD inside:
 

DTD external
 


Why is DTD useful

  • With a DTD, each of your XML files can carry a description of its own format.
  • With a DTD, independent groups of people can agree to use a standard DTD for interchanging data.
  • Your application can use a standard DTD to verify that the data you receive from the outside world is valid.
  • You can also use a DTD to verify your own data.

Bulding blocks in XML

  • Elements
  • Attributes
  • Entities
  • PCDATA
  • CDATA
Entities
The following entities are predefined in XML:
Entity ReferencesCharacter
&lt<
&lt<
&amp&
""
''


PCDATA
PCDATA means parsed character data.
PCDATA is text that will be parsed by a parser. The text will be examined by the parser for entities and markup.

CDATA
CDATA means character data.
CDATA is text that will not be parsed by a parser. Tags inside the text will NOT be treated as markup and entities will not be expanded.

Elements

Declaring
<!ELEMENT element-name category>
or
<!ELEMENT element-name (element-content)>

Empty elements
<!ELEMENT br EMPTY>

Elements with PCDATA
<!ELEMENT from (#PCDATA)>

Element with children
<!ELEMENT note (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 "note" element is:

<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

One occurence
<!ELEMENT note (message)>

One and more
<!ELEMENT note (message+)>

Zero and more
<!ELEMENT note (message*)>

Zero or one
<!ELEMENT note (message?)>

Either/ or content
<!ELEMENT note (to,from,header,(message|body))>

Mixed content
<!ELEMENT note (#PCDATA|to|from|header|message)*>

Attributes

Declaring attributes
<! ATTLIST element-name attribute-name attribute-type default-value>

Example:
<!ATTLIST payment type CDATA "check">

The attribute-type can be one of the following:
TypeDescription
CDATAThe value is character data
(en1|en2|..)The value must be one from an enumerated list
IDThe value is a unique id
IDREFThe value is the id of another element
IDREFSThe value is a list of other ids
NMTOKENThe value is a valid XML name
NMTOKENSThe value is a list of valid XML names
ENTITYThe value is an entity
ENTITIESThe value is a list of entities
NOTATIONThe value is a name of a notation
xml:The value is a predefined xml value


The default-value can be one of the following:
ValueExplanation
valueThe default value of the attribute
#REQUIREDThe attribute is required
#IMPLIEDThe attribute is not required
#FIXED valueThe attribute value is fixed


Default attribute value
<!ELEMENT square EMPTY>
<!ATTLIST square width CDATA "0">

In the example above, the "square" element is defined to be an empty element with a "width" attribute of  type CDATA. If no width is specified, it has a default value of 0.

Enumerated values
<!ATTLIST payment type (check|cash) "cash">

Example:
<payment type="check" />
or
<payment type="cash" />

Why is elements better then attributes

Data can be stored in child elements or in attributes.

Attribute:
 

Element:
 

Some of the problems with attributes are:
  • attributes cannot contain multiple values (child elements can)
  • attributes are not easily expandable (for future changes)
  • attributes cannot describe structures (child elements can)
  • attributes are more difficult to manipulate by program code
  • attribute values are not easy to test against a DTD
Use attributes only to provide information that is not relevant to the data.

Example:
 


Entities

Entities are variables used to define shortcuts to standard text or special characters.   
  • Entities can be declared internal or external
Internal
<!ENTITY entity-name "entity-value">

Example
<!ENTITY copyright "Copyright W3Schools.">

External
<!ENTITY entity-name SYSTEM "URI/URL">

Example
<!ENTITY writer SYSTEM "http://www.w3schools.com/entities.dtd">


No comments:

Post a Comment