Upload
cameron-watson
View
249
Download
1
Embed Size (px)
DESCRIPTION
Introduction to DTD What is DTD? DTD stands for Document Type Definition DTD is a schema specification method for XML documents. DTD states what elements and attributes are used to describe content in an XML document, where each element is allowed, and which elements can appear within other elements.
Citation preview
DTD
Document Type Definition
Agenda
Introduction to DTD DTD Building Blocks DTD Elements DTD Attributes DTD Entities DTD Exercises DTD Q&A
Introduction to DTD What is DTD?
DTD stands for Document Type DefinitionDTD is a schema specification method for XML
documents.DTD states what elements and attributes are
used to describe content in an XML document, where each element is allowed, and which elements can appear within other elements.
Introduction to DTD Why use a DTD?
Each of your XML files can carry a description of its own format.
Independent groups of people can agree to use a standard DTD for interchanging data.
Application can use a standard DTD to verify that the data you receive from the outside world is valid.
Introduction to DTD Internal DTD Declaration
A DTD which declare inside an XML document call inline DTD declaration.
It should be wrapped in a DOCTYPE definition.<!DOCTYPE root-element [
element-declarations]>
<?xml version="1.0"?> <!DOCTYPE note [
<!ELEMENT note (message)> <!ELEMENT message (#PCDATA)>
]> <note>
<message>Welcome to the DTD world</message> </note>
Introduction to DTD External DTD Declaration
A DTD which declare in an external file (.dtd file) and call it inside an XML document.
<!DOCTYPE root-element SYSTEM "filename">
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note> <message>Welcome to the DTD world</message>
</note>
DTD Building Blocks Building blocks in an XML document
Elements Elements are the main building blocks of both XML and
HTML documents. Attributes
Attributes provide extra information about elements. Entities
Special characters in XML PCDATA
PCDATA is text that WILL be parsed by a parser. CDATA
CDATA is text that will NOT be parsed by a parser.
DTD Elements Declaring Elements
In DTD, elements are declared with an ELEMENT declaration.
<!ELEMENT element-name category> or <!ELEMENT element-name (element-content)>
Empty ElementsEmpty elements are declared with the category
keyword EMPTY. <!ELEMENT element-name EMPTY>Example:
<!ELEMENT br EMPTY>XML example:
<br />
DTD Elements Elements with Parsed Character Data
Elements with only parsed character data are declared with #PCDATA inside parentheses.
<!ELEMENT element-name (#PCDATA)> Example:
<!ELEMENT from (#PCDATA)>
PCDATA - Parsed Character DataPCDATA is text that WILL be parsed by a
parser. The text will be examined by the parser for
entities and markup.
DTD Elements Elements with any Contents
Elements declared with the category keyword ANY, can contain any combination of parsable data
<!ELEMENT element-name ANY>Example:
<!ELEMENT note ANY>
Elements with sub-elements (Children) Elements with one or more children are declared with
the name of the children elements inside parentheses<!ELEMENT element-name (child1)>or<!ELEMENT element-name (child1,child2,...)>
Example:<!ELEMENT note (to,from,heading,body)>
DTD Elements Declaring Only One Occurrence of an Element
The + sign in the example bellow declares that the child element "message" must occur one or more times inside the "note" element
<!ELEMENT element-name (child-name+)>Example:
<!ELEMENT note (message+)>
Declaring Zero or More Occurrences of an Element The * sign in the example bellow declares that the child
element "message" can occur zero or more times inside the "note" element.
<!ELEMENT element-name (child-name*)>Example:
<!ELEMENT note (message*)>
DTD Elements Declaring Zero or One Occurrences of an Element
The ? sign in the example bellow declares that the child element "message" can occur zero or one time inside the "note" element.
<!ELEMENT element-name (child-name?)>Example:
<!ELEMENT note (message?)>
Declaring either/or Content The example bellow declares that the "note" element
must contain a "to" element, a "from" element, a "header" element, and either a "message" or a "body" element.
Example:<!ELEMENT note (to,from,header,(message|body))>
DTD Elements Declaring Mixed Content
The example above declares that the "note" element can contain zero or more occurrences of parsed character data, "to", "from", "header", or "message" elements.
Example:<!ELEMENT note (#PCDATA|to|from|header|message)*>
DTD ElementsDescription Syntax
Empty Elements <!ELEMENT element-name EMPTY> Elements with Parsed Character Data
<!ELEMENT element-name (#PCDATA)>
Elements with any Contents <!ELEMENT element-name ANY>
Elements with Children <!ELEMENT element-name (child1)> or <!ELEMENT element-name (child1,child2,.)>
Declaring Only One Occurrence <!ELEMENT element-name (child-name)> Declaring Minimum One Occurrence
+
Declaring Zero or More Occurrences
*
Declaring Zero or One Occurrences ?Declaring either/or Content |
DTD Attributes Declaring Attributes
In a DTD, attributes are declared with an ATTLIST declaration.
<!ATTLIST element-name attribute-name attribute-type default-value>DTD example:
<!ATTLIST payment type CDATA "check"> XML example:
<payment type="check" />
DTD Attributes attribute-type
Type DescriptionCDATA The value is character data(en1|en2|..) The value must be one from an enumerated listID The value is a unique idIDREF The value is the id of another elementIDREFS The value is a list of other idsNMTOKEN The value is a valid XML nameNMTOKENS The value is a list of valid XML namesENTITY The value is an entityENTITIES The value is a list of entitiesNOTATION The value is a name of a notationxml: The value is a predefined xml value
DTD Attributes default-value
Value Explanationvalue The default value of the
attribute#REQUIRED The attribute is required#IMPLIED The attribute is not
required#FIXED value The attribute value is
fixed
DTD Attributes A Default Attribute Value
DTD: <!ELEMENT square EMPTY> <!ATTLIST square width CDATA "0">
Valid XML: <square width="100" />
If no width is specified, it has a default value of 0.
DTD Attributes #REQUIRED
Use the #REQUIRED keyword if you don't have an option for a default value, but still want to force the attribute to be present.
DTD: <!ATTLIST person number CDATA #REQUIRED>
Valid XML: <person number="5677" />
Invalid XML: <person />
DTD Attributes #IMPLIED
Use the #IMPLIED keyword if you don't want to force the author to include an attribute, and you don't have an option for a default value.
DTD: <!ATTLIST contact fax CDATA #IMPLIED>
Valid XML: <contact fax="555-667788" />
Valid XML: <contact />
DTD Attributes #FIXED
Use the #FIXED keyword 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.
DTD: <!ATTLIST sender company CDATA #FIXED "Microsoft">
Valid XML: <sender company="Microsoft" />
Invalid XML: <sender company="W3Schools" />
DTD Attributes Enumerated Attribute Values
Use enumerated attribute values when you want the attribute value to be one of a fixed set of legal values.
DTD: <!ATTLIST payment type (check|cash) "cash">
XML example: <payment type="check" /> or <payment type="cash" />
DTD Entities Entities are variables used to define
shortcuts to standard text or special characters.
Entity references are references to entities Entities can be declared internal or external An entity has three parts: an ampersand (&),
an entity name, and a semicolon (;).
DTD Entities An Internal Entity Declaration
DTD Example:<!ENTITY writer "Donald Duck."><!ENTITY copyright "Copyright W3Schools.">
XML example:<author>&writer;©right;</author>
An External Entity DeclarationDTD Example:
<!ENTITY writer SYSTEM "http://www.w3schools.com/entities.dtd">
<!ENTITY copyright SYSTEM "http://www.w3schools.com/entities.dtd">
XML example:<author>&writer;©right;</author>
DTD Exercises Write an XML document for following DTD.
Note: Consider this DTD as an external DTD.<?xml version="1.0" encoding="ISO-8859-1"?>
<!ELEMENT recipebook (recipe)+ ><!ELEMENT recipe (recipetype, title, ingredient+, step+) >
<!ELEMENT title (#PCDATA) > <!ELEMENT ingredient (#PCDATA) > <!ELEMENT step (#PCDATA) > <!ATTLIST step number CDATA #REQUIRED > <!ELEMENT recipetype EMPTY > <!ATTLIST recipetype name CDATA #REQUIRED >
DTD Exercises Write a DTD for following XML document.
<?xml version="1.0"?> <!DOCTYPE PARTS SYSTEM "parts.dtd"> <PARTS>
<TITLE>Computer Parts</TITLE> <PART>
<ITEM>Motherboard</ITEM> <MANUFACTURER>ASUS</MANUFACTURER> <MODEL>P3B-F</MODEL> <COST> 123.00</COST>
</PART> <PART>
<ITEM>Video Card</ITEM> <MANUFACTURER>ATI</MANUFACTURER> <MODEL>All-in-Wonder Pro</MODEL> <COST> 160.00</COST>
</PART> </PARTS>
DTD Q&A