Corso di Laurea Specialistica in Ingegneria Informatica Corso di Linguaggi e Tecnologie Web A. A. 2011-2012 DTD ed XML Schema - Esercizi Eufemia TINELLI
Contenuti Esercizi: DTD XML e XSD XML Design Pattern per XML 2012 2
Esercizio 1 Si progetti il DTD e si scriva un XML valido per un linguaggio di markup per lettere. Requisiti: Ogni lettera ha un mittente, una data, un destinatario, un oggetto, una forma cortese di saluto, un corpo, una chiusura, una firma. Il corpo della lettera ha quanto meno un paragrafo. 2012 3
DTD es. 1 <!ELEMENT lettera (mittente, data, destinatario, oggetto, saluto,corpo, chiusura, firma)> <!ELEMENT mittente (#PCDATA)> <!ELEMENT data (#PCDATA)> <!ELEMENT destinatario (#PCDATA)> <!ELEMENT oggetto (#PCDATA)> <!ELEMENT saluto(#pcdata)> <!ELEMENT corpo (paragrafo+)> <!ELEMENT paragrafo (#PCDATA)> <!ELEMENT chiusura (#PCDATA)> <!ELEMENT firma (#PCDATA)> 2012 4
XML es. 1 <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE lettera SYSTEM "lettera.dtd"> <lettera> <mittente>max Temporali</mittente> <data>23 Ottobre 2011</data> <destinatario>i tifosi della MotoGP</destinatario> <oggetto>dramma a Sepang durante la gara della MotoGP</oggetto> <saluto>ciao Marco</saluto> <corpo> <paragrafo>non mi sembra vero, sono frastornato. Mi tremano le mani ed ho un senso di nausea da malessere. [ ]</paragrafo> <paragrafo>non la commento, non le voglio nemmeno rivedere quelle immagini. Sono crude, impietose, devastanti per la sensibilità di chiunque.[ ]</paragrafo> <paragrafo>marco è stato un campione di umanità e di simpatia prima di tutto, l unico pilota a cui ho chiesto una dedica su un poster [ ]</paragrafo> <paragrafo>non oso mettermi nei panni del babbo di Marco, della fidanzata, della mamma e [ ]</paragrafo> </corpo> <chiusura> Con affetto,</chiusura> <firma>max Temporali</firma> </lettera> 2012 5
XML di uno schema relazionale DTD <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE schema SYSTEM schemarelazionale.dtd"> <schema> <relazione nome="studente"> <attributi> <attributo null="no">matricola</attributo> <attributo null="yes">nome</attributo> <attributo null="yes">cognome</attributo> <attributo null="yes">data nascita</attributo></attributi> <chiave><componente>matricola</componente> </chiave> </relazione> <relazione nome="esame"> <attributi> <attributo null="no">corso</attributo> <attributo null="no">studente</attributo> <attributo null="yes">data</attributo> <attributo null="no">voto</attributo></attributi> <chiave><componente>corso</componente><componente>studente</componente></chiave> <chiave_esterna> <sorgente><componente>studente</componente></sorgente> <destinazione ref= Studente >Matricola</destinazione> </chiave_esterna> </relazione> </schema> 2012 6
XML DTD: Soluzione <?xml version="1.0" encoding="utf-8"?> <!ELEMENT schema (relazione)+> <!ELEMENT relazione (attributi,chiave,chiave_esterna*)> <!ELEMENT attributi (attributo+)> <!ELEMENT chiave (componente+)> <!ELEMENT chiave_esterna (sorgente,destinazione)> <!ELEMENT sorgente (componente+)> <!ELEMENT attributo (#PCDATA)> <!ELEMENT componente (#PCDATA)> <!ELEMENT destinazione (#PCDATA)> <!ATTLIST relazione nome ID #REQUIRED> <!ATTLIST attributo null (yes no) yes > <!ATTLIST destinazione ref IDREF #REQUIRED> 2012 7
Esercizio 2 - http://www.w3.org/tr/xmlschema-0/ Si progetti un XSD e si scriva un XML valido per un linguaggio di markup per un ordine di acquisto. Requisiti: Ogni ordine di acquisto ha la data dell ordine ed una serie di informazioni: dati acquirente degli US (nome e indirizzo), dati venditore degli US (nome e indirizzo) e dati dei prodotti acquistati. Ciascuna di queste informazioni può avere un commento opzionale Ciascun prodotto ha un nome, una quantità (non superiore a 100), un prezzo, una data di spedizione ed un codice (es. formato del codice 123-AB) 2012 8
XSD es. 2 (1) <xsd:schema xmlns:xsd="http://www.w3.org/2001/xmlschema"> <xsd:annotation> <xsd:documentation xml:lang="en">purchase order schema for Example.com.Copyright 2000 Example.com. All rights reserved.</xsd:documentation> </xsd:annotation> <xsd:element name="purchaseorder" type="purchaseordertype"/> <xsd:element name="comment" type="xsd:string"/> 2012 9
XSD es. 2 (2) <xsd:complextype name="purchaseordertype"> <xsd:sequence> <xsd:element name="shipto type="usaddress"/> <xsd:element name="billto" type="usaddress"/> <xsd:element ref="comment minoccurs="0"/> <xsd:element name="items" type="items"/> </xsd:sequence> <xsd:attribute name="orderdate" type="xsd:date"/> </xsd:complextype> 2012 10
XSD es. 2 (3) <xsd:complextype name="usaddress"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="street" type="xsd:string"/> <xsd:element name="city" type="xsd:string"/> <xsd:element name="state" type="xsd:string"/> <xsd:element name="zip" type="xsd:decimal"/> </xsd:sequence> <xsd:attribute name="country" type="xsd:nmtoken fixed="us"/> </xsd:complextype> 2012 11
XSD es. 2 (4) <xsd:complextype name="items"> <xsd:sequence> <xsd:element name="item" minoccurs="0" maxoccurs="unbounded"> <xsd:complextype> <xsd:sequence> <xsd:element name="productname" type="xsd:string"/> <xsd:element name="quantity"> <xsd:simpletype> <xsd:restriction base="xsd:positiveinteger"> <xsd:maxexclusive value="100"/> </xsd:restriction> </xsd:simpletype> </xsd:element> <xsd:element name="usprice" type="xsd:decimal"/> <xsd:element ref="comment" minoccurs="0"/> <xsd:element name="shipdate" type="xsd:date" minoccurs="0"/> </xsd:sequence> <xsd:attribute name="partnum" type="sku" use="required"/> </xsd:complextype> </xsd:element> </xsd:sequence> </xsd:complextype> 2012 12
XSD es. 2 (5) <!-- Stock Keeping Unit, a code for identifying products --> <xsd:simpletype name="sku"> <xsd:restriction base="xsd:string"> <xsd:pattern value="\d{3}-[a-z]{2}"/> </xsd:restriction> </xsd:simpletype> </xsd:schema> 2012 13
XML es. 2 <?xml version="1.0"?> <purchaseorder xmlns:xs="http://www.w3.org/2001/xmlschema-instance" xs:nonamespaceschemalocation= purchase-order.xsd orderdate="1999-10-20"> <shipto country="us"> <name>alice Smith</name><street>123 Maple Street</street> <city>mill Valley</city><state>CA</state> <zip>90952</zip> </shipto> <billto country="us"> <name>robert Smith</name><street>8 Oak Avenue</street> <city>old Town</city><state>PA</state> <zip>95819</zip> </billto> <comment>hurry, my lawn is going wild!</comment> <items> <item partnum="872-aa"> <productname>lawnmower</productname><quantity>1</quantity><usprice>148.95</usprice> <comment>confirm this is electric</comment> </item> <item partnum="926-aa"> <productname>baby Monitor</productName><quantity>1</quantity><USPrice>39.98</USPrice> <shipdate>1999-05-21</shipdate> </item> </items> </purchaseorder> 2012 14
XML di una business card XSD <?xml version="1.0" encoding="utf-8"?> <b:card xmlns:b="http://businesscard.org" xmlns:xs="http://www.w3.org/2001/xmlschema-instance" xs:schemalocation="http://businesscard.org <b:name>john Doe</b:name> business-card.xsd"> <b:title>ceo, Widget Inc.</b:title> <b:email>john.doe@widget.com</b:email> <b:phone>(202) 555-1414</b:phone> <b:logo uri="widget.gif" /> </b:card> 2012 15
XML XSD: Soluzione <schema xmlns="http://www.w3.org/2001/xmlschema" xmlns:b="http://businesscard.org" targetnamespace="http://businesscard.org" elementformdefault="qualified"> <element name="card" type="b:card_type"/> <complextype name="card_type"> <sequence> <element ref="b:name"/><element name="title" type="string"/> <element name="email" type="b:email"/><element name="phone" type="string" minoccurs="0"/> <element name="logo" type="b:logo_type" minoccurs="0"/> </sequence> </complextype> <element name="name" type="string"/> <simpletype name="email"> <restriction base="string"><pattern value="([a-z] [0-9] [.])+@([a-z] [0-9] [.])+"/> </restriction> </simpletype> <complextype name="logo_type"> <attribute name="uri" type="anyuri" use="required"/> </complextype> </schema> 2012 16
Best practices - http://blogs.msdn.com blogs.msdn.com/b/ /b/skaufman/archive/2005/05/10/416269.aspx www.xfront.com/globalversuslocal.html 2012 17
Approccio Bambole Russe <xsd:element name="book"> <xsd:complextype> <xsd:sequence> <xsd:element name="title" type="xsd:string"/> <xsd:element name="author" type="xsd:string"/> </xsd:sequence> </xsd:complextype> </element> Nessuno dei tipi e degli elementi è riutilizzabile (local scope) Stile compatto De-accoppiamento: ogni elemento si descrive da se Coesione: tutte le informazioni sono raggruppate in un unica descrizione 2012 18
Approccio Fette di Salame <xsd:element name="title" type="xsd:string"/> <xsd:element name="author" type="xsd:string"/> <xsd:element name="book"> <xsd:complextype> <xsd:sequence> <xsd:element ref="title"/> <xsd:element ref="author"/> </xsd:sequence> </xsd:complextype> </xsd:element> Tipi ed elementi riutilizzabili (global scope) Stile verboso Accoppiamento: elementi interconnessi Coesione: tutte le informazioni sono raggruppate in un unica descrizione 2012 19
Approccio Tende alla Veneziana <xsd:simpletype name="title"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="mr."/> <xsd:enumeration value="mrs."/> <xsd:enumeration value="dr."/> </xsd:restriction> </xsd:simpletype> <xsd:simpletype name="name"> <xsd:restriction base="xsd:string"> <xsd:minlength value="1"/> </xsd:restriction> </xsd:simpletype> <xsd:complextype name="publication"> <xsd:sequence> <xsd:element name="title" type="title"/> <xsd:element name="author" type="name"/> </xsd:sequence> </xsd:complextype> Riuso delle definizioni dei tipi Stile verboso Accoppiamento: elementi interconnessi Coesione: tutte le informazioni sono raggruppate in un unica descrizione <xsd:element name="book" type="publication"/> 2012 20
Approccio Giardino dell Eden Eden <?xml version="1.0" encoding="utf-8"?> <xs:schema targetnamespace="http://book.org" xmlns:xs="http://www.w3.org/2001/xmlschema" elementformdefault="qualified" attributeformdefault="unqualified"> <xs:element name="bookinformation" type="bookinformationtype"/> <xs:complextype name="bookinformationtype"> <xs:sequence> <xs:element ref="title"/> <xs:element ref="isbn"/> <xs:element ref="publisher"/> <xs:element ref="peopleinvolved" maxoccurs="unbounded"/> </xs:sequence> </xs:complextype> <xs:complextype name="peopleinvolvedtype"> <xs:sequence> <xs:element ref="author"/> </xs:sequence> </xs:complextype> <xs:element name="title" type="string"/> <xs:element name="isbn" type="string"/> <xs:element name="publisher" type="string"/> <xs:element name="author" type="string"/> <xs:element name="peopleinvolved" type="peopleinvolvedtype"/> </xs:schema> 2012 21