Page 13 .... Web: How to Program, Ed. Pearson International. Edition ! http://www.
w3.org/ ! www.deitel.com/books/iw3htp4 (per il codice di esempio degli esercizi)
XML Dott. Nicole NOVIELLI
[email protected] http://www.di.uniba.it/intint/people/nicole.html
XML: eXtensible Markup Language ! Permits document authors to create markup language, that is text-based notations for describing data
! Enables document authors to create entrely new
markup languages for describing any type of data Eg.:
! ! ! ! ! ! !
Mathematical formulas Software-configuration instructions Chemical structures Music News Reports …
Es.: xml describing a baseball player’s information John Doe 0.375 • XML documents contain text that represent content (in red) and elements that speciry the document’s structure (tag) • XML documents delimit elements with start tags () and end tags () • Every XML document must have a root element hat contains all the otehr element (‘player’ in the example)
Vocabularies ! XML-based markup langugage ! Provide a means for describing particular types of data in a standard and structured way
! Some XML vocabularies include: ! ! ! !
XHTML MathML VoiceXML CML (chemical markup language)
VoiceXML Voice Extensible MarkUp Language Un Tutorial qui: http://www.voicexml.org/tutorials/intro1.html
VoiceXML (VXML) is the W3C's standard XML format for specifying interactive voice dialogues between a human and a computer. It allows voice applications to be developed and deployed in an analogous way to HTML for visual applications. Just as HTML documents are interpreted by a visual web browser, VoiceXML documents are interpreted by a voice browser. VoiceXML has tags that instruct the voice browser to provide speech synthesis, automatic speech recognition, dialog management, and audio playback. An example of a VoiceXML document: Hello world!
5
VoiceXML is designed for creating audio dialogs that feature synthesized speech, digitized audio, recognition of spoken and DTMF key input, recording of spoken input, telephony, and mixed initiative conversations. Un esempio Would you like coffee, tea, milk, or nothing?
6
Validating XML documents: DTD and schema
! An XML document can refer to a DTD (Document type Definition) or to a schema
! Validating parsers can read the DTD/Schema and check that the XML document conforms to it ! That is the document has an appropriate structure ! E.g.: for the player’s information example: we are referencing a
DTD that specified that a player element must have firstName, lastName and battingAverage elements ! Omitting one of them would caus invalidation of player.xml, though the document would still be well-formed because it follows properly the XML syntax
! A nonvalidating parser just checks the syntax of an XML document
XML is highly portable
! Viewing or modifying an XML file (extension is ‘.xml’) does not
require any specialized software ! Any text editor that supports ASCII/Unicode characters can open an XML document for viewing and editing
! Most web browsers can disply XML documents in a formatted manner that shows the XML structure
XML parser and syntax
! Software for processing the XML files: ! makes the document available to other applications ! Checks that the document follows the syntax rules specified by W3C’s XML Recommendation (www.w3.org/XML)
! XML syntax requires a single root element and a start and end tag for each eleements ! Elements must be properly nested
! If an XML parser can process the document entirely then the XML document is well-formed
Structuring data ! XML Schema is a document definition language ! It specifies the structure of instance documents ! “elements contained by other elements" ! It specifies the datatype of each element/attribute ! "this element shall hold an integer with the range 0 to 12,000"
! The XML Schema language is also referred to as XML Schema Definition (XSD)
! Composed of two parts: ! Structure: http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/ ! Datatypes: http://www.w3.org/TR/2001/RECxmlschema-2-20010502/
! XML Schema is an XML based alternative to DTD
DTD Il Document Type Definition (definizione del tipo di documento): uno strumento utilizzato dai programmatori il cui scopo è quello di definire le componenti ammesse nella costruzione di un documento XML. Il termine non è utilizzato soltanto per i documenti XML ma anche per tutti i documenti derivati dall'SGML (di cui peraltro XML vuole essere una semplificazione che ne mantiene la potenza riducendone la complessità) tra cui famosissimo è l'HTML. In SGML, un DTD è necessario per la validazione del documento. Anche in XML, un documento è valido se presenta un DTD ed è possibile validarlo usando il DTD. Tuttavia XML permette anche documenti ben formati, ovvero documenti che, pur essendo privi di DTD, presentano una struttura sufficientemente regolare e comprensibile da poter essere controllata.
Schema vs. DTD ! Both are XML document definition languages ! XML Schema are written using XML ! Unlike DTDs, XML Schema are Extensible – like XML
! More verbose than DTDs
Schema vs. DTD: example
Schema vs. DTD: example My Life and Times Paul McCartney 1998 1-56592-235-2 McMillin Publishing …
Un esempio: markup for a business letter
Riferimento ad un dtd esterno È possibile, in alternativa, dichiarare il dtd nel file XML (inline)
letter.dtd
My Life and Times Paul McCartney 1998 1-56592-235-2 McMillin Publishing …
Fonte: slide Prof. Filippo Lanubile
XLM Namespaces La possibilità di creare elementi personalizzati con XML, potrebbe portare a conflitti nella gestione dei nomi Naming collision: lo stesso nome è usato per indicare elementi diversi An XML namespace is a collection of element and attribute names XML namespaces provide a means for document author to unambiguosly refer to the elements with the same name (i.e. prevent collision)
esempio Problem: and
Geometry Cardiology
both use ‘subject’ to markup data. In the first case, the subject is something one studies in school, whereas in the second case, teh subject is a field of medicine Solution: differentiation using namespaces Geometry and Cardiology
Differentiating elements with namespaces A book list A funny picture - The xmlns reserved attribute is used to create two namespace prefixes: texts and image - Each namespace prefi is boud to a URI (Uniform Resource Identifier) - Document authors create their own namespace prefixes and URI - To ensure that namespaces are unique, we must provide unique URIs - In this example we use URN: Uniform Resource Name
Differentiating elements with namespaces
simpleType are restrictions of a type typically called a base type. In this case, the base type is the decimal that is restricted to be at least 2.1 by using the minInclusive element
Creating a complexType with simpleContent
A complexType with simple content can have attributes but not child elements. Also, they must extend or restrict some XML Schema type or user-defined type. The extension element with attribute base sets the base type as string. In this example the string type is extended with the attribute model
Creating a complexType with complex content A complexType with complex content is allowed to have both attributes and child elements. The element all encloses elements that mus each be included once in the corresponding XML instance document, in any order. When using types CPU and gigahertz we must include the prefix computer because thee user-defined types are part of the computer namespace xmlns:computer = "http://www.deitel.com/computer” targetNamespace = "http://www.deitel.com/computer">
This line declares the actual element that uses the three types defined in the schema. The element is called laptop and is of type portable We have now created an element named laptop that contains child elements processor, monitor, CPUSpeed and RAM and the attribute manufacturer
laptop.xml: an XML file using the laptop.xsd schema defined Intel 17 2.4 256
Riferimenti ! Harvey M. Deitel and Paul J. Deitel, Internet & World Wide Web: How to Program, Ed. Pearson International Edition
! http://www.w3.org/ ! www.deitel.com/books/iw3htp4 (per il codice di esempio degli esercizi)