1 / 15

DTD Document Type Definition per XML

DTD Document Type Definition per XML. Cosa è una DTD. Una DTD è un documento Una DTD serve a definire i blocchi legali di un documento XML Definisce la struttura di un documento XML Un documento XML è well-formed se è conforme alla sintassi XML

laird
Télécharger la présentation

DTD Document Type Definition per XML

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DTD Document Type Definition per XML

  2. Cosa è una DTD • Una DTD è un documento • Una DTD serve a definire i blocchi legali di un documento XML • Definisce la struttura di un documento XML • Un documento XML è well-formed se è conforme alla sintassi XML • Un documento XML è valido se è anche conforme ad una DTD.

  3. Perché usare una DTD • Ogni documento XML può portare con sé, o riferirsi a, una descrizione del proprio formato corretto. • Gruppi di persone possono accordarsi su un formato comune per lo scambio di dati • Una applicazione può verificare automaticamente la validità dei dati scambiati sulla base della DTD.

  4. Usare una DTD • Una DTD può essere dichiarata in linea: • <!DOCTYPE root-element [element-declarations]> • Esempio: <?xml version="1.0"?> <!DOCTYPE note [ <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> ]> <note> <to>MyLove</to> <from>Gege</from> <heading>Reminder</heading> <body> Non ti scordar di me</body> </note>

  5. Usare una DTD • Può essere definita mediante un riferimento esterno: • <!DOCTYPE root-element SYSTEM "filename"> • Esempio: • File XML: <?xml version="1.0"?> <!DOCTYPE note SYSTEM "note.dtd"> <note> <to>MyLove</to> <from>Gege</from> <heading>Reminder</heading> <body> Non ti scordar di me </body> </note> • DTD file: <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)>

  6. I blocchi di una DTD • Elements  body, messaggio – I blocchi che costituiscono un documento XML • Tags  <element_name> </element_name> - Annotano (mark up) gli elementi • Attributes  <element_name id=“1”> - Forniscono valori per gli elementi, qualificandoli • Entities  &lt; (<), &gt; (>) , &quot; (“) – definiscono testo comunemente usato • PCDATA  Parsed Character Data – testo che verrà analizzato da un parser • CDATA  Character Data – testo che non verrà analizzato da un parser

  7. La dichiarazione di un elemento XML in una DTD • <!ELEMENT element-name category> oppure <!ELEMENT element-name (element-content)> • Esempi: • <!ELEMENT br EMPTY>  <br /> • <!ELEMENT element-name (#PCDATA)>  <!ELEMENT from (#PCDATA)> • <!ELEMENT element-name ANY>  <!ELEMENT note ANY> • <!ELEMENT element-name (child-element-name)> oppure <!ELEMENT element-name (child-element-name,child-element-name,.....)> <!ELEMENT note (to,from,heading,body)>

  8. <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> Quando i figli vengono dichiarati in sequenza separata da virgole devono apparire nel medesimo ordine. I figli devono pure essere dichiarati e possono avere altri figli

  9. Cardinalità degli elementi • <!ELEMENT element-name (child-name)>  Cardinalità 1 • <!ELEMENT element-name (child-name+)>  Cardinalità almeno 1 • <!ELEMENT element-name (child-name*)>  Cardinalità 0 o più • <!ELEMENT element-name (child-name?)>  Cardinalità 0 o 1 • Dichiarazioni e/o  <!ELEMENT note (to,from,header,(message|body))> <!ELEMENT note (#PCDATA|to|from|header|message)*>

  10. Attributi • <!ATTLIST element-name attribute-name attribute-type default-value> • Esempio DTD: • <!ATTLIST payment type CDATA "check"> • Esempio XML: • <payment type="check" />

  11. Attributi attribute-type • CDATA character data • (en1|en2|..) lista enumerata • ID id univoco • IDREF id di un altro elemento • IDREFS Lista di altri id • NMTOKEN nome XML valido • NMTOKENS Lista di nomi XML validi • ENTITY Entità • ENTITIES Lista di entità • NOTATION Nome di una notation • xml: Valore XML predefinito

  12. Value default value • #REQUIRED il valore deve essere presente • #IMPLIED il valore non deve essere presente • #FIXED value valore costante • Default: <!ELEMENT square EMPTY> <!ATTLIST square width CDATA "0">  <square width="100" /> • Implied: <!ATTLIST contact fax CDATA #IMPLIED>  <contact fax="555-667788" /> • Required: <!ATTLIST person number CDATA #REQUIRED>  <person number="5677" /> • Fixed: <!ATTLIST sender company CDATA #FIXED "Microsoft">  <sender company="Microsoft" />

  13. Entità • Oltre a quelle standard possono essere definite in una DTD, sia internamente che come riferimento esterno • Interno:<!ENTITY entity-name "entity-value"> <!ENTITY writer "Donald Duck."> • <author>&writer;</author> • Esterno: <!ENTITY entity-name SYSTEM "URI/URL"> • <!ENTITY writer SYSTEM "http://www-ictserv.poliba.it/entita/entita.xml"> • <author>&writer;</author>

  14. <h:html xmlns:ama="http://www.amazon.it/books”xmlns:h="http://www.w3.org/HTML/1998/html4"> • <h:head><h:title>Book Review</h:title></h:head> • <h:body> • <ama:bookreview style=“rilegato” h:style=“{font-size=18;}”> • <ama:title>3 Uomini in barca</ama:title> • <h:table> • <h:tr><h:td>Author</h:td> <h:td>Price</h:td> • <h:td>Pages</h:td> <h:td>Date</h:td> </h:tr> • <h:tr> • <h:td><ama:author>Jerome K. Jerome</ama:author></h:td> • <h:td><ama:price>25.000</ama:price></h:td> • <h:td><ama:pages>352</ama:pages></h:td> • <h:td><ama:publisher>Mondadori</ama:publisher></h:td> • </h:tr> • </h:table> • </ama:bookreview> • </h:body> • </h:html>

  15. I namespace sono stati introdotti dopo i DTD, e quindi esprimere namespace nei DTD non è facile. L'unico modo è di esprimere come nomi validi i nomi qualificati, bloccando il prefisso. • <!DOCTYPE a:item [ • <!ELEMENT a:item (a:title, a:publisher)> • <!ATTLIST a:item • xmlns:a CDATA #FIXED "http://www.betterbooks.org"> • <!ELEMENT a:title (#PCDATA) > • <!ELEMENT a:publisher (#PCDATA) > • ]> • <a:item xmlns:a="http://www.betterbooks.org"> • <a:title>The Importance of Namespaces</a:title> • <a:publisher>BestPublishersInTheWorld</a:publisher> • </a:item> • Da notare che la dichiarazione di namespace va sia nel DTD (viene considerata un attributo), sia nel documento (come dichiarazione vera e propria)

More Related