Ex 01 DTD

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Exercises:

 DTD  
Basi  di  da/  2  

Disheng  Qiu    
disheng.qiu@gmail.com  

Luca  Rossi    
luca.rossi.917@gmail.com  
Hints:  
• Use  a  validator:  
– Eclipse  has  an  embedded  XML/DTD  validator  
– W3C  Validator:  hIp://validator.w3.org/  

• Common  mistakes:  
– Whitespaces  
• Tabs  are  allowed  
• Whitespaces  are  considered  CDATA  

– Quotes  
• “…”  !=  "…"  

– Order  of  mixed  content  


• Ex.:  (#PCDATA,elem1,elem2)  is  NOT  allowed  
– #PCDATA  |  elem1  |  elem2  

– Non-­‐determinis<c  sequences  
• Ex.:  (this  |  this,  that  |  other)  

– A?ributes  modifiers  are  needed  


• <!ATTLIST  elem  aIr  CDATA  #REQUIRED>  
Exercises:  
2x  From  instance  to  DTD  
2x  From  DTD  to  instance  
1x  From  specs  to  instance  to  DTD  
Exercise  1:  Orders  
From  instance  to  DTD  
Ex.1  -­‐  From  instance  to  DTD  
<?xml  version="1.0"?>  
<shiporder  orderid="889923">  
<orderperson>  John  Smith  </orderperson>  
<shipto>  
   <name>  Ola  Nordmann  </name>  
 <address>  Langgt  23  </address>  
 <city>  4000  Stavanger  </city>  
 <country>  Norway  </country>  
</shipto>  
<item>  
   </tle>  Empire  Burlesque<//tle>  
   <note>  Special  Edi/on  </note>  
   <quan/ty>  1  </quan/ty>    
   <price>  10.90  </price>  
</item>  
<item>    
   </tle>  Hide  your  heart<//tle>    
   <quan/ty>1</quan/ty>  
   <price>9.90</price>  
</item>  
</shiporder>  
Ex.1  -­‐  From  instance  to  DTD:  Solu/on  
<?xml  version="1.0"?>  
<!DOCTYPE  shiporder[  
<!ELEMENT  shiporder  (orderperson,shipto,item+)>  
<!ATTLIST  shiporder  orderid  CDATA  #REQUIRED>  
<!ELEMENT  orderperson  (#PCDATA)>  
<!ELEMENT  shipto  (name,address,city,country)>  
<!ELEMENT  name  (#PCDATA)>  
<!ELEMENT  address  (#PCDATA)>  
<!ELEMENT  city  (#PCDATA)>  
<!ELEMENT  country  (#PCDATA)>  
<!ELEMENT  item  (/tle,note?,quan/ty,price)>  
<!ELEMENT  /tle  (#PCDATA)>  
<!ELEMENT  note  (#PCDATA)>  
<!ELEMENT  quan/ty  (#PCDATA)>  
<!ELEMENT  price  (#PCDATA)>  
]>  
Exercise  2:  Le(era  
From  instance  to  DTD  
Ex.2  -­‐  From  instance  to  DTD  
<leIera>  
Gen/le  <cliente>  tal  dei  tali  </cliente>,  
la  informiamo  che  i  seguen/  ar/coli  da  lei  ordina/  non  sono  più  disponibili  a  magazzino:  
<ordine  num="1234">  
<ar/colo>    
<codice>1</codice>  
<descr>ar/colo  1</descr>  
</ar/colo>  
<ar/colo>  
<codice>5</codice>  
<descr>ar/colo  5</descr>  
</ar/colo>  
</ordine>  
Cordiali  salu/,    
<responsabile></t>dr.<//t>Mario  Rossi</responsabile>  
</leIera>  
Ex.2  -­‐  From  instance  to  DTD:  Solu/on  
<?xml  version="1.0"?>  
<!DOCTYPE  leIera[  
<!ELEMENT  leIera  (#PCDATA|cliente|ordine|responsabile)*>  
<!ELEMENT  cliente  (#PCDATA)>  
<!ELEMENT  ordine  (ar/colo+)>  
<!ATTLIST  ordine  num  CDATA  #REQUIRED>  
<!ELEMENT  ar/colo  (codice,descr)>  
<!ELEMENT  codice  (#PCDATA)>  
<!ELEMENT  descr  (#PCDATA)>  
<!ELEMENT  responsabile  (#PCDATA|/t)*>  
<!ELEMENT  /t  (#PCDATA)>  
]>  
Exercise  3:  Catene  Montuose  
From  DTD  to  instance  
Ex.3  -­‐  From  DTD  to  instance  
<?xml  version="1.0"?>  
<!DOCTYPE  cateneMontuose[  
<!ELEMENT  cateneMontuose  (catenaMontuosa*)>  
<!ELEMENT  catenaMontuosa  (monte+)>  
<!ELEMENT  monte  (nome,  regione?,  altezza?)>  
<!ELEMENT  nome  (#PCDATA)>  
<!ELEMENT  regione  (#PCDATA)>  
<!ELEMENT  altezza  (#PCDATA)>  
<!ATTLIST  altezza  unitaMisura  CDATA  #REQUIRED>  
]>  
Ex.3  -­‐  From  DTD  to  instance:  Solu/on  
<cateneMontuose>  
<catenaMontuosa>  
<monte>  
   <nome>  Monte  Bianco  </nome>  
   <regione>  Valle  d’Aosta  </regione>  
   <altezza  unitaMisura="metri">4810</altezza>  
</monte>  
</catenaMontuosa>                    
<catenaMontuosa>  
<monte>  
   <nome>Gransasso</nome>  
</monte>  
</catenaMontuosa>  
</cateneMontuose>  
Exercise  4:  Stock/Used  cars  
From  DTD  to  instance  
Ex.4  -­‐  From  DTD  to  instance  
<?xml  version="1.0"?>  
<!DOCTYPE  stock[  
<!ELEMENT  stock  (new-­‐car  |  used-­‐car)*>  
<!ELEMENT  new-­‐car  (model,  price)>  
<!ELEMENT  used-­‐car  (model,  price,  mileage,  condi/on?)>  
<!ELEMENT  model  (#PCDATA)>  
<!ELEMENT  price  (#PCDATA)>  
<!ELEMENT  mileage  (#PCDATA)>  
<!ELEMENT  condi/on  (#PCDATA)>  
]>  
Ex.4  -­‐  From  DTD  to  instance:  Solu/on  
<stock>  
<new-­‐car>  
<model>Fiat  Panda</model>  
<price>12000</price>  
</new-­‐car>  
<used-­‐car>  
<model>Fiat  Bravo</model>  
<price>4000</price>  
<mileage>1000</mileage>  
<condi/on>Good</condi/on>  
</used-­‐car>  
<used-­‐car>  
<model>Ferrari</model>  
<price>400000</price>  
<mileage>100</mileage>  
</used-­‐car>  
</stock>  
Exercise  5:  Football  matches  
From  specs  to  instance  to  DTD  
Ex.5  -­‐  From  specs  to  instance  to  DTD  

• Consider   an   applica/on   in   which   the   results   of   football   games   are   to   be  


represented  in  XML.    

• For  each  game,  we  want  to  be  able  to  represent  the  two  teams  involved,  which  
one  was  playing  at  home,  which  players  scored  goals  (some  of  which  may  have  
been   penal/es)   and   the   /me   when   each   was   scored,   and   which   players   were  
shown  yellow  or  red  cards.    

• You  might  use  some  aIributes.    


Ex.5  -­‐  From  specs  to  instance  to  DTD:  Solu/on  
<games>  
 <game  score="1-­‐1">  
   <home-­‐team>Roma</home-­‐team>  
   <ex-­‐team>Lazio</ex-­‐team>  
   <scores>  
 <score  /me=  "15">  

<player>Klose</player>  
 </score>  
 <score  /me=  "85"  type="penalty">  
<player>Tox</player>  
 </score>  
 </scores>  
   <yellows>  
     <player>Tox</player>  
     <player>Hernanes</player>  
   </yellows>  
   <reds>  
     <player>Kjaer</player>  
   </reds>  
 </game>  
</games>  
Ex.5  -­‐  From  specs  to  instance  to  DTD:  Solu/on  
<?xml  version="1.0"?>  
<!DOCTYPE  games[  
<!ELEMENT  games  (game)*>  
<!ELEMENT  game  (home-­‐team,  ex-­‐team,  scores,  yellows,  reds)>  
<!ELEMENT  home-­‐team  (#PCDATA)>  
<!ELEMENT  ex-­‐team  (#PCDATA)>  
<!ELEMENT  scores  (score)*>  
<!ELEMENT  yellows  (player)*>  
<!ELEMENT  reds  (player)*>  
<!ELEMENT  score  (player)*>  
<!ELEMENT  player  (#PCDATA)>  
<!ATTLIST  game  score  CDATA  #REQUIRED>  
<!ATTLIST  score  /me  CDATA  #REQUIRED>  
<!ATTLIST  score  type  (field|penalty)  #IMPLIED>  
]>  

You might also like