Your SlideShare is downloading. ×
0
XML and DTD Jussi Pohjolainen TAMK University of Applied Sciences
Recap from Previous Lecture <ul><li>XML – Meta Language </li></ul><ul><li>XML Well Formed </li></ul><ul><li>XML Valid </li...
XML Predefined Entities Entity Name Replacement as Text &lt; < &gt; > &amp; & &apos; ' &quot; &quot;
CDATA <ul><li>Character Data - used about text data that should not be parsed by the XML parser.  </li></ul><ul><li>Everyt...
INTRODUCTION TO DTD
Scenario:  Moving Books from Library A to Library B Java EE PHP if(is_wf(&quot;books.xml&quot;)) { save_to_DB(&quot;books....
Better Way Java EE PHP if(is_wf(&quot;books.xml&quot;) and is_valid(&quot;books.xml&quot;) { save_to_DB(&quot;books.xml&qu...
Creating XML languages XML XHTML (.xhtml) MathML (.mml) OOXML (.docx)
DOCUMENT TYPE DEFINITION
<ul><li>DTD is used both in XML and SGML </li></ul><ul><li>Specifying the structure and tag-names in XML-language: </li></...
<ul><li>A DTD is associated with an XML document via a  Document Type Declaration </li></ul><ul><li>Internal vs. external ...
General Syntax: External Subset <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><!DOCTYPE root-element [SYSTEM O...
Root Element <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><!DOCTYPE  root  [SYSTEM OR PUBLIC FPI] &quot;uri&q...
System or Public <ul><li>Document Type Declaration can be </li></ul><ul><ul><li>System: DTD is for private system </li></u...
Public <ul><li>If PUBLIC keyword is chosen, after declaration must have  F ormal  P ublic  I dentifier: </li></ul><ul><ul>...
FPI? <ul><li>Syntax: </li></ul><ul><ul><li><!DOCTYPE HTML PUBLIC  </li></ul></ul><ul><ul><li>&quot;[registration]//[organi...
FPI <ul><li>Example: </li></ul><ul><ul><li><!DOCTYPE ROOT PUBLIC  </li></ul></ul><ul><ul><li>&quot;[registration]//[organi...
URI <ul><li>Example: </li></ul><ul><ul><li><!DOCTYPE ROOT PUBLIC  </li></ul></ul><ul><ul><li>&quot;[registration]//[organi...
Examples <ul><ul><li><!DOCTYPE html PUBLIC  </li></ul></ul><ul><ul><li>&quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; </li><...
Internal DTD <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><!DOCTYPE root [  </li></ul><ul><li><!ELEMENT root ...
External DTD: foo.xml (public) <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><!DOCTYPE root PUBLIC &quot;-//TA...
External DTD: foo.xml (system) <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><!DOCTYPE root SYSTEM &quot;foo.d...
External DTD: foo.dtd <ul><li><!ELEMENT root (foo)> </li></ul><ul><li><!ELEMENT foo (#PCDATA)> </li></ul>
DTD's ELEMENT Declaration <ul><li>ELEMENT defines tag-name: </li></ul><ul><ul><li><!ELEMENT tagname (values)> </li></ul></...
Example of ELEMENT - usage <ul><li>XML: </li></ul><ul><ul><li><student> </li></ul></ul><ul><ul><li><name>Tiina</name> </li...
Example of ELEMENT - usage <ul><li>XML </li></ul><ul><ul><li><koululaiset> </li></ul></ul><ul><ul><li><koululainen> </li><...
The Amount of Elements <ul><li><!ELEMENT  koululaiset (koululainen+)> </li></ul><ul><li><!ELEMENT  koululainen (etunimi+, ...
Alternative Elements <ul><li>Alternative elements: </li></ul><ul><ul><li><!ELEMENT materiaali (kirja | video)> </li></ul><...
Attribute Declaration <ul><li>Attribute is defined with ATTLIST. </li></ul><ul><li>Attribute in XML: </li></ul><ul><ul><li...
Attribute Types <ul><li>Attribute Datatypes </li></ul><ul><ul><li>CDATA : character data </li></ul></ul><ul><ul><li>ENTITY...
Attribute Requirements <ul><li>Requirements </li></ul><ul><ul><li>#REQUIRED , mandatory </li></ul></ul><ul><ul><li>#FIXED ...
Example <ul><li><!ELEMENT kalenteri (tapaaminen*)> </li></ul><ul><li><!ELEMENT tapaaminen (aika, paikka)> </li></ul><ul><l...
Entity in DTD's <ul><li>It's possible to create your own entities in DTD's: </li></ul><ul><ul><ul><li>Syntax:  <! ENTITY  ...
Is the XML Valid? <ul><li>XML </li></ul><ul><ul><li><students> </li></ul></ul><ul><ul><li><student> </li></ul></ul><ul><ul...
What About Now? <ul><li>XML </li></ul><ul><ul><li><students> </li></ul></ul><ul><ul><li><student> </li></ul></ul><ul><ul><...
DTD Critique and Schema <ul><li>The problem with DTD is that it specifies loosely the content of the elements </li></ul><u...
Upcoming SlideShare
Loading in...5
×

XML and DTD

3,717

Published on

Published in: Technology, News & Politics
2 Comments
4 Likes
Statistics
Notes
No Downloads
Views
Total Views
3,717
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
244
Comments
2
Likes
4
Embeds 0
No embeds

No notes for slide

Transcript of "XML and DTD"

  1. 1. XML and DTD Jussi Pohjolainen TAMK University of Applied Sciences
  2. 2. Recap from Previous Lecture <ul><li>XML – Meta Language </li></ul><ul><li>XML Well Formed </li></ul><ul><li>XML Valid </li></ul>
  3. 3. XML Predefined Entities Entity Name Replacement as Text &lt; < &gt; > &amp; & &apos; ' &quot; &quot;
  4. 4. CDATA <ul><li>Character Data - used about text data that should not be parsed by the XML parser. </li></ul><ul><li>Everything inside a CDATA section is ignored by the parser. </li></ul><ul><ul><li>Example: </li></ul></ul><ul><ul><ul><li><tag>6 is &lt; 7 &amp; 7 &gt; 6</tag> </li></ul></ul></ul><ul><ul><li>With CDATA </li></ul></ul><ul><ul><ul><li><tag> <![CDATA[ 6 is < 7 & 7 > 6 ]]> </tag> </li></ul></ul></ul><ul><li>Begins with &quot;<![CDATA[&quot; and ends with &quot;]]>&quot; </li></ul>
  5. 5. INTRODUCTION TO DTD
  6. 6. Scenario: Moving Books from Library A to Library B Java EE PHP if(is_wf(&quot;books.xml&quot;)) { save_to_DB(&quot;books.xml&quot;); } Library A Library B
  7. 7. Better Way Java EE PHP if(is_wf(&quot;books.xml&quot;) and is_valid(&quot;books.xml&quot;) { save_to_DB(&quot;books.xml&quot;); } Library A Library B
  8. 8. Creating XML languages XML XHTML (.xhtml) MathML (.mml) OOXML (.docx)
  9. 9. DOCUMENT TYPE DEFINITION
  10. 10. <ul><li>DTD is used both in XML and SGML </li></ul><ul><li>Specifying the structure and tag-names in XML-language: </li></ul><ul><ul><li>Tag names, order, amount </li></ul></ul><ul><ul><li>Attribute names and datatypes </li></ul></ul><ul><ul><li>The order of the elements </li></ul></ul>In General
  11. 11. <ul><li>A DTD is associated with an XML document via a Document Type Declaration </li></ul><ul><li>Internal vs. external subset </li></ul><ul><ul><li>Internal : DTD is part of the declaration and is embedded into the xml-document </li></ul></ul><ul><ul><li>External : DTD is in external file and declaration links to the file </li></ul></ul>Associating DTDs with Documents
  12. 12. General Syntax: External Subset <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><!DOCTYPE root-element [SYSTEM OR PUBLIC FPI] &quot;uri&quot;> </li></ul><ul><li><root> </li></ul><ul><li><foo>...</foo> </li></ul><ul><li></root> </li></ul>
  13. 13. Root Element <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><!DOCTYPE root [SYSTEM OR PUBLIC FPI] &quot;uri&quot;> </li></ul><ul><li>< root > </li></ul><ul><li><foo>...</foo> </li></ul><ul><li></root> </li></ul>
  14. 14. System or Public <ul><li>Document Type Declaration can be </li></ul><ul><ul><li>System: DTD is for private system </li></ul></ul><ul><ul><li>Public: DTD is open to the public </li></ul></ul><ul><li>System </li></ul><ul><ul><li><!DOCTYPE root SYSTEM &quot;uri&quot;> </li></ul></ul><ul><li>Public </li></ul><ul><ul><li><!DOCTYPE root PUBLIC FPI &quot;uri&quot;> </li></ul></ul>
  15. 15. Public <ul><li>If PUBLIC keyword is chosen, after declaration must have F ormal P ublic I dentifier: </li></ul><ul><ul><li><!DOCTYPE root PUBLIC FPI &quot;uri&quot;> </li></ul></ul>
  16. 16. FPI? <ul><li>Syntax: </li></ul><ul><ul><li><!DOCTYPE HTML PUBLIC </li></ul></ul><ul><ul><li>&quot;[registration]//[organization]//[type] </li></ul></ul><ul><ul><li>[label]//[language]&quot; </li></ul></ul><ul><ul><li>URI > </li></ul></ul><ul><li>Example: </li></ul><ul><ul><li><!DOCTYPE HTML PUBLIC </li></ul></ul><ul><ul><li>&quot;-//W3C//DTD </li></ul></ul><ul><ul><li>HTML 4.0 Transitional//EN&quot; </li></ul></ul><ul><ul><li>URI > </li></ul></ul>
  17. 17. FPI <ul><li>Example: </li></ul><ul><ul><li><!DOCTYPE ROOT PUBLIC </li></ul></ul><ul><ul><li>&quot;[registration]//[organization]//[type] </li></ul></ul><ul><ul><li>[label]//[language]&quot; </li></ul></ul><ul><ul><li>URI > </li></ul></ul><ul><li>Registration: + or -, </li></ul><ul><ul><li>+ indicates that the organization name that follows is ISO-registered. </li></ul></ul><ul><ul><li>- organization name that follows is not ISO-registered </li></ul></ul><ul><li>Organization </li></ul><ul><ul><li>Organization who is responsible for the DTD </li></ul></ul><ul><li>Type </li></ul><ul><ul><li>DTD </li></ul></ul><ul><li>Label </li></ul><ul><ul><li>a unique descriptive name for the public text (DTD) being referenced. </li></ul></ul><ul><li>Language </li></ul><ul><ul><li>The language of the xml. (en, fi) </li></ul></ul>
  18. 18. URI <ul><li>Example: </li></ul><ul><ul><li><!DOCTYPE ROOT PUBLIC </li></ul></ul><ul><ul><li>&quot;[registration]//[organization]//[type] </li></ul></ul><ul><ul><li>[label]//[language]&quot; </li></ul></ul><ul><ul><li>URI > </li></ul></ul><ul><li>URI </li></ul><ul><ul><li>The URI to the DTD - file </li></ul></ul>
  19. 19. Examples <ul><ul><li><!DOCTYPE html PUBLIC </li></ul></ul><ul><ul><li>&quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; </li></ul></ul><ul><ul><li>&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;> </li></ul></ul><ul><ul><li><!DOCTYPE books PUBLIC </li></ul></ul><ul><ul><li>&quot;-//TAMK//DTD MY BOOKS//EN&quot; </li></ul></ul><ul><ul><li>&quot;books.dtd&quot;> </li></ul></ul><ul><ul><li><!DOCTYPE books SYSTEM </li></ul></ul><ul><ul><li>&quot;books.dtd&quot;> </li></ul></ul>
  20. 20. Internal DTD <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><!DOCTYPE root [ </li></ul><ul><li><!ELEMENT root (foo)> </li></ul><ul><li><!ELEMENT foo (#PCDATA)> </li></ul><ul><li>]> </li></ul><ul><li><root> </li></ul><ul><li><foo>...</foo> </li></ul><ul><li></root> </li></ul>
  21. 21. External DTD: foo.xml (public) <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><!DOCTYPE root PUBLIC &quot;-//TAMK//DTD MY BOOKS//EN&quot; </li></ul><ul><li>&quot;foo.dtd&quot;> </li></ul><ul><li><root> </li></ul><ul><li><foo>...</foo> </li></ul><ul><li></root> </li></ul>
  22. 22. External DTD: foo.xml (system) <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><!DOCTYPE root SYSTEM &quot;foo.dtd&quot;> </li></ul><ul><li><root> </li></ul><ul><li><foo>...</foo> </li></ul><ul><li></root> </li></ul>
  23. 23. External DTD: foo.dtd <ul><li><!ELEMENT root (foo)> </li></ul><ul><li><!ELEMENT foo (#PCDATA)> </li></ul>
  24. 24. DTD's ELEMENT Declaration <ul><li>ELEMENT defines tag-name: </li></ul><ul><ul><li><!ELEMENT tagname (values)> </li></ul></ul><ul><li>Values defines child-elements </li></ul><ul><ul><li><!ELEMENT tagname (child1, child2)> </li></ul></ul><ul><li>Or the type of the element </li></ul><ul><ul><li><!ELEMENT tagname (type)> </li></ul></ul><ul><li>Types? </li></ul><ul><ul><li>#PCDATA, ANY or EMPTY </li></ul></ul><ul><li>Amount </li></ul><ul><ul><ul><li>+ = 1 - n </li></ul></ul></ul><ul><ul><ul><li>* = 0 - n </li></ul></ul></ul><ul><ul><ul><li>? = 0 - 1 </li></ul></ul></ul>
  25. 25. Example of ELEMENT - usage <ul><li>XML: </li></ul><ul><ul><li><student> </li></ul></ul><ul><ul><li><name>Tiina</name> </li></ul></ul><ul><ul><li></student> </li></ul></ul><ul><li>DTD: </li></ul><ul><ul><li><!ELEMENT student (name)> </li></ul></ul><ul><ul><li><!ELEMENT name (#PCDATA)> </li></ul></ul>
  26. 26. Example of ELEMENT - usage <ul><li>XML </li></ul><ul><ul><li><koululaiset> </li></ul></ul><ul><ul><li><koululainen> </li></ul></ul><ul><ul><li><etunimi>Tiina</etunimi> </li></ul></ul><ul><ul><li><sukunimi>Virtanen</sukunimi> </li></ul></ul><ul><ul><li></koululainen> </li></ul></ul><ul><ul><li></koululaiset> </li></ul></ul><ul><li>DTD </li></ul><ul><ul><li><!ELEMENT koululaiset (koululainen)> </li></ul></ul><ul><ul><li><!ELEMENT koululainen (etunimi, sukunimi)> </li></ul></ul><ul><ul><li><!ELEMENT etunimi (#PCDATA)> </li></ul></ul><ul><ul><li><!ELEMENT sukunimi (#PCDATA)> </li></ul></ul>
  27. 27. The Amount of Elements <ul><li><!ELEMENT koululaiset (koululainen+)> </li></ul><ul><li><!ELEMENT koululainen (etunimi+, sukunimi, henktunnus?)> </li></ul><ul><li><!ELEMENT etunimi (#PCDATA)> </li></ul><ul><li><!ELEMENT sukunimi (#PCDATA)> </li></ul><ul><li><!ELEMENT henktunnus (#PCDATA)> </li></ul>
  28. 28. Alternative Elements <ul><li>Alternative elements: </li></ul><ul><ul><li><!ELEMENT materiaali (kirja | video)> </li></ul></ul><ul><li>The use of brackets: </li></ul><ul><ul><li><!ELEMENT henkilo ((etunimi+,(sukunimi, tyttönimi?))|tunnus)> </li></ul></ul>
  29. 29. Attribute Declaration <ul><li>Attribute is defined with ATTLIST. </li></ul><ul><li>Attribute in XML: </li></ul><ul><ul><li><koululainen tunnari=&quot;aaa-222&quot;> </li></ul></ul><ul><li>SYNTAX: <!ATTLIST ELEMENTNAME ATTRIBUTENAME TYPE REQUIRED> </li></ul><ul><ul><li>ELEMENTNAME: The element which the attribute is given </li></ul></ul><ul><ul><li>ATTRIBUTENAME: attribute name </li></ul></ul><ul><ul><li>TYPE: attribute contents </li></ul></ul><ul><ul><li>REQUIRED: Is the attribute mandatory or not </li></ul></ul><ul><li>Example: </li></ul><ul><ul><li><!ATTLIST koululainen tunnari ID #REQUIRED> </li></ul></ul>
  30. 30. Attribute Types <ul><li>Attribute Datatypes </li></ul><ul><ul><li>CDATA : character data </li></ul></ul><ul><ul><li>ENTITY or ENTITIES : entity defined somewhere else </li></ul></ul><ul><ul><li>ID : Unique value. Must begin with letter, underscore or colon. </li></ul></ul><ul><ul><li>IDREF or IDREFS : Reference to ID </li></ul></ul><ul><ul><li>NMTOKEN or NMTOKENS : CDATA without spaces </li></ul></ul><ul><ul><li>NOTATION: Link to external resource </li></ul></ul><ul><li>Example </li></ul><ul><ul><li><!ATTLIST koululainen tunnari ID #REQUIRED> </li></ul></ul>
  31. 31. Attribute Requirements <ul><li>Requirements </li></ul><ul><ul><li>#REQUIRED , mandatory </li></ul></ul><ul><ul><li>#FIXED , value is fixed </li></ul></ul><ul><ul><li>#IMPLIED , optional </li></ul></ul><ul><li>Usage: </li></ul><ul><ul><li><!ATTLIST koululainen tunnari ID #REQUIRED > </li></ul></ul>
  32. 32. Example <ul><li><!ELEMENT kalenteri (tapaaminen*)> </li></ul><ul><li><!ELEMENT tapaaminen (aika, paikka)> </li></ul><ul><li><!ELEMENT aika (pvm , klo)> </li></ul><ul><li><!ELEMENT paikka EMPTY> </li></ul><ul><li><!ELEMENT klo (#PCDATA)> </li></ul><ul><li><!ELEMENT pvm (#PCDATA)> </li></ul><ul><li><!ATTLIST tapaaminen id ID #REQUIRED> </li></ul><ul><li><!ATTLIST paikka yritys CDATA #REQUIRED </li></ul><ul><li>kaupunki (Helsinki|Tampere) &quot;Helsinki&quot; tila NMTOKEN #IMPLIED> </li></ul>
  33. 33. Entity in DTD's <ul><li>It's possible to create your own entities in DTD's: </li></ul><ul><ul><ul><li>Syntax: <! ENTITY company &quot;My Company&quot;> </li></ul></ul></ul><ul><ul><ul><li>Usage in XML: & company ; </li></ul></ul></ul><ul><li>Getting entity value from external file </li></ul><ul><ul><ul><li><! ENTITY company SYSTEM &quot;company.txt&quot;> </li></ul></ul></ul>
  34. 34. Is the XML Valid? <ul><li>XML </li></ul><ul><ul><li><students> </li></ul></ul><ul><ul><li><student> </li></ul></ul><ul><ul><li><name>Tiina Virtanen</name> </li></ul></ul><ul><ul><li><gender>Female</gender> </li></ul></ul><ul><ul><li></student> </li></ul></ul><ul><ul><li></students> </li></ul></ul><ul><li>DTD </li></ul><ul><ul><li><!ELEMENT students (student)> </li></ul></ul><ul><ul><li><!ELEMENT student (name, gender)> </li></ul></ul><ul><ul><li><!ELEMENT name (#PCDATA)> </li></ul></ul><ul><ul><li><!ELEMENT gender (#PCDATA)> </li></ul></ul>
  35. 35. What About Now? <ul><li>XML </li></ul><ul><ul><li><students> </li></ul></ul><ul><ul><li><student> </li></ul></ul><ul><ul><li><name>Tiina Virtanen</name> </li></ul></ul><ul><ul><li><gender> Cow </gender> </li></ul></ul><ul><ul><li></student> </li></ul></ul><ul><ul><li></students> </li></ul></ul><ul><li>DTD </li></ul><ul><ul><li><!ELEMENT students (student)> </li></ul></ul><ul><ul><li><!ELEMENT student (name, gender)> </li></ul></ul><ul><ul><li><!ELEMENT name (#PCDATA)> </li></ul></ul><ul><ul><li><!ELEMENT gender (#PCDATA)> </li></ul></ul>
  36. 36. DTD Critique and Schema <ul><li>The problem with DTD is that it specifies loosely the content of the elements </li></ul><ul><li>Use Schema instead </li></ul><ul><ul><li>Schema is a XML-based language which defines new XML – languages </li></ul></ul><ul><ul><li>Much more specific than DTD (data types!) </li></ul></ul><ul><ul><li>Expandable </li></ul></ul><ul><li>We'll cover the basics of Schema next time... </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×