490 likes | 710 Views
XML Schema. Laurea Magistrale in Informatica Chapter 04 Modulo del corso Thecnologies for Innovation. Agenda. Schema characteristics Element Declaration Simple Element Restrictions Complex Elements Indicator Namespaces. XML Schema.
E N D
XML Schema Laurea Magistrale in Informatica Chapter 04 Modulo del corso Thecnologies for Innovation
Agenda • Schema characteristics • Element Declaration • Simple Element • Restrictions • Complex Elements • Indicator • Namespaces XML Schema
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
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
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
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
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
Default and Fixed attribute XML Schema
MaxOccurs and MinOccurs attribute XML Schema
REF and Type attribute XML Schema
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
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 sonoxs: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
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 perxs:element • Altri attributi che gli elementi semplici possono avere: • default="defaultvalue" se nessun valore è specificato • fixed="value”nessun altro valore può essere specificato • use="optional" non richiesto (default) • use="required"richiesto XML Schema
<simpleType> XML Schema
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
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 valuecifre • fractionDigits -- numero deve avere non più di valuecifre dopo il punto decimale XML Schema
Restrizioni su stringhe • length -- la stringa deve contenere valuecaratteri • minLength -- la stringa deve contenere almeno valuecaratteri • maxLength -- la stringa deve contenere non più di valuecaratteri • 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
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
size by value size by name 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". XML Schema
Element that use new attribute Legal value for the size attribute UNIONE per creare un attributo di nome “SIZE” XML Schema
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
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
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
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
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
Base a complex element on an existing complex element and add some elements XML Schema
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> XML Schema
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
Complex Text-Only Elements <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> A complex text-only element can contain text and attributes. Use the extension/restriction element to expand or to limit the base simple type for the element. XML Schema
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
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
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
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
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
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
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
Working example <?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 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 Schema
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
<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
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
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
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 elementis the element for which we are declaring the Namespace The prefixis the prefix we will use with our element names the Namespace URIis 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 the document will be treated as members of that Namespace unless they are marked otherwise. XML Schema
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
Single Default Namespace XML Schema
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
Default and Prefixed Namespaces • 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. XML Schema
default Namespace globally, and the secondNamespace declared locally XML Schema
list of the Namespaces for some of the technologies discussed XML Schema