11
1/11 ITApplications ITApplications XML Module XML Module Session 3: Session 3: Document Type Definition Document Type Definition (DTD) Part 1 (DTD) Part 1

1/11 ITApplications XML Module Session 3: Document Type Definition (DTD) Part 1

Embed Size (px)

Citation preview

1/11

ITApplications ITApplications XML ModuleXML Module

Session 3: Session 3: Document Type Definition (DTD) Document Type Definition (DTD)

Part 1Part 1

2/11

What is a DTDWhat is a DTD

Document Type DefinitionDocument Type Definition Sets the rules for structure and type of Sets the rules for structure and type of

content for an xml document, ensuring content for an xml document, ensuring “validity”“validity”

Has its own syntax – but is not XMLHas its own syntax – but is not XML Syntax derived from Syntax derived from Standard Generalized Standard Generalized

Markup Language (SGML)Markup Language (SGML)

3/11

Sample DTDSample DTD<?xml version="1.0" encoding="UTF-8"?><!ELEMENT photos (photoItem*)><!ELEMENT photoItem (title, text?, captions)><!ATTLIST photoItem

ref ID #REQUIREDfolder NMTOKEN #REQUIREDtype (jpg | gif | png) "jpg"

><!ELEMENT title (#PCDATA)><!ELEMENT text (p+ | ul*)*><!ELEMENT p (#PCDATA)*><!ELEMENT captions (caption+)><!ELEMENT caption (#PCDATA)><!ELEMENT ul (#PCDATA)>

Filename: Photos.dtd

* Note: NMTOKEN stands for Name Token and indicates that the value of the element may NOT have spaces in it and must conform to XML elements naming conventions (i.e. elements can only start with a letter, an underscore or a colon).

4/11

Corresponding sample XML documentCorresponding sample XML document

<?xml version="1.0" encoding="UTF-8"?><?xml version="1.0" encoding="UTF-8"?><!DOCTYPE photos SYSTEM "Photos.dtd"><!DOCTYPE photos SYSTEM "Photos.dtd"><photos><photos>

<photoItem ref="logo" folder="images" type="jpg"><photoItem ref="logo" folder="images" type="jpg"><title>Logo</title><title>Logo</title><text><text>

<p>My company logo image</p><p>My company logo image</p></text></text><captions><captions>

<caption>Company Logo</caption><caption>Company Logo</caption></captions></captions>

</photoItem></photoItem></photos></photos>

Filename: Photos.xmlFilename: Photos.xml

5/11

DTD Syntax – ElementsDTD Syntax – Elements To specify an element:To specify an element:

<!ELEMENT [element_name] [child_definitions]><!ELEMENT [element_name] [child_definitions]>

An element name must be a valid XML nameAn element name must be a valid XML name

The child definitions can be as follows:The child definitions can be as follows: #PCDATA (Parsed character data)#PCDATA (Parsed character data)

Example: <!ELEMENT title (#PCDATA)> Example: <!ELEMENT title (#PCDATA)>

Child ElementsChild ElementsExample: <!ELEMENT captions (caption)>Example: <!ELEMENT captions (caption)>

SequencesSequencesExample: <!ELEMENT photoItem (title, text?, captions)>Example: <!ELEMENT photoItem (title, text?, captions)>

6/11

Number of childrenNumber of children?? Zero or one of the element Zero or one of the element* Zero or more of the element* Zero or more of the element+ One or more of the element+ One or more of the element

Example:Example:

<?xml version="1.0" encoding="UTF-8"?><?xml version="1.0" encoding="UTF-8"?><!ELEMENT customerorder (companyname, mailingaddress, <!ELEMENT customerorder (companyname, mailingaddress,

billingaddress?, orderitem+)>billingaddress?, orderitem+)><!ELEMENT mailingaddress (addressline+, direction*)><!ELEMENT mailingaddress (addressline+, direction*)><!ELEMENT billingaddress (addressline)><!ELEMENT billingaddress (addressline)><!ELEMENT companyname (#PCDATA)><!ELEMENT companyname (#PCDATA)><!ELEMENT addressline (#PCDATA)><!ELEMENT addressline (#PCDATA)><!ELEMENT direction (#PCDATA)><!ELEMENT direction (#PCDATA)><!ELEMENT orderitem (#PCDATA)><!ELEMENT orderitem (#PCDATA)>

Filename: CustomerOrders.dtdFilename: CustomerOrders.dtd

7/11

Sample XML document:Sample XML document:

<?xml version="1.0" encoding="UTF-8"?><?xml version="1.0" encoding="UTF-8"?><!DOCTYPE customerorder SYSTEM “CustomerOrders.dtd"><!DOCTYPE customerorder SYSTEM “CustomerOrders.dtd"><customerorder><customerorder>

<companyname>Despatch International Ltd</companyname><companyname>Despatch International Ltd</companyname><mailingaddress><mailingaddress>

<addressline>1265 Manor Street</addressline><addressline>1265 Manor Street</addressline><addressline>Berkley, CA 94710</addressline><addressline>Berkley, CA 94710</addressline><direction>Take right turning at Hollow Way</direction><direction>Take right turning at Hollow Way</direction><direction>Turn left at notting hill junction</direction><direction>Turn left at notting hill junction</direction>

</mailingaddress></mailingaddress><orderitem>XML How To Program Deitel</orderitem><orderitem>XML How To Program Deitel</orderitem><orderitem>CD Album Blue 2004</orderitem><orderitem>CD Album Blue 2004</orderitem>

</customerorder></customerorder>

Filename: Filename: CustomerOrders.xmlCustomerOrders.xml

8/11

DTD Syntax – ElementsDTD Syntax – Elements

Choices and grouping (parentheses)Choices and grouping (parentheses)<!ELEMENT employee (firstName, lastName, (employeeid | <!ELEMENT employee (firstName, lastName, (employeeid |

nationalinsurance))>nationalinsurance))>

Empty ElementsEmpty Elements<!ELEMENT img EMPTY><!ELEMENT img EMPTY>

XML examples: XML examples:

<img></img> <img></img>

or or

<img /><img />

9/11

Mixed contentMixed content DTDDTD

<?xml version="1.0" encoding="UTF-8"?><?xml version="1.0" encoding="UTF-8"?><!ELEMENT summarymessage (#PCDATA | warningmessage)*><!ELEMENT summarymessage (#PCDATA | warningmessage)*><!ELEMENT warningmessage (#PCDATA)> <!ELEMENT warningmessage (#PCDATA)>

Filename: Filename: WarningMessages.dtdWarningMessages.dtd

Sample XML document:Sample XML document:

<?xml version="1.0" encoding="UTF-8"?><?xml version="1.0" encoding="UTF-8"?><!DOCTYPE summarymessage SYSTEM <!DOCTYPE summarymessage SYSTEM ""WarningMessages.dtd">WarningMessages.dtd"><summarymessage>You have entered invalid values in the fields <summarymessage>You have entered invalid values in the fields

below:below:<warningmessage>Start date must be greater than 31-Jan-<warningmessage>Start date must be greater than 31-Jan-2006</warningmessage>2006</warningmessage><warningmessage>End date must be less or equal to 31-Jan-<warningmessage>End date must be less or equal to 31-Jan-

2007 2007 </warningmessage></warningmessage></summarymessage></summarymessage>

Filename: Filename: WarningMessages.xmlWarningMessages.xml

10/11

DTD Syntax – AttributesDTD Syntax – Attributes To specify an attribute for an element, you must first To specify an attribute for an element, you must first

define the element, then:define the element, then: DTD Syntax: <!ATTLIST [element_name] [attribute_list]>DTD Syntax: <!ATTLIST [element_name] [attribute_list]>

Example DTD: <!ATTLIST client id CDATA #REQUIRED>Example DTD: <!ATTLIST client id CDATA #REQUIRED>Sample XML: <client id=“NI1234”></client>Sample XML: <client id=“NI1234”></client>

The attribute list is:The attribute list is: [attribute_name] [attribute_type] [attribute_default][attribute_name] [attribute_type] [attribute_default]

An attribute name must be a valid XML nameAn attribute name must be a valid XML name

An attribute type can be:An attribute type can be: CDATA – any string of textCDATA – any string of text NMTOKEN – a name tokenNMTOKEN – a name token NMTOKENS – sequence of name tokens separated by spacesNMTOKENS – sequence of name tokens separated by spaces ID – a unique reference for the element in the documentID – a unique reference for the element in the document

11/11

DTD Syntax – AttributesDTD Syntax – Attributes IDREF – a reference to IDIDREF – a reference to ID IDREFS – sequence of IDREF separated by spacesIDREFS – sequence of IDREF separated by spaces EnumerationEnumeration

Example DTD:Example DTD:<!ATTLIST date month (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) <!ATTLIST date month (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) #REQUIRED>#REQUIRED>

Sample XML:Sample XML:<date month=“Jan”></date><date month=“Jan”></date>

Attribute defaults can be:Attribute defaults can be: #IMPLIED – the attribute is optional#IMPLIED – the attribute is optional #REQUIRED – the attribute must be set#REQUIRED – the attribute must be set #FIXED – the attribute can only have the specified value#FIXED – the attribute can only have the specified value Default value specified in quotesDefault value specified in quotes

Example DTD:Example DTD:

<!ATTLIST img width CDATA “40”><!ATTLIST img width CDATA “40”>Sample XML:Sample XML:

<img width=“40”></img><img width=“40”></img>