Übungserie 2: DTD 

Theoretische Grundlagen der Informatik, WS 02/03

Dozenten: J.-M. Piveteau, U.-M. Künzi

Aufgabe 1 (Einfache DTD)

Schreiben Sie eine DTD für das XML-Dokument der Aufgabe 2 der ersten Übungsserie.

 <!ENTITY % Kontoinformationen "Kontonummer, Saldo, Transaktion*" >
 <!ENTITY % PersonInfo "Name, Vorname, Adresse, Nationalität" >

 <!ELEMENT Stammkonto (Stammkontonummer, Kundendaten, Identifikationsmethode, KundenberaterName, KontenListe, PersonenMitVollmacht, Dauerauftrag*) >
 <!ELEMENT Stammkontonummer (#PCDATA) >
 <!ELEMENT Identifikationsmethode EMPTY >
 <!ATTLIST Identifikationsmethode
       Id_Methode        (Pass | ID-Karte | Führerschein)  #REQUIRED
       Angestellter_Id  CDATA                                          #REQUIRED>
 <!ELEMENT KundenberaterName (#PCDATA) >
 <!ELEMENT KontenListe (Depotkonto?, Bankkonto*, Hypothek*) >
 <!ELEMENT PersonenMitVollmacht (VollmachtsPerson*) >
 <!ELEMENT Dauerauftrag EMPTY >
 <!ATTLIST Dauerauftrag 
       Begünstigtenname   CDATA                                                    #REQUIRED
       Kontonummer           CDATA                                                    #REQUIRED
       Betrag                         CDATA                                                    #REQUIRED
       Periodizität                 (täglich | monatlich | vierteljährlich | jährlich)   #REQUIRED
       ErsteAusführung      CDATA                                                     #IMPLIED
       LetzteAusführung    CDATA                                                     #IMPLIED
  >

<--! Kundendaten -->
 <!ELEMENT Kundendaten     (%PersonInfo;, Geburtsdatum, Telefonnummer, email) >
 <!ELEMENT Geburtsdatum    (#PCDATA) >
 <!ELEMENT Telefonnummer  (Tel_Privat?, Tel_Geschäft?, Tel_Mobile?) >
 <!ELEMENT Tel_Privat           (#PCDATA) >
 <!ELEMENT Tel_Geschäft      (#PCDATA) >
 <!ELEMENT Tel_Mobile         (#PCDATA) >
 <!ELEMENT email                     (#PCDATA) >

 <--! Kontoinformationen -->
 <!ELEMENT Kontonummer     (#PCDATA) >
 <!ELEMENT Saldo                    (#PCDATA) >
 <!ELEMENT Transaktion         (Datum, Betrag, BeteiligtesKonto) >
 <!ATTLIST Transaktion 
       transaktionsart (Einzahlung | Auszahlung | Dauerauftrag) #REQUIRED
 >
 <!ELEMENT Trans_Datum                     (#PCDATA) >
 <!ELEMENT Trans_Betrag                     (#PCDATA) >
 <!ELEMENT Trans_BeteiligtesKonto   (#PCDATA) >

 <--! PersonInfo -->
 <!ELEMENT Name            (#PCDATA) >
 <!ELEMENT Vorname      (#PCDATA) >
 <!ELEMENT Adresse       (#PCDATA) >
 <!ELEMENT Nationalität (#PCDATA) >

<--! Kontotypen -->
 <!ELEMENT Depotkonto (%KontoInformationen;) >
 <!ELEMENT Bankkonto   (%Kontoinformationen;) >
 <!ELEMENT Hypothek     (%Kontoinformationen;) >

<--! Person mit Vollmacht -->
 <!ELEMENT VollmachtsPerson (%PersonInfo;) >

Aufgabe 2 (DTD, Externe Entitäten)

Das Dokument handbook.doc beschreibt das Buch "Handbook of Applied Cryptography".

  1. Schreiben Sie eine gültige DTD für den Inhalt dieses Dokuments. Das Bild ist im Verzeichnis http://.... abrufbar. Beachten Sie dabei, dass Ihre DTD für ähnliche Buchbeschreibungen wieder verwenbar sein soll
    <!ELEMENT cover_picture EMPTY>
    <!ATTLIST cover_picture   url    CDATA    #REQUIRED>
    <!ELEMENT title (#PCDATA)>
    <!ATTLIST  title
                        reference_nr             ID            #IMPLIED>
    <!ELEMENT author EMPTY>
    <!ATTLIST author
                        name             CDATA   #REQUIRED
                        first_name                CDATA   #REQUIRED
                        reference_nr             ID            #IMPLIED>
    <!ELEMENT short_description (paragraph+)>
    <!ELEMENT paragraph (bold*, link*)>
    <!ELEMENT bold (#PCDATA)>
    <!ELEMENT link (#PCDATA)>
    <!ELEMENT  publishing_info EMPTY>
    <!ATTLIST publishing_info
                        publishing_house  CDATA   #REQUIRED
                        isbn                     CDATA   #REQUIRED
                        print_date            CDATA   #REQUIRED
                        edition                 CDATA   #IMPLIED
                        nr_of_pages        CDATA   #IMPLIED
    <!ELEMENT additional_information (#PCDATA)>
    <!ATTLIST additional_information
                        reference   IDREF    #REQUIRED>

     

  2. Stellen Sie die zur DTD entsprechende Baumstruktur des Dokuments dar
    cover_picture
       --> Attribut url = http://...
    title
      --> Wert: Handbook of Applied Cryptography
      --> Attribut reference_nr = 1
    author
      --> Attribut name = Menezer
      --> Attribut first_name = Alfred J.
      --> Attribut reference_nr = 2
    author
      --> Attribut name = Van Oorschot
      --> Attribut first_name = Paul C.
      --> Attribut reference_nr = 3
    author
      --> Attribut name = Vanstone
      --> Attribut first_name = Scott A.
      --> Attribut reference_nr = 4
    short_description
      --> paragraph
              --> Wert: Cryptography, in particular.... 
                    --> bold
                          --> Handbook of...
      --> paragraph
              --> Wert: An actualised list... 
                    --> link
                          --> http://www.cacr...
              
    publishing_info
      --> Attribut publishing_house = CRC Press
      --> Attribut isbn = 0-8493-...
      --> Attribut print_date = October 1996
      --> Attribut nr_of_pages = 816
    additional_information
      --> Wert: This text is adapted...
      --> Attribut reference = 1
    additional_information
      --> Wert: Department of Combinatorics...
      --> Attribut reference = 2
    additional_information
      --> Wert: School of computer science,...
      --> Attribut reference = 3
    additional_information
      --> Wert: Department of ...
      --> Attribut reference = 4

Aufgabe 3 (DTD, Attributstypen, Parameterentitäten)

Für jeden HSR-Student wird folgender Datensatz gespeichert:

  1. Schreiben Sie eine gültige DTD für eine Datei, welche die Datensätze aller HSR-Studenten beinhaltet. Wählen Sie jeweils  geeignete Attributstypen (z.B. ID, IDREF, etc.)
    <!ELEMENT Studentenliste (Student*)>
    <!ELEMENT Student EMPTY>
    <!ATTLIST Student
                                    Name               CDATA    #REQUIRED
                                    Vorname         CDATA    #REQUIRED
                                    PersonalId      CDATA    #REQUIRED
                                    Abteilung       IDREF       #REQUIRED
                                    Module           IDREFS     #IMPLIED>

    <!ELEMENT Abteilung EMPTY>
    <!ATTLIST Abteilung 
                                    Abteilungsname   ID        #REQUIRED>
    <!ELEMENT Modul EMPTY>
    <!ATTLIST Modul
                                    Modulname           ID        #REQUIRED>

     

  2. Gruppieren Sie die Module, welche den Studenten vom Standardstundenplan vorgeschlagen werden, in geeignete Parameter-Entitäten








Aufgabe 4 (XML-Applikation aus der Gesundheitsbranche)

Die Patientendaten der Abteilung für Magen-Darm-Erkrankungen des Kantonspitals werden neu in einer zentralen GIML-Datenbank gespeichert (GIML ist der Gastro Intestinal Markup Language, welche 2001 im MIT entwickelt wurde). Eine alte Patientenliste, welche als Textfile vorhanden ist, muss im neuen System eingespeist werden. Ein Eintrag in dieser Liste enthält folgende Informationen:

Ein Eintrag sieht typischerweise so aus:

Dupont
Philippe
Schmid
01 234 56 78
Dickdarm, Pylorus

Schreiben Sie eine Applikation die einen Eintrag der Liste in eine GIML-Datei umwandelt. Um die GIML-DTD im Internet zu suchen, können Sie beispielsweise den Schema-Repository  http://www.xml.org/xml/registry_searchresults.jsp benutzen.

import java.io.*;
import java.util.*;

public class giml_creation {

  public FileReader dbFile;
  public FileWriter outFile;
  private StringBuffer toWrite = new StringBuffer("<?xml version=\"1.0\" standalone=\"no\"?>\n<!DOCTYPE GIML   SYSTEM \"giml.dtd\">\n");
  private Stack tags = new Stack();


  public giml_creation() {
  }
  public static void main(String[] arg) {
  if( arg.length != 2 ){
    System.out.println("usage: java giml_creation dbFile.txt outfile.xml");
    System.exit(-1);
  }

  try{
    Vector attrName = new Vector();
    Vector attrValue = new Vector();
    giml_creation gimlc = new giml_creation();
    gimlc.dbFile = new FileReader( arg[0] );
    gimlc.outFile = new FileWriter( arg[1] );
    String temp = new String();
    char c[] = new char[1024]; 
    int i = 0;

    gimlc.dbFile.read( c, 0, 1020 );
    StringTokenizer tokenizer = new StringTokenizer( new String(c) );

    gimlc.writeStartTag( "GIML", attrName, attrValue );
    gimlc.writeStartTag( "head", attrName, attrValue );
    attrName.add( "type" );
    attrValue.add( "dob" );
    gimlc.writeStartTag( "time", attrName, attrValue ); attrName.clear(); attrValue.clear();
    gimlc.writeStartTag( "year", attrName, attrValue ); gimlc.writeEndTag(); 
    gimlc.writeStartTag( "month", attrName, attrValue ); gimlc.writeEndTag(); 
    gimlc.writeStartTag( "date", attrName, attrValue ); gimlc.writeEndTag();
    gimlc.writeEndTag(); 
    gimlc.writeEndTag(); 
    try{
      while( true ){
        temp = tokenizer.nextToken();
        switch(i){
          case 0:
            attrName.add( "id" );
            attrValue.add( (new Integer(i)).toString() );
            gimlc.writeStartTag( "patient", attrName, attrValue );
            attrName.clear();
            attrValue.clear();
            gimlc.writeStartTag( "name", attrName, attrValue );
            gimlc.writeStartTag( "surname", attrName, attrValue );
            gimlc.writeText( temp );
            gimlc.writeEndTag();
            break;
          case 1:
            gimlc.writeStartTag( "given", attrName, attrValue );
            gimlc.writeText( temp );
            gimlc.writeEndTag();
            gimlc.writeEndTag();
            // do the following to get a valid document
            attrName.add( "type" );
            attrValue.add( "dob" );
            gimlc.writeStartTag( "time", attrName, attrValue ); attrName.clear(); attrValue.clear();
            gimlc.writeStartTag( "year", attrName, attrValue ); gimlc.writeEndTag(); 
            gimlc.writeStartTag( "month", attrName, attrValue ); gimlc.writeEndTag(); 
            gimlc.writeStartTag( "date", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeEndTag();
            gimlc.writeStartTag( "gender", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "mass", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "height", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "contact_info", attrName, attrValue ); 
            gimlc.writeStartTag( "address", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "city", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "state", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "zip", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "country", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "telephone", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "fax", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "email", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeEndTag();
            gimlc.writeStartTag( "allergies", attrName, attrValue ); gimlc.writeEndTag();
            break;
          case 2:
            gimlc.writeStartTag( "referring_doctor", attrName, attrValue );
            gimlc.writeStartTag( "name", attrName, attrValue );
            gimlc.writeStartTag( "surname", attrName, attrValue );
            gimlc.writeText( temp );
            gimlc.writeEndTag();
            // do the following to get a valid document
            gimlc.writeStartTag( "given", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "middle", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "title", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeEndTag();
            break;
          case 3:
            // do the following to get a valid document
            gimlc.writeStartTag( "contact_info", attrName, attrValue );
            gimlc.writeStartTag( "address", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "city", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "state", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "zip", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "country", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "telephone", attrName, attrValue );
            gimlc.writeText( temp );
            break;
          case 4:
            gimlc.writeText( " " + temp );
            break;
          case 5:
            gimlc.writeText( " " + temp );
            break;
          case 6:
            gimlc.writeText( " " + temp );
            gimlc.writeEndTag();
            // do the following to get a valid document
            gimlc.writeStartTag( "fax", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "email", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeEndTag();
            gimlc.writeStartTag( "hospital", attrName, attrValue );
            gimlc.writeStartTag( "name", attrName, attrValue );
            gimlc.writeStartTag( "surname", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "given", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "middle", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "title", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeEndTag();
            gimlc.writeStartTag( "contact_info", attrName, attrValue ); 
            gimlc.writeStartTag( "address", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "city", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "state", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "zip", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "country", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "telephone", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "fax", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "email", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeEndTag();
            gimlc.writeEndTag();
            gimlc.writeEndTag();
            gimlc.writeStartTag( "hospital", attrName, attrValue );
            gimlc.writeStartTag( "name", attrName, attrValue );
            gimlc.writeStartTag( "surname", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "given", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "middle", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "title", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeEndTag();
            gimlc.writeStartTag( "contact_info", attrName, attrValue ); 
            gimlc.writeStartTag( "address", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "city", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "state", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "zip", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "country", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "telephone", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "fax", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "email", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeEndTag();
            gimlc.writeEndTag();
            break;
          case 7:
            // todo: find out what a investigation the patient had
            attrName.add( "type" );
            attrValue.add( "0" );
            gimlc.writeStartTag( "investigation", attrName, attrValue );
            attrName.clear();
            attrValue.clear();
            attrName.add( "num" );
            attrName.add( "type" );
            attrName.add( "organ" );
            attrValue.add( "" );
            attrValue.add( "0" );
            if( temp.indexOf( "Dickdarm" ) != -1 ){
              attrValue.add( "8" ); // todo: find out what a organ is investigated
              temp = tokenizer.nextToken(); // do this without any sense in this case
              // remove this as soon as you can find out what organ was investigated
            }else{
              attrValue.add( "0" ); // todo: find out what a organ is investigated
            }
            gimlc.writeStartTag( "sensor", attrName, attrValue );
            attrName.clear();
            attrValue.clear();
            // do the following to get a valid document
            gimlc.writeStartTag( "comment", attrName, attrValue ); gimlc.writeEndTag();
            gimlc.writeStartTag( "interval", attrName, attrValue ); gimlc.writeEndTag();
            attrName.add( "marker" );
            attrName.add( "position" );
            attrValue.add( "0" );
            attrValue.add( "null" );
            gimlc.writeStartTag( "d", attrName, attrValue ); gimlc.writeEndTag();
            attrName.clear();
            attrValue.clear();
            gimlc.writeEndTag();
            gimlc.writeEndTag();
            gimlc.writeEndTag();
            break;           
          default:
            break;
        }
        i++;
      }
    }catch( NoSuchElementException e ){};

    gimlc.writeEndTag();
    gimlc.writeTheFile();

    }catch( Exception e ){
      System.out.println( e );
    }
  }
  
  public void writeStartTag( String name, Vector attNames, Vector attValues ){
    toWrite.append( "<" + name );
    for( int i = 0; i < attValues.size(); i++ ){
      toWrite.append( " " + attNames.elementAt( i ) + "=\"" + attValues.elementAt( i ) + "\"" );
    }
    toWrite.append( ">" );
    tags.push( name );
  }

  public void writeText( String value ){
    toWrite.append( value );
  }

  public void writeEndTag( ){
    toWrite.append( "</" + tags.pop() + ">\n" );
  }

  public void writeTheFile( ){
    try{
      outFile.write( toWrite.toString(), 0, toWrite.toString().length() );
      outFile.close();
    }catch(IOException e){
      System.out.println( e );
    }
  }
}