Post on 26-Mar-2015
XML Schema
Laurea Magistrale in InformaticaLaurea Magistrale in Informatica
Chapter 04 Chapter 04
Modulo del corsoModulo del corso
Thecnologies for InnovationThecnologies for Innovation
XML Schema 2
Agenda
Schema characteristics
Element Declaration
Simple Element
Restrictions
Complex Elements
Indicator
Namespaces
Schema characteristics
Element Declaration
Simple Element
Restrictions
Complex Elements
Indicator
Namespaces
XML Schema 3
XML Schema
“Schema” è un termine generale, dal vocabolario inglese: “a structured framework or plan”
Quando si parla di “XML Schema” si intende usualmente il W3C XML Schema Language … e il suo acronimo XSD (Xml Schema Definitiom)
DTD, XML Schema, e RELAX NG sono tutti linguaggi di “schema” XML
XML Schema 4
Very Simple xml schema
It starts with an XML declaration L’estensione è .xsd e l’elemento root è <schema> Namespace declaration Element Declaration
The declaration makes use of our Schema namespace, and it uses attributes to provide information about the element we are defining
XML SCHEMA ARE WELL_FORMED XML DOCUMENTS
XML Schema 5
Schema characteristics
Because XML Schemas are written using XML, that allows Schemas to be adopted quickly and easily by applications already using XML.
Because XML is text based, it is easily transferable via common Internet file transfer methods, such as FTP and HTTP.
In fact, Schemas are "optimized for interoperability" in this way because they are recursive, self-describing documents.
XML Schemas is for datatyping.Datatypes allow you to specify that a piece of information has to be in a specific data format. Datatypes are a very powerful mechanism for placing
constraints on your XML documents.
XML Schema 6
Element Declarations
The declaration uses the element type, which has a number of attributes that manipulate properties of the element.
In the preceding declaration, there is a name attribute, which is used to specify the name of the element type being declared.
There is also a type attribute, which is how you specify the datatype associated with an element using XML Schema.
XML Schema 7
Valid Attributes for tag <element>
The element declaration in an XML Schema takes the form of the <element> tag.
That tag forms the start of any element declaration in your schema.
The <element> declaration can accept a number of attributes, each one designed to manipulate one or more of the properties that make up an element declaration.
XML Schema 8
Default and Fixed attribute
XML Schema 9
MaxOccurs and MinOccurs attribute
XML Schema 10
REF and Type attribute
XML Schema 11
Content Models: Elementi “semplici” e “complessi”
Un elemento semplice contiene solo testo Non ha attributi Non contiene altri elementi Non può essere vuoto Ci possono essere varie restrizioni applicate al
contenuto
Un elemento complesso Può avere attributi Può essere vuoto, contenere testo, altri elementi, o
sia testo che altri elementi.
XML Schema 12
Definire un elemento semplice
Un elemento semplice è definito come:
<xs:element name="name” type="type” /> dove:
name è il nome dell’elemento Valori comuni per type sono
xs:boolean xs:integer xs:date xs:string xs:decimal xs:time
<xs:element name="quantity" type="xs:integer"/>
<xs:element name="temperature" type="xs:decimal"/>
<xs:element name="holiday" type="xs:date"/>
<xs:element name="attendance" type="xs:boolean"/>
<xs:element name="title" type="xs:string"/>
XML Schema 13
Definire un attributo
Gli attributi sono dichiarati sempre come tipi semplici Definito come
<xs:attribute name="name” type="type” />
dove: name e type sono gli stessi che per xs:element
Altri attributi che gli elementi semplici possono avere: default="default value" se nessun valore è specificato fixed="value” nessun altro valore può essere specificato use="optional" non richiesto (default) use="required" richiesto
XML Schema 14
<simpleType>
XML Schema 15
Restrizioni (“facets”)
La forma generale delle restrizioni è: <xs:element name="name"> (o
xs:attribute) <xs:restriction base="type"> ... the restrictions ... </xs:restriction></xs:element>
Ad esempio: <xs:element name="age">
<xs:restriction base="xs:integer"> <xs:minInclusive value="0"> <xs:maxInclusive value="140"> </xs:restriction></xs:element>
XML Schema 16
Restrizioni su numeri
minInclusive -- numero deve essere ≥ di value
minExclusive -- numero deve essere > di value
maxInclusive -- numero deve essere ≤ di value
maxExclusive -- numero deve essere < di value
totalDigits -- numero deve avere value cifre
fractionDigits -- numero deve avere non più di value
cifre dopo il punto decimale
XML Schema 17
Restrizioni su stringhe
length -- la stringa deve contenere value caratteri minLength -- la stringa deve contenere almeno
value caratteri maxLength -- la stringa deve contenere non più di
value caratteri pattern -- value è una espressione regolare da
soddisfare whiteSpace -- dice come trattare gli spazi bianchi
value="preserve" li mantiene value="replace" rinpiazza con “spazi” value="collapse" rimuove gli spazi iniziali, finali e
rimpiazza le sequenze con uno spazio singolo
XML Schema 18
Enumerazioni
Restringe il range di possibili valori ad una loro enumerazione
Esempio: <xs:element name="season">
<xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="Spring"/> <xs:enumeration value="Summer"/> <xs:enumeration value="Autumn"/> <xs:enumeration value="Fall"/> <xs:enumeration value="Winter"/> </xs:restriction> </xs:simpleType></xs:element>
XML Schema 19
ESEMPIO : writing tip on a pen
if we wanted to create an attribute for the size of the writing tip on a pen, we might want the size to be able to be expressed as a name, such as "Extra Fine" or as a decimal, such as ".05".
size by name size by value
XML Schema 20
UNIONE per creare un attributo di nome “SIZE”
Element that use new attribute
Legal value for the size attribute
XML Schema 21
Elementi Complessi
Un elemento complesso è definito da: <xs:element name="name">
<xs:complexType> ... information about the complex type... </xs:complexType> </xs:element>
Esempio: <xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstName" type="xs:string" /> <xs:element name="lastName" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element>
XML Schema 22
Kinds of complex elements
empty elements elements that contain only other elements elements that contain only text elements that contain both other elements and text
Note: Each of these elements may contain attributes as well!
XML Schema 23
Examples of Complex Elements
A complex XML element, "product", which is empty:<product pid="1345"/>
A complex XML element, "employee", which contains only other elements:<employee> <firstname>John</firstname> <lastname>Smith</lastname> </employee>
A complex XML element, "food", which contains only text:<food type="dessert">Ice cream</food>
A complex XML element, "description", which contains both elements and text:<description> It happened on <date lang="norwegian">03.03.99</date> .... </description>
XML Schema 24
Complex Element Definition: declared directly by naming the element
If you use the method described above, only the "employee" element can use
the specified complex type.
Note that the child elements, "firstname" and "lastname", are surrounded by
the <sequence> indicator. This means that the child elements must appear in
the same order as they are declared.
XML Schema 25
Complex Element Definition: have a type attribute that refers to the name of the complex type to use
If you use the method described above, several elements
can refer to the same complex type
XML Schema 26
Base a complex element on an existing complex element and add some elements
XML Schema 27
Complex Empty Elements
<product prodid="1345" />
Product element declaration :
<xs:element name="product"> <xs:complexType> <xs:attribute name="prodid" type="xs:positiveInteger"/> </xs:complexType></xs:element>
<xs:element name="product" type="prodtype"/>
<xs:complexType name="prodtype"> <xs:attribute name="prodid" type="xs:positiveInteger"/></xs:complexType>
<xs:element name="product" type="prodtype"/>
<xs:complexType name="prodtype"> <xs:attribute name="prodid" type="xs:positiveInteger"/></xs:complexType>
XML Schema 28
Complex Type - Elements Only
An "elements-only" complex type contains an element that contains only other elements.
<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType></xs:element>
xs:element name="person" type="persontype"/><xs:complexType name="persontype"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence></xs:complexType>s
XML Schema 29
Complex Text-Only Elements
A complex text-only element can contain text and attributes.
<xs:element name="somename"> <xs:complexType> <xs:simpleContent> <xs:extension base="basetype"> .... .... </xs:extension> </xs:simpleContent> </xs:complexType></xs:element>
OR
<xs:element name="somename"> <xs:complexType> <xs:simpleContent> <xs:restriction base="basetype"> .... .... </xs:restriction> </xs:simpleContent> </xs:complexType></xs:element>
Use the extension/restriction element to expand or to limit the base simple type for the element.
XML Schema 30
Complex Types With Mixed Content
A mixed complex type element can contain attributes, elements, and text
<letter>Dear Mr.<name>John Smith</name>.Your order <orderid>1032</orderid>will be shipped on <shipdate>2001-07-13</shipdate>.</letter>
<xs:element name="letter"> <xs:complexType mixed="true"> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="orderid" type="xs:positiveInteger"/> <xs:element name="shipdate" type="xs:date"/> </xs:sequence> </xs:complexType></xs:element>
XML Schema 31
Indicator
Order indicators: are used to define the order of the elements All Choice Sequence
Occurrence indicators: are used to define the order of the elements maxOccurs minOccurs
Group indicators: are used to define related sets of elements Group name attributeGroup name
XML Schema 32
Order Indicator : ALL
The <all> indicator specifies that the child elements can appear in any order, and that each child element must occur only once
<xs:element name="person"> <xs:complexType> <xs:all> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:all> </xs:complexType></xs:element>
Note: When using the <all> indicator you can set the <minOccurs> indicator to 0 or 1 and the <maxOccurs> indicator can only be set to 1
XML Schema 33
Order Indicator : CHOICE
The <choice> indicator specifies that either one child element or another can occur
<xs:element name="person"> <xs:complexType> <xs:choice> <xs:element name="employee" type="employee"/> <xs:element name="member" type="member"/> </xs:choice> </xs:complexType></xs:element>
XML Schema 34
Order Indicator : SEQUENCE
The <sequence> indicator specifies that the child elements must appear in a specific order
<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType></xs:element
XML Schema 35
Occurrence Indicator : maxOccurs
The <maxOccurs> indicator specifies the maximum number of times an element can occur
<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string" maxOccurs="10"/> </xs:sequence> </xs:complexType></xs:element>
The example above indicates that the "child_name" element can occur a minimum of one time (the default value for minOccurs is 1) and a maximum of ten times in the "person" element.
XML Schema 36
Occurrence Indicator : minOccurs
The <maxOccurs> indicator specifies the minimum number of times an element can occur
<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string" maxOccurs="10" minOccurs="0"/> </xs:sequence> </xs:complexType></xs:element>
The example above indicates that the "child_name" element can occur a minimum of zero times and a maximum of ten times in the "person" element.
Tip: To allow an element to appear an unlimited number of times, use the maxOccurs="unbounded" statement:
XML Schema 37
Working example
<?xml version="1.0" encoding="ISO-8859-1"?><persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="family.xsd"><person><full_name>Hege Refsnes</full_name><child_name>Cecilie</child_name></person><person><full_name>Tove Refsnes</full_name><child_name>Hege</child_name><child_name>Stale</child_name><child_name>Jim</child_name><child_name>Borge</child_name></person><person><full_name>Stale Refsnes</full_name></person></persons>
<?xml version="1.0" encoding="ISO-8859-1"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"elementFormDefault="qualified"><xs:element name="persons"> <xs:complexType> <xs:sequence> <xs:element name="person" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string" minOccurs="0" maxOccurs="5"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType></xs:element></xs:schema>
The XML file contains a root element named "persons". Inside this root element we have defined three "person" elements. Each "person" element must contain a "full_name" element and it can contain up to five "child_name" elements
XML Schema 38
Group Indicator : Element/Attribute Group
Element groups are defined with the group declaration, like this:<xs:group name="groupname"> ...</xs:group>
xs:group name="persongroup"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:element name="birthday" type="xs:date"/> </xs:sequence></xs:group>
<xs:element name="person" type="personinfo"/>
<xs:complexType name="personinfo"> <xs:sequence> <xs:group ref="persongroup"/> <xs:element name="country" type="xs:string"/> </xs:sequence></xs:complexType>
XML Schema 39
<any> <anyAttribute>
The <any> and <anyAttribute> elements are used to make EXTENSIBLE documents! They allow documents to contain additional elements that are not declared in the main XML schema.
The following example is a fragment from an XML schema called "family.xsd". It shows a declaration for the "person" element. By using the <any> element we can extend (after <lastname>) the content of "person" with any element:
<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:any minOccurs="0"/> </xs:sequence> </xs:complexType></xs:element>
XML Schema 40
Perchè XML Schema?
DTD fornisce specifiche deboli Nessuna restrizione sul contenuto del testo Poco controllo sui contenuti misti (mixed content, text
+ elements) Poco controllo sull’ordinamento degli elementi
DTD è scritto in un formato non-XML Parser separati per DTD e XML
XML Schema Definition Language risolve questi problemi Più controllo su strutture e contenuti XSD è scritto in XML
XML Schema 41
Riferirsi ad uno schema
Per la DTD il riferimento va prima del root element:<?xml version="1.0"?><!DOCTYPE rootElement SYSTEM "url"><rootElement> ... </rootElement>
Per l’XML Schema il riferimento va nel root element:<?xml version="1.0"?><rootElement
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" (riferimeno richiesto a XML Schema Instance) xsi:noNamespaceSchemaLocation="url.xsd"> (dove trovare lo Schema definition voluto) ...
</rootElement>
XML Schema 42
Specifing Namespaces
Are specified using the xmlns attribute to declare the Namespace with an element. The attribute takes the general form
<element xmlns:prefix="Namespace URI">
the element is the element for which we are declaring the Namespace
The prefix is the prefix we will use with our element names
the Namespace URI is the identifier of the Namespace itself.
The prefix is actually optional—if it is left out, the Namespace declaration is considered the default Namespace, and all the elements in thedocument will be treated as members of that Namespace unless they are marked otherwise.
XML Schema 43
Namespace URI
Generally, this takes the form of a URL, which serves as a good unique identifier, because it includes the domain name, and is easily understood by Web-enabled software.
The most common misconception resulting from the use of a URL here is that the URL necessarily points to something, when,in fact, it does not. The URL is simply used as a string to identify the Namespace —it does not necessarily point to any specific document (although it can if you want it to) and there is no syntax or standard for creating a Namespace document to live on your server.
XML Schema 44
Single Default Namespace
XML Schema 45
Multiple Prefixed Namespaces
To make all the Namespaces in the document require a prefix, we use the same basic structure as declaring a default Namespace, only we add the : prefix to the xmlns attribute
XML Schema 46
The advantage of this method is that now both Namespaces are declared globally.
That is because they are declared in the root element, and the default Namespace applies to any element without a prefix in the document.
Because the second Namespace is also declared globally, any element in the document can be made part of that Namespace by appending the prefix.
Default and Prefixed Namespaces
XML Schema 47
default Namespace globally, and the secondNamespace declared locally
XML Schema 48
list of the Namespaces for some of the technologies discussed