extensible Markup Language: XML
2 Ο παραδοσιακός Παγκόσµιος Ιστός Ένα απλό, κοινό πρότυπο για ανταλλαγή και διάδοση πληροφοριών. Η πληροφορία κατατέµνεται σε τµήµατα (πληροφοριακές «νησίδες» - ιστοσελίδες), τα οποία µπορούν να κατονοµασθούν και να µεταδοθούν. Ένα τµήµα είναι συνήθως ένα αρχείο το οποίο επονοµάζεται µε ένα URL. Τι είναι η HTML; Ένας τρόπος δόµησης των πληροφοριών µε στόχο την γραφική τους στοιχειοθέτηση-εµφάνιση σε κάποια τερµατική συσκευή. Ένας τρόπος για τον καθορισµό συσχετίσεων ανάµεσα σε στοιχεία του περιεχοµένου του ίδιου αρχείου και ανάµεσα σε διαφορετικά αρχεία.
3 Τι ξέρουµε από τις Βάσεις Δεδοµένων Η δόµηση των πληροφοριών παρουσιάζεται µε εναλλακτικούς τρόπους: Σχεσιακά σχήµατα ΒΔ. Διαγράµµατα Οντοτήτων-Συσχετίσεων. Πως επιτυγχάνουµε την κοινοχρησία των πληροφοριών ΒΔ: Με χρήση γλωσσών επερωτήσεων για πρόσβαση προς τις πληροφορίες. Με µηχανισµούς για έλεγχο ταυτοχρονίας (concurrency control) και ανάνηψη πληροφοριών (recovery). Διαχωρισµός ανάµεσα στη λογική όψη των ΒΔ και στην φυσική τους υλοποίηση.
4 Ανάγκη για γεφύρωση Ιστού και ΒΔ Γιατί υπάρχει ανάγκη γεφύρωσης; Ολοένα και περισσότερα δεδοµένα Ιστού παράγονται από ΒΔ. Προκύπτει συχνά η ανάγκη εξαγωγής δεδοµένων από ιστοσελίδες για ανάλυση, καταγραφή σε ΒΔ κλπ. Π.χ.: Οικονοµικά δεδοµένα ΒΔ, δηµοσιοποιούνται σε κάποια ιστοσελίδα αφού γίνει µορφοποίηση των καταλλήλων επερωτήσεων προς τη ΒΔ σε HTML. Κάποιος άλλος οργανισµός που θέλει να αναλύσει τα δεδοµένα αυτής της ιστοσελίδας, έχει πρόσβασή µόνο στον κώδικα HTML (µέσω Διαδικτύου). Η κωδικοποίηση των δεδοµένων σε HTML έχει τα ακόλουθα µειονεκτήµατα: Αστάθεια: τα προγράµµατα ανάλυσης ιστοσελίδων «σπάνε» εύκολα µετά από επουσιώδεις αλλαγές στον κώδικα της ΗΤΜL (και όχι στη λογική δοµή των δεδοµένων). Σπατάλη πόρων: για την ανάκτηση της χρήσιµης πληροφορίας µπορεί να χρειάζεται να ανακτήσουµε όγκο αχρείαστων πληροφοριών µεγαλύτερο από αυτό που θα παίρναµε µε απευθείας εκτέλεση επερώτησης προς κάποια βάση.
5 Ανάγκη για γεφύρωση Ιστού και ΒΔ Από την σκοπιά των ψηφιακών εγγράφων, θέµατα όπως η γρήγορη ανάκτηση, ο έλεγχος έκδοσης, οι επερωτήσεις πάνω στα έγγραφα κοκ καθίστανται ολοένα και πιο σηµαντικά. Από την σκοπιά των ΒΔ, ο Ιστός έχει δηµιουργήσει ζήτηση για νέες αρχιτεκτονικές και έχει οδηγήσει στη δηµιουργία µοντέλων και γλωσσών για ηµιδοµηµένα δεδοµένα.
6 XML Μια µετα-γλώσσα για κωδικοποίηση πληροφοριών. Απλό συνταντικό το οποίο µπορεί να διαβασθεί και από άνθρωπο και από µηχανή. Ένα πρώτο βήµα προς τη σύγκλιση ιστού και ΒΔ: Συντακτικό παρόµοιο µε της HTML (υποσύνολο της SGML). Περιγράφει δοµηµένα δεδοµένα. Ώριµη διαπροσωπεία και ευσταθή εργαλεία σάρωσης, ανεξάρτητα της εµφάνισης-παρουσίασης του περιεχοµένου. Από µόνη της η XML δεν αρκεί για την εξαγωγή δεδοµένων από ΧΜL αρχεία, αλλά χρειάζεται και άλλες τεχνολογίες όπως γλώσσες επερωτήσεων σε σχήµατα ΧΜL, οντολογίες, κοκ.
7 XML New standard adopted by W3C to complement HTML for data exchange. HTML describes the structure of the presentation and not the structure of the content. XML describes the content (stylesheets can be used for defining how to display an XML document) XML allows the definition of new tags. An XML document can contain an optional description of its grammar.
8 Η οικογένεια της XML XML: µετα-γλώσσα, η οποία χρησιµοποιείται για τον ορισµό γλωσσών σήµανσης. XML Namespaces: προδιαγραφή για την αποφυγή ονοµατολογικών συγκρούσεων µέσω καθορισµού χώρων ονοµάτων για έγγραφα XML. XML Schema: καθορίζει τύπους στοιχείων που µπορούν να περιέχονται σε ένα έγγραφο XML, και τα δεδοµένα που µπορούν να περιλαµβάνουν αυτοί οι τύποι. XPath και XPointer: επιτρέπουν την αναφορά σε τµήµατα ενός εγγράφου XML και τον ορισµό fragment identifiers. XLink: δίνει τη δυνατότητα ορισµού υπερσυνδέσµων ανάµεσα σε έγγραφα XML. XForms: επιτρέπει τον καθορισµό φορµών εισαγωγής στοιχείων σε XML. XML Signature: παράσχει συντακτικό και κανόνες επεξεργασίας για ψηφιακές υπογραφές σε XML.
9 Η οικογένεια της XML (συνέχεια) DOM (document object model): API για την περιγραφή ενός εγγράφου XML ως δένδρου κόµβων και για τον καθορισµό του πως οι κόµβοι αυτοί είναι δοµηµένοι, πως µπορούµε να έχουµε πρόσβαση σε αυτούς και πως µπορούµε να τους αλλάξουµε. XSL (extensible Stylesheet Language): αποτελείται από το XSL-T (XSL Transformations) και XSL-FO (XSL Formatting Objects) και είναι µια γλώσσα για µετασχηµατισµό εγγράφων XML σε άλλα έγγραφα XML και για την αναπαράστασή τους, π.χ. σε HTML, Braille, κλπ. XQuery : γλώσσα επερωτήσεων XML εγγράφων, η οποία θεωρεί ένα XML αρχείο σαν ΒΔ. CSS: απλή γλώσσα περιγραφής οδηγών στοιχειοθεσίας. XHTML: ορισµός της HTML4.0 σε XML.
10 Η οικογένεια της XML (συνέχεια) RDF (Resource Description Framework): ακρογωνιαίος λίθος του σηµασιολογικού Ιστού (semantic web). Ορίζει ένα απλό µοντέλο δεδοµένων για την περιγραφή των σηµασιολογικών χαρακτηριστικών πόρων του Ιστού. RDF Schema: σχήµα για τον ορισµό γλωσσών RDF. DAML+OIL (DARPA Agent Markup Language + Ontology Inference Layer): γλώσσες για την περιγραφή οντολογιών που επεκτείνουν το RDF Schema.
11 Παράδειγµα εγγράφου XML <?xml version="1.0"?> <!DOCTYPE telegram SYSTEM "/xml-resources/dtds/telegram.dtd"> <telegram pri="important"> <to>sarah Bellum</to> <from>colonel Timeslip</from> <subject>robot-sitting instructions</subject> <graphic fileref="figs/me.jpg"/> <message>thanks for watching my robot pal <name>zonky</name> while I'm away. He needs to be recharged <emphasis>twice a day</emphasis> and if he starts to get cranky, give him a quart of oil. I'll be back soon, after I've tracked down that evil mastermind <villain>dr. Indigo Riceway</villain>. </message> </telegram>
12 Χρήση XML: αποθήκευση/ανάκτηση δεδοµένων Για την επεξεργασία δεδοµένων εκπεφρασµένων σε XML χρησιµοποιούµε σαρωτές XML, οι οποίοι διαβάζουν XML περιεχόµενο και το µετατρέπουν σε πιο εύχρηστη µορφή. Η XML δουλεύει καλύτερα στην περίπτωση µικρών αρχείων δεδοµένων, στα οποία δεν γίνεται τυχαία αναζήτηση ή πρόσβαση (random search/access), καθώς η XML αποτελεί σειριακό µέσο αποθήκευσης. Υπάρχουν συστήµατα και προτάσεις για υπέρβαση αυτού του περιορισµού.
13 Παράδειγµα <caldata> <holiday type="international"> <name>new Year's Day</name> <date><month>january</month><day>1</day></date> </holiday> <holiday type="personal"> <name>erik's birthday</name> <date><month>april</month><day>23</day></date> </holiday> <holiday type="national"> <name>independance Day</name> <date><month>july</month><day>4</day></date> </holiday> <holiday type="religious"> <name>religious</name> <date><month>december</month><day>25</day></date> </holiday> </caldata>
Χρήση XML: Στοιχειοθεσία εγγράφων Με χρήση CSS και XML, προσθέτοντας ειδικές εντολές στα αρχεία XML: <?xml-stylesheet type= text/css href= ex2_memo.css?> Με µετασχηµατισµό εγγράφου XML σε έγγραφο άλλης γλώσσας σήµανσης (π.χ. HTML 4.0). Οι κανόνες του µετασχηµατισµού περιγράφονται µε γλώσσα XSLT. Με µετασχηµατισµό σε ενδιάµεση µορφή, στην XSL-FO, µε χρήση XSLT. Τα formatting objects είναι αφαιρετικές αναπαραστάσεις τµηµάτων ενός εγγράφου XML. Στη συνέχεια, τα FΟs µετατρέπονται σε µια µορφή αναπαράστασης (π.χ. PDF) µέσω κάποιου εξειδικευµένου XSL formatter. Με προγραµµατισµό µέσω κάποιας γενικής γλώσσας προγραµµατισµού, όπως η JAVA. 14
15 Χρήση XML: ακεραιότητα δεδοµένων Η XML µας δίνει τη δυνατότητα να διασφαλίζουµε έναν µίνιµουµ επίπεδο εµπιστοσύνης στα δεδοµένα µας µε µηχανισµούς όπως: Έλεγχος απλών συντακτικών κανόνων (well-formedness) µε απλούς σαρωτές (parsers). Έλεγχος «καλής σύνταξης» µε σαρωτές σύνταξης (validating parsers), µε βάση κάποιο µοντέλο εγγράφου (document model) εκφρασµένου ως: Document Type Definition (DTD file): δεν υποστηρίζει πολλούς ελέγχους στο περιεχόµενο XML Schema: προσφέρει µεγαλύτερες δυνατότητες ελέγχου της σύνταξης των στοιχείων ενός εγγράφου XML.
16 Παράδειγµα DTD <!ELEMENT telegram (from,to,subject,graphic?,message)> <!ATTLIST telegram pri CDATA #IMPLIED> <!ELEMENT from (#PCDATA)> <!ELEMENT to (#PCDATA)> <!ELEMENT subject (#PCDATA)> <!ELEMENT graphic EMPTY> <!ATTLIST graphic fileref CDATA #REQUIRED> <!ELEMENT message (#PCDATA emphasis name villain)*> <!ELEMENT emphasis (#PCDATA)> <!ELEMENT name (#PCDATA)>
Παράδειγµα XML Schema <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema"> <xs:element name="telegram" type="telegramtype" /> <xs:complextype name="telegramtype"> <xs:sequence> <xs:element name="to" type="texttype" /> <xs:element name="from" type="texttype" /> <xs:element name="subject" type="texttype" /> <xs:element name="graphic" type="graphictype" /> <xs:element name="message" type="messagetype" /> </xs:sequence> <xs:attribute name="pri" type="xs:token" /> </xs:complextype> 17
Παράδειγµα XML Schema <xs:simpletype name="texttype"> <xs:restriction base="xs:string"> <xs:minlength value="1" /> </xs:restriction> </xs:simpletype> <xs:complextype name="graphictype"> <xs:attribute name="fileref" type="xs:anyuri" use="required" /> </xs:complextype> <xs:complextype name="messagetype" mixed="true"> <xs:choice minoccurs="0" maxoccurs="unbounded"> <xs:element name="emphasis" type="xs:string" /> <xs:element name="name" type="xs:string" /> <xs:element name="villain" type="xs:string" /> </xs:choice> </xs:complextype> </xs:schema> 18
19 Χρήση XML: υποστήριξη πολλών γλωσσών Υιοθετεί το σύνολο χαρακτήρων (character set) Unicode, το οποίο υποστηρίζει διαφορετικές γραφές: λατινική, αραβική, ελληνική, εβραϊκή κλπ. Υποστηρίζει διαφορετικές κωδικοποιήσεις των χαρακτήρων. Π.χ.: UTF-8, iso-8859-1 κλπ.
20 Πως ξεκινάµε; Συγγραφή XML: µε χρήση του emacs µε το κατάλληλο plugin (psgml), µε χρήση εργαλείων όπως το XMLSpy. Εµφάνιση-στοιχειοθεσία αρχείων XML: µε χρήση σύγχρονων φυλλοµετρητών (IE, Amaya, Mozilla 1.4, Opera, Safari). Σάρωση: η επεξεργασία ενός XML αρχείου προϋποθέτει καταρχάς τη σάρωσή του για τον διαχωρισµό και την αναγνώριση των στοιχείων που περιέχονται σε αυτό και την περαιτέρω επεξεργασία τους. Στη συνέχεια, τον συνταντικό έλεγχο του αρχείου, µέσω validating parser, όπως ο Xerxes της Apache.
21 Πως ξεκινάµε; Μετασχηµατισµός: απαιτεί το αρχικό έγγραφο και ένα stylesheet µετασχηµατισµού (συνήθως κωδικοποιηµένο σε XSLT). Ο µετασχηµατισµός γίνεται από έναν XSLT processor, όπως ο Xalan της Apache. Στοιχειοθεσία για εκτύπωση: Η χρήση απευθείας µετασχηµατισµού από XML σε εκτυπώσιµη κωδικοποίηση (pdf, ps κλπ) είναι πολύ δύσκολη. Γι αυτό, χρησιµοποιούµε µετασχηµατισµό σε µια ενδιάµεση XML µορφή, την XSL-FO, η οποία περιλαµβάνει δεδοµένα και οδηγίες στοιχειοθεσίας. Η δηµιουργία της XSL-FΟ µορφής γίνεται µε χρήση XSLT. Η XSL-FO µορφή µετατρέπεται σε PDF από µορφοποιητές όπως ο XSL-FO της apache.
22 Πως ξεκινάµε; Προγραµµατισµός: αναπτύσσοντας προγράµµατα τα οποία επεξεργάζονται έγγραφα XML και τα οποία ακολουθούν δύο πιθανές στρατηγικές: Push: αντιστοιχεί σε stream processing ενός αρχείου XML. Ο σαρωτής διαβάζει το έγγραφο και καλεί το πρόγραµµά µας για την διαχείριση του κάθε νέου στοιχείου XML που προκύπτει στην ροή. Γρήγορη µέθοδος, αλλά µε περιορισµένη ευελιξία. Pull: επιτρέπει την ανάγνωση ολόκληρου του εγγράφου XML και την αποθήκευσή του σαν δοµή δεδοµένων (δένδρο). Στη συνέχεια, το πρόγραµµά µας µπορεί να διαχειριστεί τη δοµή αυτή.
23 SAX και DOM Για την ενσωµάτωση των τεχνικών αυτών σε προγράµµατα, έχουν ορισθεί δύο αντίστοιχες προδιαγραφές προγραµµατιστικών διαπροσωπειών: Simple API for XML (SAX): καθορίζει πως ένας σαρωτής αλληλεπιδρά µε ένα πρόγραµµα κατά τη διάρκεια της επεξεργασίας ροής εγγράφων XML. Document Object Model (DOM): καθορίζει µεθόδους για πρόσβαση στη δενδρική δοµή δεδοµένων, η οποία αποθηκεύει ένα έγγραφο XML.
Βασικά στοιχεία XML εγγράφων
Αντικείµενα XML 25
26 Δοµή εγγράφων XML Πρόλογος Εγγράφου (document prolog): Περιέχει µεταδεδοµένα σχετικά µε το έγγραφο. Η χρήση του είναι προαιρετική. Στοιχείο ρίζας (root element): Περιλαµβάνει όλα τα στοιχεία που ανήκουν στο έγγραφο.
Δοµή Εγγράφου XML 27
28 XML declaration Προαιτετική χρήση - αν χρησιµοποιηθεί πρέπει να βρίσκεται στην πρώτη γραµµή ενός εγγράφου. Οι παράµετροι έχουν τη µορφή: name = value όπου το name είναι: version: έκδοση της XML encoding: χρησιµοποιούµενο σύνολο χαρακτήρων standalone: ενηµερώνει τον σαρωτή αν υπάρχουν δηλώσεις εκτός του εγγράφου, οι οποίες να χρειάζονται για την σύνταξη και επικύρωση του.
29 Document Type Declaration Χρησιµοποιείται για: Τον ορισµό των στοιχείων (elements) και των κατηγορηµάτων (attributes) που υπάρχουν σε ένα έγγραφο XML. Την υποστήριξη σαρωτών επικύρωσης (validating parsers), οι οποίοι ελέγχουν την γραµµατική και το λεξιλόγιο των γλωσσών σήµανσης.
30 Περιεχόµενα Document Type Declaration Element: προσδιορίζει τον τύπο του XML εγγράφου. DTD identifier (προσδιοριστής γραµµατικής εγγράφου) : προαιρετικός προσδιορισµός της γραµµατικής (document type definition) του εγγράφου. Μπορεί να έχει τη µορφή: SYSTEM identifier: προσδιορίζει τη φυσική διεύθυνση ενός αρχείου στο σύστηµά µας, στο οποίο περιέχεται η γραµµατική. PUBLIC identifier: Προσδιορίζει µέσω µοναδικού ονόµατος την γραµµατική που ακολουθεί το αρχείο µας. Επειδή το όνοµα αυτό µπορεί να µην είναι γνωστό σε κάποιον σαρωτή, συνοδεύεται από το URL ενός αρχείου το οποίο ορίζει τη γραµµατική.
System identifiers 31
Public identifiers 32
Declarations Περιλαµβάνει ορισµούς που είναι χρήσιµοι για την συναρµολόγηση και την επικύρωση του εγγράφου. Εµπλουτίζουν τις δηλώσεις που υπάρχουν στο DTD του XML εγγράφου µας. Ένας σαρωτής XML θα διαβάσει πρώτα τις δηλώσεις που υπάρχουν στο εξωτερικό DTD αρχείο (που ορίζεται από τον προσδιοριστή της γραµµατικής του εγγράφου) και µετά θα διαβάσει τις δηλώσεις που είναι ενσωµατωµένες στον πρόλογο του αρχείου XML. Παράδειγµα: Δηλώσεις οντοτήτων (entity declarations) Δηλώσεις στοιχείων (element declarations) 33
34 Παράδειγµα προλόγου <?xml version="1.0" standalone="no"?> <!DOCTYPE reminder Beginning of the DOCTYPE declaration Root element name SYSTEM "/home/eray/reminder.dtd" DTD identifier [ Internal subset start delimiter <!ENTITY smile "<graphic file="smile.jpg"/>"> Entity declaration ]> Internal subset end delimiter <reminder> ⌣ Start of document element Reference to the entity declared above <msg>smile! It can always get worse.</msg> </reminder> End of document element
35 Στοιχεία εγγράφων XML (doc. Elements) Τα στοιχεία (elements) είναι τα συστατικά ενός εγγράφου XML, τα οποία διαιρούν το έγγραφο σε µια ιεραρχία περιοχών, καθορίζοντας τη λογική δοµή του εγγράφου. Υπάρχουν: Στοιχεία εγκυβωτισµού (container elements) τα οποία περιέχουν άλλα στοιχεία ή κείµενο. Άδεια στοιχεία (empty elements), τα οποία χρησιµοποιούνται για να σηµειώσουν ένα σηµείο στο οποίο πρέπει να γίνει ειδική επεξεργασία ή να ενσωµατωθεί κάποιος ειδικός µορφότυπος.
36 XML Elements XML-element: a piece of text bounded by matching tags. Inside the tags there may be a combination of text and other elements. Tags are user-defined. <person> </person> <name> Alan </name> <age> 42 </age> <email> agb@abc.com</email> Tags must be balanced. Abbreviation for empty elements: <married></married> can be abbreviated to <married/>
XML Elements 37
38 Κατηγορήµατα στοιχείων (attributes) Δίνουν τη δυνατότητα να προσθέτουµε περισσότερες πληροφορίες σε ένα στοιχείο XML. Τα κατηγορήµατα ορίζονται σαν ζεύγη (name,value) και εντάσσονται µετά το όνοµα του εναρκτήριου σηµαντήρα ενός στοιχείου. Π.χ.: <product> <name language= French >trompette six trous</ name> <price currency= Euro > 420.12 </price> </product> Η τιµή ενός κατηγορήµατος είναι πάντοτε µια συµβολοσειρά (string) που πρέπει να περικλείεται σε εισαγωγικά. Ένα κατηγόρηµα µπορεί να υπάρξει το πολύ µία φορά µέσα σε έναν σηµαντήρα.
39 XML Attributes XML attributes provide an alternative way of representing elements. <person> <name> Alan </name> <age> 42 </age> <email> agb@abc.com</email> </person> <person name= Alan age= 42 email= agb@abc.com />
40 XML Entities (οντότητες XML) Αποτελούν µηχανισµό καθορισµού συντοµογραφιών Διευκολύνουν την ενσωµάτωση σε αρχείο XML: Χαρακτήρων που είναι δύσκολο ή αδύνατο να πληκτρολογηθούν. Κειµένου το οποίο επαναλαµβάνεται συχνά. Κειµένου και σηµάνσεων που βρίσκονται σε κάποιο εξωτερικό αρχείο. Η εισαγωγή γίνεται µέσω παραποµπής στο όνοµα της οντότητας.
Entity Declarations : placeholders 41
42 Είδη οντοτήτων XML Οι παραµετρικές οντότητες εµφανίζονται mόνο µέσα σε DTDs
Κατηγορίες Οντοτήτων XML Οντότητες χαρακτήρων: Προκαθορισµένες: amp, apos, gt, lt, quot. Αριθµητικές παραποµπές χαρακτήρων Unicode: &# decimal number; &#x hexadecimal number; Μνηµονικά ονόµατα χαρακτήρων Unicode: Þ Μεικτού περιεχοµένου: µη πεπερασµένου µήκους, µπορεί να συµπεριλαµβάνουν σεσηµασµένο περιεχόµενο και κείµενο. Δύο είδη: Εσωτερικές: το περιεχόµενο αντικατάστασης καθορίζεται µε τη δήλωση της οντότητας. Εξωτερικές: το περιεχόµενο αντικατάστασης βρίσκεται σε άλλο αρχείο (αποτελούν µηχανισµό ενσωµάτωσης αποσπασµάτων XML µέσα σε ένα αρχείο). Στη δήλωση της οντότητας πρέπει να καθορίζουµε τη θέση του περιεχοµένου αντικατάστασης. Μη σαρωνόµενες οντότητες (unparsed entities): χρησιµοποιούνται για την περιγραφή περιεχοµένου το οποίο δεν πρέπει να σαρωθεί γιατί περιέχει πολυµεσική κωδικοποίηση. 43
Παραδείγµατα Οντοτήτων XML 44
Παραδείγµατα Οντοτήτων XML 45
XML Trees Conceptually, an XML document is a tree structure node, edge root, leaf child, parent sibling (ordered), ancestor, descendant An Introduction to XML and Web Technologies 46
An Analogy: File Systems An Introduction to XML and Web Technologies 47
Recipes in XML An Introduction to XML and Web Technologies 48
Recipes in XML Define our own Recipe Markup Language Choose markup tags that correspond to concepts in this application domain recipe, ingredient, amount,... An Introduction to XML and Web Technologies 48
Recipes in XML Define our own Recipe Markup Language Choose markup tags that correspond to concepts in this application domain recipe, ingredient, amount,... No canonical choices granularity of markup? structuring? elements or attributes?... An Introduction to XML and Web Technologies 48
Example (1/2) <collection> <description>recipes suggested by Jane Dow</description> <recipe id="r117"> <title>rhubarb Cobbler</title> <date>wed, 14 Jun 95</date> <ingredient name="diced rhubarb" amount="2.5" unit="cup"/> <ingredient name="sugar" amount="2" unit="tablespoon"/> <ingredient name="fairly ripe banana" amount="2"/> <ingredient name="cinnamon" amount="0.25" unit="teaspoon"/> <ingredient name="nutmeg" amount="1" unit="dash"/> <preparation> <step> Combine all and use as cobbler, pie, or crisp. </step> </preparation> An Introduction to XML and Web Technologies 49
Example (2/2) <comment> Rhubarb Cobbler made with bananas as the main sweetener. It was delicious. </comment> <nutrition calories="170" fat="28%" carbohydrates="58%" protein="14%"/> <related ref="42">garden Quiche is also yummy</related> </recipe> </collection> An Introduction to XML and Web Technologies 50
Tree View of the XML Recipes An Introduction to XML and Web Technologies 51
Nodes in XML Trees An Introduction to XML and Web Technologies 52
Nodes in XML Trees Text nodes: carry the actual contents, leaf nodes An Introduction to XML and Web Technologies 52
Nodes in XML Trees Text nodes: carry the actual contents, leaf nodes Element nodes: define hierarchical logical groupings of contents, each have a name An Introduction to XML and Web Technologies 52
Nodes in XML Trees Text nodes: carry the actual contents, leaf nodes Element nodes: define hierarchical logical groupings of contents, each have a name Attribute nodes: unordered, each associated with an element node, has a name and a (string) value An Introduction to XML and Web Technologies 52
Nodes in XML Trees Text nodes: carry the actual contents, leaf nodes Element nodes: define hierarchical logical groupings of contents, each have a name Attribute nodes: unordered, each associated with an element node, has a name and a (string) value Comment nodes: ignorable meta-information An Introduction to XML and Web Technologies 52
Nodes in XML Trees Text nodes: carry the actual contents, leaf nodes Element nodes: define hierarchical logical groupings of contents, each have a name Attribute nodes: unordered, each associated with an element node, has a name and a (string) value Comment nodes: ignorable meta-information Processing instructions: instructions to specific XML processors, each have a target and a value An Introduction to XML and Web Technologies 52
Nodes in XML Trees Text nodes: carry the actual contents, leaf nodes Element nodes: define hierarchical logical groupings of contents, each have a name Attribute nodes: unordered, each associated with an element node, has a name and a (string) value Comment nodes: ignorable meta-information Processing instructions: instructions to specific XML processors, each have a target and a value Root nodes: every XML tree has one root node that represents the entire tree An Introduction to XML and Web Technologies 52
Textual Representation An Introduction to XML and Web Technologies 53
Textual Representation Text nodes: written as the text they carry An Introduction to XML and Web Technologies 53
Textual Representation Text nodes: written as the text they carry Element nodes: start-end tags An Introduction to XML and Web Technologies 53
Textual Representation Text nodes: written as the text they carry Element nodes: start-end tags <bla...>... </bla> An Introduction to XML and Web Technologies 53
Textual Representation Text nodes: written as the text they carry Element nodes: start-end tags <bla...>... </bla> short-hand notation for empty elements: <bla/> An Introduction to XML and Web Technologies 53
Textual Representation Text nodes: written as the text they carry Element nodes: start-end tags <bla...>... </bla> short-hand notation for empty elements: <bla/> Attribute nodes: name= value or name= value in start tags An Introduction to XML and Web Technologies 53
Textual Representation Text nodes: written as the text they carry Element nodes: start-end tags <bla...>... </bla> short-hand notation for empty elements: <bla/> Attribute nodes: name= value or name= value in start tags Comment nodes: <!-- bla --> An Introduction to XML and Web Technologies 53
Textual Representation Text nodes: written as the text they carry Element nodes: start-end tags <bla...>... </bla> short-hand notation for empty elements: <bla/> Attribute nodes: name= value or name= value in start tags Comment nodes: <!-- bla --> Processing instructions: <?target value?> An Introduction to XML and Web Technologies 53
Textual Representation Text nodes: written as the text they carry Element nodes: start-end tags <bla...>... </bla> short-hand notation for empty elements: <bla/> Attribute nodes: name= value or name= value in start tags Comment nodes: <!-- bla --> Processing instructions: <?target value?> Root nodes: implicit An Introduction to XML and Web Technologies 53
Browsing XML (without XSLT) An Introduction to XML and Web Technologies 54
Ορισµός Γραµµατικών XML
56 Σχήµατα XML (Schemas) Σχήµα (schema): γενική αναπαράσταση µιας κατηγορίας πραγµάτων. Τι είναι το σχήµα: ένα µοντέλο για την περιγραφή της δοµής πληροφοριών ενός αρχείου. Στο πλαίσιο της XML περιγράφει ένα µoντέλο για µια κατηγορία εγγράφων, προσδιορίζοντας: Επιτρεπόµενους συνδυασµούς σηµαντήρων και περιεχοµένου σε ένα έγκυρο έγγραφο.
57 Σχήµατα XML (Schemas) Τα µοντέλα περιγράφονται σαν περιορισµοί (constraints). Ένας περιορισµός καθορίζει τι σηµαντήρες, περιεχόµενο κλπ µπορούν να εµφανίζονται σε κάποιο συγκεκριµένο συγκείµενο. Ένας περιορισµός µπορεί να είναι: Μοντέλου περιεχοµένου (content model constraint), ο οποίος προσδιορίζει τη διάταξη και τη σειρά υπαρχόντων στοιχείων Τύπου δεδοµένων, ο οποίος προσδιορίζει έγκυρους τύπους δεδοµένων (π.χ. σε κάποιο στοιχείο XML).
58 Σχήµατα XML Μπορούµε να το δούµε σαν: µια συµφωνία για ένα κοινό λεξιλόγιο ανάµεσα σε εφαρµογές που ανταλλάσσουν έγγραφα ένα τρόπο ελέγχου της εγκυρότητας εγγράφων. Ένα XML σχήµα ενηµερώνει έναν επεξεργαστή περιεχοµένου XML για το πώς αυτός θα πρέπει να διαβάσει και να επεξεργαστεί το έγγραφο. Ο επεξεργαστής διαβάζει τους κανόνες και τις δηλώσεις του σχήµατος και τα χρησιµοποιεί για να παράξει έναν σαρωτή επικύρωσης, ο οποίος µπορεί στη συνέχεια να ελέγξει την εγκυρότητα οποιουδήποτε αρχείου XML που ακολουθεί το συγκεκριµένο σχήµα.
59 Έλεγχος εγκυρότητας Αφορά σε τέσσερα επίπεδα: Δοµή: χρήση και τοποθέτηση σηµαντήρων και κατηγορηµάτων. Τύπος δεδοµένων: ορθότητα περιεχοµένων στοιχείων XML. Ακεραιότητα: η κατάσταση των ζεύξεων ανάµεσα σε κόµβους και πόρους. Επιχειρησιακοί κανόνες (business rules): διάφοροι λογικοί έλεγχοι. An XML document is well-formed (καλά δοµηµένο), if it has only one root its tags nest properly, and its attributes are unique. An XML document is valid (έγκυρο), if it has a DTD and it conforms to that DTD
60 Η χρησιµότητα των σχηµάτων Αποτελούν δηµοσιοποιήσιµες προδιαγραφές γραµµατικής εγγράφων: µας επιτρέπουν να περιγράψουµε µε συνοπτικό τρόπο το πως πρέπει να οργανώνονται έγγραφα µιας συγκεκριµένης κατηγορίας. Επιτρέπουν την ανίχνευση συντακτικών σφαλµάτων στη δοµή εγγράφων XML. Αποτελούν έναν φορητό και αποδοτικό τρόπο ελέγχου της εγκυρότητας εγγράφων XML. Είναι επεκτάσιµα και µπορούν να επαναχρησιµοποιηθούν από άλλα σχήµατα.
61 Document Type Definition (DTD) Ο παλαιότερος µηχανισµός ορισµού του µοντέλου (γραµµατικής) εγγράφων XML. Αποτελεί τρόπο περιγραφής µιας γραµµατικής ανεξάρτητης από τα συµφραζόµενα (context-free grammar) για κατηγορίες εγγράφων XML. Ένα DTD είναι ένα σύνολο κανόνων (rules) ή δηλώσεων (declarations), καθένας από τους οποίους εισαγάγει ένα νέο στοιχείο, κατηγόρηµα, οντότητα ή συµβολισµό της γραµµατικής που ορίζει. Επιτρέπεται η χρήση αναδροµικών ορισµών: <!ELEMENT node (leaf (node, node))> <!ELEMENT leaf (#PCDATA)>
62 DTDs Ένα DTD µιας κατηγορίας εγγράφων XML: Δηλώνει το σύνολο των στοιχείων που µπορούν να υπάρξουν σε έγγραφα αυτής της κατηγορίας. Καθορίζει τη δοµή και τα περιεχόµενα του κάθε στοιχείου. Δηλώνει τα κατηγορήµατα που επιτρέπονται σε κάθε στοιχείο. Παρέχει µηχανισµούς που καθιστούν ευκολότερη τη διαχείριση µοντέλων XML. Η σειρά εµφάνισης των δηλώσεων είναι σηµαντική όταν: Πρόκειται για πολλαπλές δηλώσεις της ίδιας οντότητας (προτεραιότητα στην πρώτη δήλωση). Έχουµε παραµετρικές οντότητες, οι δηλώσεις των οποίων πρέπει να προηγούνται της χρήσης των οντοτήτων.
63 DTDs Ένα DTD µπορεί να βρίσκεται: Ενσωµατωµένο σε ένα XML αρχείο Αποθηκευµένο σε ξεχωριστό αρχείο, στο οποίο γίνεται παραποµπή µέσα από το έγγραφο XML: <!DOCTYPE bibliography SYSTEM biblio.dtd > Αποθηκευµένο σε αρχείο κάπου στο διαδίκτυο (το οποίο να διαθέτει URL): <!DOCTYPE bibliography SYSTEM http://www. /biblio.dtd >
64 Παράδειγµα Document Type Definition <! DOCTYPE bibliography [ <!ELEMENT book (title, author+, price, review*, bestseller?)> <!ELEMENT title (#PCDATA)> <!ELEMENT author ( #PCDATA lastname firstname fullname)*> <!ELEMENT lastname (#PCDATA>)> <!ELEMENT firstname (#PCDATA>)> <!ELEMENT fullname (firstname, lastname>)> <!ELEMENT price (#PCDATA>)> <!ATTLIST price currency CDATA #REQUIRED taxed CDATA #FIXED yes > <!ELEMENT bestseller EMPTY> ]>
65 Δήλωση κατηγορηµάτων <! DOCTYPE family [ ]> <!ELEMENT family (person)*> <!ELEMENT person (name)> <!ELEMENT name (#PCDATA)> <!ATTLIST person id ID #REQUIRED mother IDREF #IMPLIED father IDRED #IMPLIED children IDREFS #IMPLIED> Type ID declares attributes that define the entity s unique identifier within the scope of a document. Attributes of type IDREF (IDREFS) have values that are some other element s identifier(s).
66 Οδηγίες σχεδιασµού DTDs Οργανώστε τις δηλώσεις σε διαφορετικές παραγράφους, ανάλογα µε την χρησιµότητά τους. Χρησιµοποιείστε κενό χώρο και σχόλια για να κάνετα τα DTD σας πιο ευανάγνωστα. Οργανώστε τις αλλαγές που κάνετε σε διαδοχικές εκδόσεις (versions). Η αποθήκευσή τους µπορεί να γίνεται µε συστήµατα όπως το RCS, το CVS και το SVN. Χρησιµοποιείστε παραµετρικές οντότητες (parameter entities) για την δήλωση δηλώσεων XML, οι οποίες επαναλαµβάνονται συχνά σε διάφορα σηµεία. Π.χ.: <!ENTITY % common.attrs id ID #IMPLIED Class CDATA #IMPLIED > Οι παραµετρικές οντότητες χρησιµοποιούνται µόνο µέσα στα DTD και περιέχουν τον χαρακτήρα % στον ορισµό τους. Παραποµπή σε παραµετρική οντότητα ξεκινάει µε % αντί µε &.
67 Οδηγίες σχεδιασµού DTD Χρησιµοποιείτε απλά και µνηµονικά ονόµατα στοιχείων. Αξιοποιείστε τις δυνατότητες ιεραρχικής οργάνωσης της πληροφορίας που σας παρέχει η XML. Είναι προτιµητέο να δηµιουργείτε δένδρα µε εξισορροπηµένο σχήµα (ούτε πολύ βαθειά, ούτε πολύ πλατιά). Προσέξτε πότε θα χρησιµοποιείτε κατηγορήµατα και πότε θα χρησιµοποιείτε στοιχεία. Διασπάστε µεγάλα DTD σε περισσότερα του ενός δοµοστοιχεία, τα οποία αποθηκεύονται χωριστά και ενσωµατώνονται στο κεντρικό αρχείο ενός DTD.
68 Ενσωµάτωση δοµοστοιχείων DTD Για την ενσωµάτωση εξωτερικών δοµοστοιχείων σε ένα DTD χρησιµοποιούµε παραµετρικές οντότητες. Π.χ.: <!ELEMENT catalog (title, metadata, front, entries+)> <!ENTITY % basic.stuff SYSTEM basics.mod > %basic.stuff; <!ENTITY % front.matter SYSTEM front.mod > %front.matter; <!ENTITY % metadata PUBLIC -Standards Stuff//DTD Metadata v3.2//en http://www.standards.org/dtds/metadata.dtd> %metadata
69 Μειονεκτήµατα DTD DTDs can be used to define content model constraints and some datatype constraints on attributes. They have the following limitations: They are written in non-xml syntax. They do not support namespaces. They offer very limited datatyping. They do not make relationships between elements with the same content model explicit. However, they are widely spread and well-understood, they have widespread tool support.
XML Namespaces
71 Ονοµατολογίες στην XML Ένας τρόπος για να διαχωρίζουµε στοιχεία και ιδιοχαρακτηριστικά ενός εγγράφου XML, είναι η χρήση των «ονοµατολογιών» (namespaces), οι οποίες προσδιορίζονται µε αντιστοίχισή τους σε µοναδικά URI. Η χρήση των ονοµατολογιών επιτρέπει σε ένα µοναδικό έγγραφο XML να περιλαµβάνει κατηγορίες στοιχείων και ιδιοχαρακτηριστικών, οι οποίες τυγχάνουν ειδικής διαχείρισης από δοµοστοιχεία λογισµικού. Για παράδειγµα, µπορούµε σε ένα έγγραφο XML να χρησιµοποιήσουµε σηµαντήρες της MathML, οι οποίοι είναι αναγνωρίσιµοι και επεξεργάσιµοι από ειδικό λογισµικό στοιχειοθεσίας µαθηµατικών εκφράσεων.
Ονοµατολογίες στην XML Μια ονοµατολογία XML (namespace) είναι µια συλλογή ονοµάτων, που αναγνωρίζονται µέσω ενός κοινού ονόµατος που παραπέµπει σε ένα µοναδικό URI. Το όνοµα χρησιµοποιείται σε έγγραφα ΧML για τον επιθετικό προσδιορισµό στοιχείων και κατηγορηµάτων. Ονόµατα από ονοµατολογίες XML εµφανίζονται σαν επιθετικοί προσδιορισµοί της µορφής: nsprefix : localpart όπου το nsprefix αντιστοιχίζεται σε µοναδικό URI. Ένα κατηγορικό συντακτικό χρησιµοποιείται για την δήλωση των αντιστοιχιών ανάµεσα σε ονόµατα και URI. 72
73 Δήλωση Ονοµατολογιών A namespace can be declared by one of the following xmlns : name = URI xmlns = URI where xmlns is a reserved attribute. In the first case name gives the namespace prefix used to associate element and attribute names with the namespace name. In the second case the namespace name is the default namespace. Namespaces are declared within elements and their associations hold within the scope of those elements.
74 Δήλωση Ονοµατολογιών <x xmlns:edi = http://ecommerce.org/schema > <!-- the edi prefix is bound to http://ecommerce.org/schema for the x element and contents --> </x>
75 Χρήση επιγραφοµένων ονοµάτων Qualified names contain a prefix (which must be associated with a namespace URI reference in a namespace declaration) and a local part. Qualified names can serve as an element type: <x xmlns:edi = http://ecommerce.org/schema > <edi:price units = Euro> 32 </edi:price> <!-- the price element s namespace is http://ecommerce.org/schema --> </x>
76 Χρήση επιγραφοµένων ονοµάτων Qualified names can serve as attribute names: <x xmlns:edi = http://ecommerce.org/schema > <item edi:taxclass = exempt >... </item> <!-- the taxclass attribute s namespace is http://ecommerce.org/schema --> </x>
77 Πεδίο ισχύος ονοµατολογιών A namespace declaration applies to the element where it is specified and to all elements within its content, unless it is overriden by another namespace declaration. <html:html xmlns:html = http://www.w3.org/tr/rec-html40 > <!-- all elements here are in the HTML namespace --> <html:head><html:title> <html:body> </html:body> </html:html> </html:head></html:title>
78 Πεδίο ισχύος ονοµατολογιών Multiple namespace prefixes can be declared as attributes of a single element. <bk:book xmlns:book = xmlns:isbn =... > <book:title> </book:title> <isbn:number> </isbn:number> </bk:book>
79 Προκαθορισµενες Ονοµατολογίες A default namespace is considered to apply to the element where it is declared (if it has no prefix) and to all the unprefixed element s subelements. The default namespace can be set to the empty string. <book xmlns = urn:loc.gov:books xmlns:isbn =... > <!-- unprefixed elements are from namespace urn:loc.gov:books --> <title> </title> <isbn:number> </isbn:number> </book>
XML Schema
Προβλήµατα µε τα DTD <census-record taker= 9170 > <date><month>?</month><day>110</day><year>03</year></date> <address> <city>munkinland</city> <street></street> <postalcode></postalcode> </address> <person employed= fulltime pid=? > <name> <last>burgle</last> <first>brad</first> </name> <age>2131234</age> <gender>yes</gender> </person> <census-record> 81
82 Προβλήµατα µε τα DTD Τα παραπάνω προβλήµατα δεν εντοπίζονται από το DTD. Θα µπορούσαν να εντοπιστούν αν γράφαµε τον σχετικό κώδικα που να ελέγχει τους τύπους των δεδοµένων. Ωστόσο, µια τέτοια διαδικασία είναι επισφαλής και επιπλέον ξεφεύγει από την λογική της χρήσης των DTD. Τα DTD είναι ωστόσο αυστηρά όσον αφορά: Την σειρά µε την οποία εµφανίζονται τα στοιχεία ενός XML εγγράφου Την εισαγωγή σε ένα έγγραφο στοιχείων που ορίζονται σε χώρους ονοµάτων διαφορετικούς από το DTD.
83 XML Schema Το XML Schema προσφέρει νέες δυνατότητες έναντι των DTD: Πλουσιότερη συλλογή τύπων (booleans, numbers, date and times, URIs, integers, intervals, etc). Διευκολύνσεις για δηµιουργία νέων τύπων και αρχετύπων. Refinable archetypes: models can be defined as open - all required elements must be present but additional elements may also be present, or refinable - additional elements may be present if the schema defines what they are (inheritance). Namespace support. Attribute grouping.
84 XML Schema The purpose of a schema is to define a class of XML documents, and so the term "instance document" is often used to describe an XML document that conforms to a particular schema. In fact, neither instances nor schemas need to exist as documents per se -- they may exist as streams of bytes sent between applications, as fields in a database record, etc.
85 Example: Purchase Order (po.xml) <?xml version="1.0"?> <purchaseorder 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>
86 Example: Purchase Order (po.xml) <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> The purchase order schema is not mentioned. An instance is not actually required to reference a schema (many will). We assume that any processor of the instance document can obtain the purchase order schema without any information from the instance document.
87 Example XML Schema po.xsd <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"/> <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>
88 Example XML Schema po.xsd <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>
89 Example XML Schema po.xsd <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>
90 XML Schema po.xsd <!-- 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>
91 XML Schema The purchase order schema consists of a schema element and a variety of subelements (element, complextype, simpletype, etc.) which determine the appearance of elements and their content in instance documents. Each of the elements in the schema has a prefix xsd: which is associated with the XML Schema namespace through the declaration, xmlns:xsd="http://www.w3.org/2001/xmlschema", that appears in the schema element. The prefix xsd: is used by convention to denote the XML Schema namespace. The same prefix, and hence the same association, also appears on the names of built-in simple types, e.g. xsd:string. The purpose of the association is to identify the elements and simple types as belonging to the vocabulary of the XML Schema language rather than the vocabulary of the schema author.
92 Complex Type Definitions In XML Schema, there is a basic difference between: complex types which allow elements in their content and may carry attributes simple types which cannot have element content and cannot carry attributes There is also a major distinction between definitions which: create new types (both simple and complex) enable elements and attributes with specific names and types (both simple and complex) to appear in document instances New complex types are defined using the complextype element, containing a set of element declarations, element references, and attribute declarations. The declarations are not themselves types, but rather an association between a name and the constraints which govern the appearance of that name in documents governed by the associated schema. Elements are declared using the element element, and attributes are declared using the attribute element.
93 Complex Type Definitions: example <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> Any element appearing in an instance whose type is declared to be USAddress must consist of five elements and one attribute. These elements must be called name, street, city, state and zip, and the elements must appear in the same sequence (order) in which they are declared. The first four of these elements will each contain a string, and the fifth will contain a number. The element whose type is declared to be USAddress may appear with an attribute called country which must contain the string US.
94 Complex Type Definitions: example <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> In PurchaseOrderType, shipto and billto, associate different element names with the same complex type: USAddress. The PurchaseOrderType definition contains an orderdate attribute declaration which identifies a simple type. All attribute declarations must reference simple types because attributes cannot contain other elements or other attributes. Sometimes, element declarations use an existing element rather than declare a new element. These declarations reference an existing element. In general, the value of the ref attribute must reference a global element, i.e. one that has been declared under schema rather than as part of a complex type definition.
95 Occurrence constraints An element is required to appear when the value of minoccurs is 1 or more. The maximum number of times an element may appear is determined by the value of a maxoccurs attribute in its declaration. This value may be a positive integer or the term unbounded to indicate there is no maximum number of occurrences. The default value for both the minoccurs and the maxoccurs attributes is 1. Attributes may appear once or not at all. Attributes can be declared with a use attribute to indicate whether the attribute is required, optional, or even prohibited. Default values of both attributes and elements are declared using the default attribute. Default attribute values apply when attributes are missing, and default element values apply when elements are empty. The fixed attribute is used in both attribute and element declarations to ensure that the attributes and elements are set to particular values.
Global elements and attributes Global elements and global attributes are created by declarations that appear as the children of the schema element. Once declared, they can be referenced in one or more declarations using the ref attribute. A declaration that references a global element enables the referenced element to appear in the instance document in the context of the referencing declaration. The declaration of a global element also enables the element to appear at the top-level of an instance document. Hence purchaseorder, which is declared as a global element in po.xsd, can appear as the top-level element in po.xml. There are a number of caveats concerning the use of global elements and attributes: One caveat is that global declarations cannot contain references; global declarations must identify simple and complex types directly. Put concretely, global declarations cannot contain the ref attribute, they must use the type attribute. A second caveat is that cardinality constraints cannot be placed on global declarations, although they can be placed on local declarations that reference global declarations. In other words, global declarations cannot contain the attributes minoccurs, maxoccurs, or use. 96
Simple types and derived types Built-in: string, byte, integer, decimal, time, date, ID, IDREF, IDREFS, NTOKEN, etc. New simple types are defined by restricting existing simple types (builtin's and derived), by : i.e., the legal range of values for the new type are a subset of the existing type's range of values. We use: the simpletype element to define and name the new simple type the restriction element to indicate the existing (base) type, and to identify the "facets" that constrain the range of values. E.g., to create myinteger, we restrict the range of the integer base type by employing two facets called mininclusive and maxinclusive: <xsd:simpletype name="myinteger"> <xsd:restriction base="xsd:integer"> <xsd:mininclusive value="10000"/> <xsd:maxinclusive value="99999"/> </xsd:restriction> </xsd:simpletype> 97
98 Derived types E.g.a simple type called SKU is derived (by restriction) from the simple type string, restricting its values using a facet called pattern in conjunction with a regular expression "\d{3}-[a-z]{2}" that is read "three digits followed by a hyphen followed by two upper-case ASCII letters": <xsd:simpletype name="sku"> <xsd:restriction base="xsd:string"> <xsd:pattern value="\d{3}-[a-z]{2}"/> </xsd:restriction> </xsd:simpletype> XML Schema defines fifteen facets, such as the enumeration facet (used to constrain the values of almost every simple type, except the boolean). The enumeration facet limits a simple type to a set of distinct values. E.g.: <xsd:simpletype name="usstate"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="ak"/> <xsd:enumeration value="al"/> <xsd:enumeration value="ar"/> <!-- and so on... --> </xsd:restriction> </xsd:simpletype>
List types List types are comprised of sequences of atomic types; the parts of a sequence (the "atoms") themselves are meaningful. You can create new list types by derivation from existing atomic types. E.g: <xsd:simpletype name="listofmyinttype"> <xsd:list itemtype="myinteger"/> </xsd:simpletype> and an element in an instance document whose content conforms to listofmyinttype: <listofmyint>20003 15037 95977 95945</listOfMyInt> Several facets can be applied to list types: length, minlength, maxlength, and enumeration. For example, to define a list of exactly six US states (SixUSStates), we first define a new list type called USStateList from USState, and then we derive SixUSStates by restricting USStateList to only six items: <xsd:simpletype name="usstatelist"> <xsd:list itemtype="usstate"/> </xsd:simpletype> <xsd:simpletype name="sixusstates"> <xsd:restriction base="usstatelist"> <xsd:length value="6"/> </xsd:restriction> </xsd:simpletype> 99
100 Union types A union type enables an element or attribute value to be one or more instances of one type drawn from the union of multiple atomic and list types. E.g.: we create a union type for representing American states as singleton letter abbreviations or lists of numeric codes. E.g.: the zipunion union type is built from one atomic type and one list type: <xsd:simpletype name="zipunion"> <xsd:union membertypes="usstate listofmyinttype"/> </xsd:simpletype> When we define a union type, the membertypes attribute value is a list of all the types in the union. Now, assuming we have declared an element called zips of type zipunion, valid instances of the element are: <zips>ca</zips> <zips>95630 95977 95945</zips> <zips>ak</zips> Two facets, pattern and enumeration, can be applied to a union type.
Anonymous Type Definitions The definitions of sets of named types can be unwieldy, especially if you define many types that are referenced only once and contain very few constraints. In these cases, a type can be more succinctly defined as an anonymous type which saves the overhead of having to be named and explicitly referenced. Anonymous types can be identified by the lack of a type= in an element (or attribute) declaration, and by the presence of an un-named (simple or complex) type definition. E.g.: <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> 101
Complex types from simple types Simple types cannot have attributes. Therefore, to add an attribute to an element containing a simple value, we must define a complex type to carry the attribute declaration. To this end, we derive a new complex type from the simple type. E.g.: <xsd:element name="internationalprice"> <xsd:complextype> <xsd:simplecontent> <xsd:extension base="xsd:decimal"> <xsd:attribute name="currency" type="xsd:string"/> </xsd:extension> </xsd:simplecontent> </xsd:complextype> </xsd:element> We use the complextype element to start the definition of a new (anonymous) type. To indicate that the content model of the new type contains only character data and no elements, we use a simplecontent element. Finally, we derive the new type by extending the simple decimal type, adding a currency attribute using a standard attribute declaration. 102
Mixed content Character data can appear alongside sub-elements, and character data not confined to the deepest subelements. E.g.: <letterbody> <salutation>dear Mr.<name>Robert Smith</name>.</salutation>Your order of <quantity>1</quantity> <productname>baby Monitor</productName> shipped from our warehouse on <shipdate>1999-05-21</shipdate>....</letterbody> The following snippet of a schema declares letterbody: <xsd:element name="letterbody"> <xsd:complextype mixed="true"> <xsd:sequence> <xsd:element name="salutation"> <xsd:complextype mixed="true"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> </xsd:sequence> </xsd:complextype> </xsd:element> <xsd:element name="quantity" type="xsd:positiveinteger"/> <xsd:element name="productname" type="xsd:string"/> <xsd:element name="shipdate" type="xsd:date" minoccurs="0"/> <!-- etc. --> </xsd:sequence> </xsd:complextype> </xsd:element> To enable character data to appear between the child-elements of letterbody, the mixed attribute on the type definition is set to true. 103
Empty content To define a type whose content is empty, we define a type that allows only elements in its content, but we do not actually declare any elements and so the type's content model is empty: <xsd:element name="internationalprice"> <xsd:complextype> <xsd:complexcontent> <xsd:restriction base="xsd:anytype"> <xsd:attribute name="currency" type="xsd:string"/> <xsd:attribute name="value" type="xsd:decimal"/> </xsd:restriction> </xsd:complexcontent> </xsd:complextype> </xsd:element> In this example, we define an (anonymous) type having complexcontent, i.e. only elements. The complexcontent element signals that we intend to restrict or extend the content model of a complex type, and the restriction of anytype declares two attributes but does not introduce any element content. Alternatively: <xsd:element name="internationalprice"> <xsd:complextype> <xsd:attribute name="currency" type="xsd:string"/> <xsd:attribute name="value" type="xsd:decimal"/> </xsd:complextype> </xsd:element> 104