Download - XML Schema (XSD)
![Page 1: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/1.jpg)
XML-Schema
Eidon School
![Page 2: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/2.jpg)
Agenda
Introduzione e richiami formali Più in dettaglio Esempi
PAUSA
Esercitazione con l’aiuto di xmllint Esercitazione con l’aiuto di carta e penna Controllo del'esercitazione con xmllint
![Page 3: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/3.jpg)
XSL-Schema
Raccomandazione del W3C (1.0 del maggio 2001, 1.1 è candidate racommendation)
È uno linguaggio XML like (primo a raggiungere la W3C recommendation) per validare un documento XML
Validare un documento XML significa verificare che è conforme ai requisiti richiesti dallo schema (presenza/assenza di nodi e restrizioni sui tipi di dati presenti)
Viene usato anche per documentazione
![Page 4: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/4.jpg)
Nomenclatura
Esistono diversi XML schema con cui validare: W3C XML Schema o semplicemente XML
Schema (o anche WXS o XSD, notale la “S” in grande di schema) usato in questa sede
Relax NG (REgular LAnguage for XML Next Generation)
Schematron XML-Data Reduced (XDR) di Microsoft altri minori
![Page 5: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/5.jpg)
DEMOValidiamo il nostro file XML
xmllint --schema Elenco.xsd Elenco.xml
![Page 6: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/6.jpg)
Annotations
Documentazione Istruzioni al sistema
<xs:annotation><xs:documentation xml:lang="it-IT">
Nodo Radice</xs:documentation><xs:appinfo>root-node</xs:appinfo>
</xs:annotation>
![Page 7: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/7.jpg)
Dichiarazione di un elemento
È la tipica dichiarazione di un elemento che contiene testo (o un tipo specificato in type)
<xs:element name="fullName" type="xs:string">
![Page 8: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/8.jpg)
Tipi di dati (alcuni) 1/2
boolean byte dateTime integer string language
![Page 9: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/9.jpg)
Tipi di dati 2/2
E’ possibile aumentare i tipi di dato unendo o restringendo i tipi base:
<xs:restriction base="xs:string">
<xs:enumeration value="casa"/>
<xs:enumeration value="ufficio"/>
</xs:restriction>
![Page 10: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/10.jpg)
Definizioni di tipo
la definizione di un elemento o di un attributo avviene definendo un ComplexType o un SimpleType
la definizione può avvenire globalmente (inserendo il tipo sotto la radice dello schema) e dandogli un nome con cui riferirlo, o dichiararlo inline senza nome (viene utilizzato solo una volta)
Vedi il SimpleType scontoType nell’Esempio.xsd
![Page 11: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/11.jpg)
SimpleType e ComplexType
Un elemento che non contiene sotto elementi né attributi può essere definito attraverso un SimpleType, altrimenti occorre usare un ComplexType.
Gli attributi sono sempre SimpleType Vedi Indirizzo nel file di esempio.
![Page 12: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/12.jpg)
Occorrenze
La dichiarazione di un element prevede un limite al numero di occorrenze di un elemento:
minOccurs da 0 a xmaxOccurs da 1 a unboundeddefault per entrambi è 1
<xs:element name="record" maxOccurs="unbounded">
![Page 13: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/13.jpg)
Il contenuto degli elementi
Un elemento può contenere: altri elementi essere vuoto contenere semplicemente testo essere a contenuto misto (mixed content, vi
ricordare il seminario XML…???) non definito
![Page 14: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/14.jpg)
Elementi vuoti (Empty)
A cosa servono? marcatori di posizione presenza/assenza contenitori di attributi
Esempio di elemento vuoto e opzionale:
<xs:element name="preferito" minOccurs="0">
<xs:complexType/>
</xs:element>
![Page 15: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/15.jpg)
Elementi con solo testo
Hanno un contenuto di tipo semplice (simpleContent) e adottano come tipi base quelli definiti dallo schema con la possibilità di ridefinirli con un meccanisco chiamato Facets.
Controlli possibili: length, minLength, maxLength, pattern, enumeration, whiteSpace, maxInclusive, maxExclusive, minInclusive, minExclusive …
![Page 16: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/16.jpg)
Limitare la lunghezza (Facet)
<xs:simpleType name="nomeType">
<xs:restriction base="xs:string"> <xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
![Page 17: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/17.jpg)
Enumerare (Facet)
<xs:simpleType name="locationType"> <xs:restriction base="xs:string"> <xs:enumeration value="work"/> <xs:enumeration value="home"/> <xs:enumeration value="mobile"/> </xs:restriction>
</xs:simpleType>
![Page 18: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/18.jpg)
Espressioni regolari (Facet)
<xs:simpleType name=“indirizzowebType">
<xs:restriction base="xs:string">
<xs:pattern value="(http|https|ftp)\://(((((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5]))))|((([a-z0-9\-])+\.)+([a-z]{2}\.[a-z]{2}|[a-z]{2,4})))"/>
</xs:restriction>
</xs:simpleType>
![Page 19: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/19.jpg)
Elemento contenente altri elementi
E’ di tipo complesso e definisce altri elementi che possono apparire come figli attraverso:
xs:sequence i figli devono apparire nell’ordine specificato
xs:choice deve essere presente uno e solo uno dei figli descritti
xs:any i figli possono apparire una e una solo volta in qualunque ordine
![Page 20: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/20.jpg)
Qualunque contenuto
E’ necessario a volte poter far includere qualunque cosa dentro un elemento (es: rendering di XHTML): xs:any xs:anyAttribute
![Page 21: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/21.jpg)
Univocità dei campi
<xs:unique name="codice_cliente_unique">
<xs:selector xpath="record"/>
<xs:field xpath="codice_cliente"/>
</xs:unique>
![Page 22: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/22.jpg)
ESERCITAZIONI
![Page 23: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/23.jpg)
Numeri
Arricurarsi che il codice cliente sia un numero da 1 a 9999.
…<codice_cliente>5</codice_cliente>…
Suggerimento: xs:restriction, xs:minInclusive, xs:maxInclusive
![Page 24: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/24.jpg)
Soluzione
<xs:element name="codice_cliente">
<xs:simpleType>
<xs:restriction base="xs:short">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="9999"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
![Page 25: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/25.jpg)
Testo
Limitare l’attributo tipo di indirizzo a “casa” e “ufficio”
Suggerimento: xs:restriction e xs:enumeration
![Page 26: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/26.jpg)
Soluzione
<xs:attribute name="tipo" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="casa"/>
<xs:enumeration value="ufficio"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
![Page 27: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/27.jpg)
Formato string
Rendere obbligatorio che il codice_fiscale inizi con “IT…”
Suggerimento: regex ‘IT.*’
![Page 28: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/28.jpg)
Soluzione
<xs:element name="codice_fiscale">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="IT.*"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
![Page 29: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/29.jpg)
Tag obbligatorio
Rendere obbligatorio il tag sconto di record
![Page 30: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/30.jpg)
Soluzione
<xs:element name="sconto" type="scontoType" minOccurs="0"/>
![Page 31: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/31.jpg)
Univocita del codice_cliente
Inserire nel file XSD il controllo dell’univocità del campo codice_cliente dell’elemento record.
Suggerimento: xs:unique
![Page 32: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/32.jpg)
Soluzione
<xs:element name="anagrafica"> <xs:complexType> <xs:sequence> <xs:element name="testata"> ...
</xs:element> <xs:element name="record" maxOccurs="unbounded">
.... </xs:element> </xs:sequence> </xs:complexType> <xs:unique name="codice_cliente_unique"> <xs:selector xpath="record"/> <xs:field xpath="codice_cliente"/>
</xs:unique>…
![Page 33: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/33.jpg)
Dichiarazioni di tipi globali
Rendere globale il tipo definito in linea per codice_fiscale
![Page 34: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/34.jpg)
Soluzione
<xs:simpleType name="scontoType"><xs:restriction base="xs:string">
<xs:enumeration value="si"/><xs:enumeration value="no"/>
</xs:restriction></xs:simpleType>
…<xs:element name="codice_fiscale"
type="codicefiscaleType"/>
![Page 35: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/35.jpg)
Key e KeyRef
![Page 36: XML Schema (XSD)](https://reader034.vdocuments.pub/reader034/viewer/2022052210/559e1ab31a28abd75b8b45fc/html5/thumbnails/36.jpg)
Riferimenti
http://www.w3.org/TR/xmlschema-0/
http://www.w3.org/TR/xmlschema-1/
http://www.w3.org/TR/xmlschema-2/
XML in a Nutshell, Third Edition by Elliotte Rusty Harold and W. Scott Means (Paperback - Sep 23, 2004)