36
XML and DTD Jussi Pohjolainen TAMK University of Applied Sciences

XML and DTD

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: XML and DTD

XML and DTD

Jussi PohjolainenTAMK University of Applied Sciences

Page 2: XML and DTD

Recap from Previous Lecture

• XML – Meta Language• XML Well Formed• XML Valid

Page 3: XML and DTD

XML Predefined Entities

Entity Name Replacement as Text&lt; <&gt; >&amp; &&apos; '&quot; "

Page 4: XML and DTD

CDATA

• Character Data - used about text data that should not be parsed by the XML parser.

• Everything inside a CDATA section is ignored by the parser.– Example:• <tag>6 is &lt; 7 &amp; 7 &gt; 6</tag>

– With CDATA• <tag><![CDATA[6 is < 7 & 7 > 6]]></tag>

• Begins with "<![CDATA[" and ends with "]]>"

Page 5: XML and DTD

INTRODUCTION TO DTD

Page 6: XML and DTD

Scenario: Moving Books from Library A to Library B

Java EEJava EE PHPPHPif(is_wf("books.xml")) { save_to_DB("books.xml");}

if(is_wf("books.xml")) { save_to_DB("books.xml");}

Library A Library B

Page 7: XML and DTD

Better Way

Java EEJava EE PHPPHPif(is_wf("books.xml") and is_valid("books.xml") { save_to_DB("books.xml");}

if(is_wf("books.xml") and is_valid("books.xml") { save_to_DB("books.xml");}

Library A Library B

Page 8: XML and DTD

Creating XML languages

XMLXML

XHTML(.xhtml)

MathML(.mml)

OOXML(.docx)

Page 9: XML and DTD

DOCUMENT TYPE DEFINITION

Page 10: XML and DTD

• DTD is used both in XML and SGML• Specifying the structure and tag-names in

XML-language:– Tag names, order, amount– Attribute names and datatypes– The order of the elements

In General

Page 11: XML and DTD

• A DTD is associated with an XML document via a Document Type Declaration

• Internal vs. external subset– Internal: DTD is part of the declaration and is

embedded into the xml-document– External: DTD is in external file and declaration

links to the file

Associating DTDs with Documents

Page 12: XML and DTD

General Syntax: External Subset<?xml version="1.0"?>

<!DOCTYPE root-element [SYSTEM OR PUBLIC FPI] "uri">

<root>

<foo>...</foo>

</root>

Page 13: XML and DTD

Root Element<?xml version="1.0"?>

<!DOCTYPE root [SYSTEM OR PUBLIC FPI] "uri">

<root>

<foo>...</foo>

</root>

Page 14: XML and DTD

System or Public

• Document Type Declaration can be– System: DTD is for private system– Public: DTD is open to the public

• System<!DOCTYPE root SYSTEM "uri">

• Public<!DOCTYPE root PUBLIC FPI "uri">

Page 15: XML and DTD

Public

• If PUBLIC keyword is chosen, after declaration must have Formal Public Identifier: <!DOCTYPE root PUBLIC FPI "uri">

Page 16: XML and DTD

FPI?

• Syntax:– <!DOCTYPE HTML PUBLIC – "[registration]//[organization]//[type]– [label]//[language]" – URI>

• Example:– <!DOCTYPE HTML PUBLIC – "-//W3C//DTD– HTML 4.0 Transitional//EN" – URI>

Page 17: XML and DTD

FPI• Example:

– <!DOCTYPE ROOT PUBLIC – "[registration]//[organization]//[type]– [label]//[language]" – URI>

• Registration: + or -, – + indicates that the organization name that follows is ISO-registered.– - organization name that follows is not ISO-registered

• Organization– Organization who is responsible for the DTD

• Type– DTD

• Label– a unique descriptive name for the public text (DTD) being referenced.

• Language– The language of the xml. (en, fi)

Page 18: XML and DTD

URI

• Example:– <!DOCTYPE ROOT PUBLIC – "[registration]//[organization]//[type]– [label]//[language]" – URI>

• URI– The URI to the DTD - file

Page 19: XML and DTD

Examples<!DOCTYPE html PUBLIC

"-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<!DOCTYPE books PUBLIC

"-//TAMK//DTD MY BOOKS//EN"

"books.dtd">

<!DOCTYPE books SYSTEM

"books.dtd">

Page 20: XML and DTD

Internal DTD<?xml version="1.0"?>

<!DOCTYPE root [

<!ELEMENT root (foo)>

<!ELEMENT foo (#PCDATA)>

]>

<root>

<foo>...</foo>

</root>

Page 21: XML and DTD

External DTD: foo.xml (public)<?xml version="1.0"?>

<!DOCTYPE root PUBLIC "-//TAMK//DTD MY BOOKS//EN"

"foo.dtd">

<root>

<foo>...</foo>

</root>

Page 22: XML and DTD

External DTD: foo.xml (system)<?xml version="1.0"?>

<!DOCTYPE root SYSTEM "foo.dtd">

<root>

<foo>...</foo>

</root>

Page 23: XML and DTD

External DTD: foo.dtd<!ELEMENT root (foo)>

<!ELEMENT foo (#PCDATA)>

Page 24: XML and DTD

DTD's ELEMENT Declaration• ELEMENT defines tag-name:

– <!ELEMENT tagname (values)>• Values defines child-elements

– <!ELEMENT tagname (child1, child2)>• Or the type of the element

– <!ELEMENT tagname (type)>• Types?

– #PCDATA, ANY or EMPTY• Amount

• + = 1 - n• * = 0 - n• ? = 0 - 1

Page 25: XML and DTD

Example of ELEMENT - usage

• XML:– <student>– <name>Tiina</name>– </student>

• DTD:– <!ELEMENT student (name)>– <!ELEMENT name (#PCDATA)>

Page 26: XML and DTD

Example of ELEMENT - usage• XML

– <koululaiset>– <koululainen>– <etunimi>Tiina</etunimi>– <sukunimi>Virtanen</sukunimi>– </koululainen>– </koululaiset>

• DTD– <!ELEMENT koululaiset (koululainen)>– <!ELEMENT koululainen (etunimi, sukunimi)>– <!ELEMENT etunimi (#PCDATA)>– <!ELEMENT sukunimi (#PCDATA)>

Page 27: XML and DTD

The Amount of Elements

<!ELEMENT koululaiset (koululainen+)>

<!ELEMENT koululainen (etunimi+, sukunimi, henktunnus?)>

<!ELEMENT etunimi (#PCDATA)>

<!ELEMENT sukunimi (#PCDATA)>

<!ELEMENT henktunnus (#PCDATA)>

Page 28: XML and DTD

Alternative Elements

• Alternative elements:– <!ELEMENT materiaali (kirja | video)>

• The use of brackets:– <!ELEMENT henkilo ((etunimi+,(sukunimi, tyttönimi?))|tunnus)>

Page 29: XML and DTD

Attribute Declaration

• Attribute is defined with ATTLIST.• Attribute in XML:

– <koululainen tunnari="aaa-222">

• SYNTAX: <!ATTLIST ELEMENTNAME ATTRIBUTENAME TYPE REQUIRED>

– ELEMENTNAME: The element which the attribute is given– ATTRIBUTENAME: attribute name– TYPE: attribute contents– REQUIRED: Is the attribute mandatory or not

• Example: – <!ATTLIST koululainen tunnari ID #REQUIRED>

Page 30: XML and DTD

Attribute Types

• Attribute Datatypes– CDATA: character data– ENTITY or ENTITIES: entity defined somewhere else– ID: Unique value. Must begin with letter, underscore or

colon.– IDREF or IDREFS: Reference to ID– NMTOKEN or NMTOKENS: CDATA without spaces– NOTATION: Link to external resource

• Example– <!ATTLIST koululainen tunnari ID #REQUIRED>

Page 31: XML and DTD

Attribute Requirements

• Requirements– #REQUIRED, mandatory– #FIXED, value is fixed– #IMPLIED, optional

• Usage:– <!ATTLIST koululainen tunnari ID #REQUIRED>

Page 32: XML and DTD

Example<!ELEMENT kalenteri (tapaaminen*)><!ELEMENT tapaaminen (aika, paikka)><!ELEMENT aika (pvm , klo)><!ELEMENT paikka EMPTY><!ELEMENT klo (#PCDATA)><!ELEMENT pvm (#PCDATA)><!ATTLIST tapaaminen id ID #REQUIRED><!ATTLIST paikka yritys CDATA #REQUIRED

kaupunki (Helsinki|Tampere) "Helsinki" tila NMTOKEN #IMPLIED>

Page 33: XML and DTD

Entity in DTD's

• It's possible to create your own entities in DTD's:

• Syntax: <!ENTITY company "My Company">• Usage in XML: &company;

• Getting entity value from external file• <!ENTITY company SYSTEM "company.txt">

Page 34: XML and DTD

Is the XML Valid?• XML

– <students>– <student>– <name>Tiina Virtanen</name>– <gender>Female</gender>– </student>– </students>

• DTD– <!ELEMENT students (student)>– <!ELEMENT student (name, gender)>– <!ELEMENT name (#PCDATA)>– <!ELEMENT gender (#PCDATA)>

Page 35: XML and DTD

What About Now?• XML

– <students>– <student>– <name>Tiina Virtanen</name>– <gender>Cow</gender>– </student>– </students>

• DTD– <!ELEMENT students (student)>– <!ELEMENT student (name, gender)>– <!ELEMENT name (#PCDATA)>– <!ELEMENT gender (#PCDATA)>

Page 36: XML and DTD

DTD Critique and Schema

• The problem with DTD is that it specifies loosely the content of the elements

• Use Schema instead– Schema is a XML-based language which defines

new XML – languages– Much more specific than DTD (data types!)– Expandable

• We'll cover the basics of Schema next time...