tanszek:oktatas:techcomm:dtd

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
tanszek:oktatas:techcomm:dtd [2024/10/07 17:33] – created kneheztanszek:oktatas:techcomm:dtd [2024/10/07 17:57] (current) – [Complex DTD Example for a Hypothetical Mail Processing System] knehez
Line 14: Line 14:
  
 === Internal declaration === === Internal declaration ===
-<sxh dtd>+<sxh>
 <!DOCTYPE uzenet [ <!DOCTYPE uzenet [
         ....         ....
Line 21: Line 21:
  
 === External URI === === External URI ===
-<sxh dtd>+<sxh>
 <!DOCTYPE html PUBLIC "-//W3//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/......dtd"> <!DOCTYPE html PUBLIC "-//W3//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/......dtd">
 </sxh> </sxh>
  
 === External file === === External file ===
-<sxh dtd>+<sxh>
 <!DOCTYPE uzenet SYSTEM "filename.dtd"> <!DOCTYPE uzenet SYSTEM "filename.dtd">
 </sxh> </sxh>
  
 The DTD defines the structure and rules that an XML document must follow to be considered valid. The DTD defines the structure and rules that an XML document must follow to be considered valid.
 +
 +==== DTD Through Examples ====
 +
 +Here is a DTD file named ''message_syntax.dtd'' and the corresponding XML on the right:
 +
 +<sxh>
 +<!ELEMENT message ( text )>
 +<!ELEMENT text ( #PCDATA )>
 +</sxh>
 +
 +<sxh xml>
 +<?xml version = "1.0" encoding = "UTF-8"?>
 +<!DOCTYPE message SYSTEM "message_syntax.dtd">
 +
 +<message>
 +   <text>Hello XML</text>
 +</message>
 +</sxh>
 +
 +In the XML structure, a **comma** indicates the required order of elements:
 +
 +<sxh>
 +<!DOCTYPE studygroup [
 +        <!ELEMENT group (teacher, student)>
 +        <!ELEMENT teacher ( #PCDATA ) >
 +        <!ELEMENT student ( #PCDATA ) >   
 +]>
 +</sxh>
 +
 +<sxh xml>
 +<studygroup>
 +    <teacher>Kiss Janos</teacher>
 +    <student>Gipsz Jakab</student>
 +</studygroup>
 +</sxh>
 +
 +The **pipe symbol `|`** expresses an **either-or** relationship. For example, the storage device can be either a **pendrive** or an **SSD**, but not both:
 +
 +<sxh>
 +<!DOCTYPE datastore [
 +    <!ELEMENT datastorage  (pendrive | SSD)>
 +    <!ELEMENT pendrive ( #PCDATA ) >
 +    <!ELEMENT SSD ( #PCDATA ) >   
 +]>
 +</sxh>
 +
 +<sxh xml>
 +<datastorage>
 +    <pendrive>64MB</pendrive>
 +</datastorage>
 +</sxh>
 +
 +There are three ways to express **frequency**:
 +
 +^ Symbol ^ Meaning ^
 +| ''+''    | The element appears at least once. |
 +| ''*''    | The element can appear any number of times, including zero. |
 +| ''?''    | The element appears zero or one time. |
 +
 +==== Example: A music CD contains **tracks**: ====
 +
 +<sxh>
 +<!DOCTYPE datastorage [
 +    <!ELEMENT CD (recording + )>
 +    <!ELEMENT recording ( #PCDATA ) >   
 +]>
 +</sxh>
 +
 +<sxh xml>
 +<CD>
 +    <felvetel>Song 1</felvetel>
 +    <felvetel>Song 2</felvetel>
 +</CD>
 +</sxh>
 +----
 +==== Example: An album has at least one title, followed by at least one **track title** and **duration**: ====
 +
 +<sxh>
 +<!DOCTYPE datastorage [
 +    <!ELEMENT album (title+, (tracktitle, duration)+)>
 +    <!ELEMENT title ( #PCDATA ) >
 +    <!ELEMENT tracktitle ( #PCDATA ) >
 +    <!ELEMENT duration ( #PCDATA ) >   
 +]>
 +</sxh>
 +
 +**Syntactically correct XML**:
 +
 +<sxh xml>
 +<album>
 +    <title>Title 1</title>
 +    <title>Subtitle</title>
 +    <tracktitle>Track Title 1</tracktitle>
 +    <duration>3.42</duration>
 +    <tracktitle>Track Title 2</tracktitle>
 +    <duration>2.32</duration>
 +</album>
 +</sxh>
 +
 +----
 +
 +==== Example: A library may contain **books** (zero or more)====
 +
 +<sxh>
 +<!DOCTYPE datastorage  [
 +    <!ELEMENT library (book*) >
 +    <!ELEMENT book (author, title) >
 +    <!ELEMENT author ( #PCDATA ) >
 +    <!ELEMENT title ( #PCDATA ) >   
 +]>
 +</sxh>
 +
 +**Syntactically correct XML**:
 +
 +<sxh xml>
 +<library>
 +    <book >
 +        <author>Orwell, George</author>
 +        <title >1984</title >
 +    </book>
 +    <book>
 +        <author>Brown, Dan</author>
 +        <title >The Da Vinci Code</title >
 +    </book >
 +</library>
 +</sxh>
 +
 +===== Defining Attributes in DTD =====
 +
 +If a class has an attribute such as "number of students," it can be specified as follows:
 +
 +<sxh>
 +<!ELEMENT class (student *) >
 +<!ATTLIST class number CDATA #REQUIRED>
 +</sxh>
 +
 +Attributes can be:
 +- **#IMPLIED**: not mandatory
 +- **#REQUIRED**: mandatory
 +- **#FIXED**: fixed value
 +
 +=== Specifying a Default Attribute Value ==
 +
 +Example:
 +
 +<sxh>
 +<!ATTLIST paymentType type CDATA "bankTransfer">
 +</sxh
 +
 +The XML could look like this:
 +
 +<sxh>
 +<paymentType/> or <paymentType type="bankTransfer">
 +</sxh>
 +
 +=== Enumerated Values ===
 +
 +**Syntax**:
 +<sxh>
 +<!ATTLIST element-name attribute-name (eval | eval | ..) default-value>
 +</sxh>
 +
 +**DTD Example**:
 +<sxh>
 +<!ATTLIST payment type (check | cash) "cash">
 +</sxh>
 +
 +**XML Example**:
 +<sxh xml>
 +<payment type="check"/> or <payment type="cash"/>
 +</sxh>
 +
 +==== Complex DTD Example for a Hypothetical Mail Processing System ====
 +
 +**Task**: Provide an XML example that satisfies the following DTD:
 +
 +<sxh>
 +<!ELEMENT mails (email*, postcard*)>
 +<!ELEMENT email (address, sender, message?, attachment?)>
 +<!ELEMENT postcard (address, sender?, message?)>
 +<!ELEMENT address (name, postalcode, city, country)>
 +<!ATTLIST address nick CDATA #IMPLIED>
 +<!ATTLIST sender nick CDATA #IMPLIED>
 +<!ATTLIST attachment type CDATA #REQUIRED>
 +<!ATTLIST postcard scanimage CDATA #IMPLIED>
 +<!ATTLIST sender name CDATA #REQUIRED>
 +</sxh>
 +
 +=== A Possible XML for the given DTD ===
 +
 +<sxh xml>
 +<mails>
 +    <email>
 +        <address nick="Alice">alice@usa.com</address>
 +        <sender nick="Bob">bob@jp.com</sender>
 +        <message>Hash code</message>
 +        <attachment type="text/doc"></attachment>
 +    </email>
 +    <postcard scanimage="kep.jpg">
 +       <address>
 +         <name>John Doe</name>
 +         <postalcode>1234</postalcode>
 +         <city>Miskolc</city>
 +         <country>Hungary</country>
 +       </address>
 +       <sender name="Bob Cat" />
 +       <message>Happy Name Day</message>
 +    </postcard>
 +</mails>
 +</sxh>
 +
 +This provides an example of how attributes, including mandatory, optional, and default values, can be defined in a DTD and represented in an XML document.
tanszek/oktatas/techcomm/dtd.1728322384.txt.gz · Last modified: 2024/10/07 17:33 by knehez