1 / 99

XML Documents and Schema in greater depth

XML Documents and Schema in greater depth. In one sense XML is …. A language neutral way of representing structured data Analogy to serialized object is easiest to understand in this context Great intermediate data format for applications to talk cross-platform, cross-language, etc.

Download Presentation

XML Documents and Schema in greater depth

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. XML Documents and Schema in greater depth

  2. In one sense XML is … • A language neutral way of representing structured data • Analogy to serialized object is easiest to understand in this context • Great intermediate data format for applications to talk cross-platform, cross-language, etc.

  3. Equivalently, XML is • A flexible format for describing any kind of data (document). • like HTML • But you define can whatever tags you want for your application. • Actually more like SGML • HTML is really a Document Type in SGML (Standard Generalized Markup Language) • A self-describing format: • an XML document gives complete information about what fields values are associated with • an application doesn’t have to infer the field names from the order. • It just describes a document • Doesn't say what it means. • Doesn't tell how to display it.

  4. Some sample XML documents

  5. Article example <Article > <Headline>Direct Marketer Offended by Term 'Junk Mail' </Headline> <authors><author> Joe Garden</author> <author> Tim Harrod</author> </authors> <abstract>Dan Spengler, CEO of the direct-mail-marketing firm Mailbox of Savings, took umbrage Monday at the use of the term "junk mail." </abstract> <body type="url" > http://www.theonion.com/archive/3-11-01.html </body> </Article>

  6. Order / Whitespace • Note that element order is important, but whitespace is not. This is the same as far as the xml parser is concerned: • <Article > • <Headline>Direct Marketer Offended by Term 'Junk Mail' </Headline> • <authors> • <author> Joe Garden</author> • <author> Tim Harrod</author> • </authors> • <abstract>Dan Spengler, CEO of the direct-mail-marketing firm Mailbox of • Savings, took umbrage Monday at the use of the term "junk mail." • </abstract> • <body type="url" > http://www.theonion.com/archive/3-11-01.html </body> • </Article>

  7. Molecule Example <?xml version "1.0" ?> <CML> <MOL TITLE="Water" > <ATOMS> <ARRAY BUILTIN="ELSYM" > H O H</ARRAY> </ATOMS> <BONDS> <ARRAY BUILTIN="ATID1" >1 2</ARRAY> <ARRAY BUILTIN="ATID2" >2 3</ARRAY> <ARRAY BUILTIN="ORDER" >1 1</ARRAY> </BONDS> </MOL> </CML>

  8. Rooms example <?xml version="1.0" ?> <rooms> <room name="Red"> <capacity>10</capacity> <equipmentList> <equipment>Projector</equipment> </equipmentList> </room> <room name="Green"> <capacity>5</capacity> <equipmentList /> <features> <feature>No Roof</feature> </features> </room> </rooms>

  9. Suggestion • Try building each of those documents in XMLSpy. • Note: it is not required to create a schema to do this. Just create new XML document and start building.

  10. Dissecting an XML Document

  11. Things that can appear in an XML document • ELEMENTS: simple, complex, empty, or mixed content; attributes. • The XML declaration • Processing instructions(PIs) <? …?> • Most common is <?xml-stylesheet …?> • <?xml-stylesheet type=“text/css” href=“mys.css”?> • Comments<!-- comment text -->

  12. Parts of an XML document Declaration Tags <?xml version "1.0"?> <CML><MOL TITLE="Water" > <ATOMS> <ARRAY BUILTIN="ELSYM" > H O H</ARRAY> </ATOMS> <BONDS> <ARRAY BUILTIN="ATID1" >1 2</ARRAY> <ARRAY BUILTIN="ATID2" >2 3</ARRAY> <ARRAY BUILTIN="ORDER" >1 1</ARRAY> </BONDS> </MOL> </CML> Begin Tags End Tags Attributes Attribute Values An XML element is everything from (including) the element's start tag to (including) the element's end tag.

  13. XML and Trees Root element CML • Tags give the structure of a document. They divide the document up into Elements, starting at the top most element, the root element. The stuff inside an element is its content – content can include other elements along with ‘character data’ MOL ATOMS BONDS ARRAY ARRAY ARRAY ARRAY CDATA sections 12 23 11 HOH

  14. XML and Trees Root element CML <?xml version "1.0"?> <CML> <MOL TITLE="Water" > <ATOMS> <ARRAY BUILTIN="ELSYM" > H O H</ARRAY> </ATOMS> <BONDS> <ARRAY BUILTIN="ATID1" >1 2</ARRAY> <ARRAY BUILTIN="ATID2" >2 3</ARRAY> <ARRAY BUILTIN="ORDER" >1 1</ARRAY> </BONDS> </MOL> </CML> MOL ATOMS BONDS ARRAY ARRAY ARRAY ARRAY Data sections 12 23 11 HOH

  15. XML and Trees rooms room room capacity features capacity equipmentlist equipmentlist equipment 10 5 feature projector No Roof

  16. More detail on elements

  17. Element relationships Book is the root element. Title, prod, and chapter are child elements of book. Book is the parent element of title, prod, and chapter. Title, prod, and chapter are siblings (or sister elements) because they have the same parent. <book> <title>My First XML</title> <prod id="33-657" media="paper"></prod> <chapter>Introduction to XML <para>What is HTML</para> <para>What is XML</para> </chapter> <chapter>XML Syntax <para>Elements must have a closing tag</para> <para>Elements must be properly nested</para> </chapter> </book>

  18. Element content Elements can have different content types. An XML element is everything from (including) the element's start tag to (including) the element's end tag. An element can have element content, mixed content, simple content, or empty content. An element can also have attributes. In the previous example, book has element content, because it contains other elements. Chapter has mixed content because it contains both text and other elements. Para has simple content (or text content) because it contains only text. Prod has empty content, because it carries no information.

  19. Element naming • XML elements must follow these naming rules: • Names can contain letters, numbers, and other characters • Names must not start with a number or punctuation character • Names must not start with the letters xml (or XML or Xml ..) • Names cannot contain spaces • Take care when you "invent" element names and follow these simple rules: • Any name can be used, no words are reserved, but the idea is to make • names descriptive. Names with an underscore separator are nice. • Examples: <first_name>, <last_name>.

  20. Element naming, cont. Avoid "-" and "." in names. For example, if you name something "first-name,“ it could be a mess if your software tries to subtract name from first. Or if you name something "first.name," your software may think that "name" is a property of the object "first." Element names can be as long as you like, but don't exaggerate. Names should be short and simple, like this: <book_title> not like this: <the_title_of_the_book>.  XML documents often have a corresponding database, in which fields exist corresponding to elements in the XML document. A good practice is to use the naming rules of your database for the elements in the XML documents. Non-English letters like éòá are perfectly legal in XML element names, but watch out for problems if your software vendor doesn't support them. The ":" should not be used in element names because it is reserved to be used for something called namespaces (more later).

  21. Well formed XML

  22. Well-formed vs Valid • Recall that an XML document is said to be well-formed if it obeys basic semantic and syntactic constraints. • This is different from a valid XML document, which (as we will see in more depth) properly matches a schema.

  23. Rules for Well-Formed XML • An XML document is considered well-formed if it obeys the following rules: • There must be one element that contains all others (root element) • All tags must be balanced • <BOOK>...</BOOK> • <BOOK /> • Tags must be nested properly: • <BOOK> <LINE> This is OK </LINE> </BOOK> • <LINE> <BOOK> This is </LINE> definitely NOT </BOOK> OK • Text is case-sensitive so • <P>This is not ok, even though we do it all the time in HTML!</p>

  24. More Rules for Well-Formed XML • The attributes in a tag must be in quotes • < ITEM CATEGORY=“Home and Garden” Name=“hoe-matic t500”> • Comments are allowed • <!–- They are done just as in HTML… --> • Must begin with • <?xml version=‘1.0’ ?> • Special characters must be escaped: the most common are • < " ' > & • <formula> x &lt; y+2x </formula> • <cd title="&quot; mmusic"> • An XML document that obeys these rules is Well-Formed

  25. Creating XML • There are many XML editors. • Xeena • XMLSpy • Xeena on the CSPP machines • Like HTML, text editors are frequently the only thing available or the only thing that produces what you want • Test in IE6 or NetScape 7.0

  26. Next Step XML Schema

  27. XML Schema • XML allows any sort of tag you want. • In a given application, you want to fix a vocabulary -- what tags make sense. • Use a Schema to define an XML dialect • MusicXML, VoiceXML, ADXML, etc. • Restrict documents to those tags. • Anyone who has your Schema can validate their document to see if it obeys the rules of the dialect.

  28. Schema determine … • What sort of elements can appear in the document. • What elements MUST appear • Which elements can appear as part of another element • What attributes can appear or must appear • What kind of values can/must be in an attribute.

  29. Rooms XML Schema <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="rooms"><xs:complexType><xs:sequence> <xs:element name="room" minOccurs="0" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="capacity" type="xs:decimal"/> <xs:element name="equiptmentList"/> <xs:element name="features" minOccurs="0"><xs:complexType> <xs:sequence> <xs:element name="feature" type="xs:string“ maxOccurs="unbounded"/> </xs:sequence> </xs:complexType></xs:element> </xs:sequence> <xs:attribute name="name" type="xs:string" use="required"/> </xs:complexType> </xs:element> </xs:sequence></xs:complexType></xs:element> </xs:schema>

  30. Bookings XML Schema <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="bookings"> <xs:complexType> <xs:sequence> <xs:element ref="lastUpdated" maxOccurs="1" minOccurs="0"/> <xs:element ref="meetingDate" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="year" type="xs:integer"/> <xs:element name="month" type="xs:string"/> <xs:element name="day" type="xs:integer"/> Note that there are four global types in this document!

  31. Bookings, cont. <xs:element name="meetingDate"> <xs:complexType> <xs:sequence> <xs:element ref="year"/> <xs:element ref="month"/> <xs:element ref="day"/> <xs:element ref="meeting" maxOccurs="unbounded" minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="lastUpdated"> <xs:complexType> <xs:attribute name="date" type="xs:string"/> <xs:attribute name="time" type="xs:string"/> </xs:complexType> </xs:element>

  32. Bookings, cont. <xs:element name="meeting"> <xs:complexType> <xs:sequence> <xs:element name="meetingName" maxOccurs="1" minOccurs="1" type="xs:string"/> <xs:element name="roomName" maxOccurs="1" minOccurs="1" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>

  33. An Example Bookings Document <?xml version="1.0" encoding="UTF-8"?> <bookings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../schemas/Bookings.xsd"> <meetingDate> <year>2003</year> <month>April</month> <day>1</day> <meeting> <meetingName>Democratic Party</meetingName> <roomName>Green Room</roomName> </meeting> <meeting> <meetingName>Republican Party</meetingName> <roomName>Red Room</roomName> </meeting> </meetingDate> </bookings>

  34. XML Schema (Document Type Definition) • A Schema (or the older DTD) is a specification: it specifies the language that you speak. • Check the DTDs for musicxml, adxml, etc. that are available off the course webpage • These give you the basic structure of each of these applications. • Not many schemas available, but much better • As we said before, like a user-defined type in a programming language. Also somewhat analogous to a database schema • says what are the components that can appear • gives default values and restrictions.

  35. Dissecting Schema

  36. What’s in a Schema? • A Schema is an XML document (a DTD is not) • Because it is an XML document, it must have a root element • The root element is <schema> • Within the root element, there can be • Any number and combination of • Inclusions • Imports • Re-definitions • Annotations • Followed by any number and combinations of • Simple and complex data type definitions • Element and attribute definitions • Model group definitions • Annotations

  37. Structure of a Schema <schema> <!– any number of the following --> <include .../> <import> ... </import> <redefine> ... </redefine> <annotation> ... </annotation> <!– any number of following definitions --> <simpleType> ... </simpleType> <complexType> ... </complexType> <element> ... </element> <attribute/> <attributeGroup> ... </attributeGroup> <group> ... </group> <annotation> ... </annotation> </schema>

  38. Simple Types

  39. Elements • What is a Simple Element? • A simple element is an XML element that can contain only text. It cannot contain any other elements or attributes. • Can also add restrictions (facets) to a data type in order to limit its content, and you can require the data to match a defined pattern.

  40. Example Simple Element • The syntax for defining a simple element is: • <xs:element name="xxx" type="yyy"/>where xxx is the name of the element and yyy is the data type of the element. Here are some XML elements: • <lastname>Refsnes</lastname> • <age>34</age> • <dateborn>1968-03-27</dateborn> And here are the corresponding simple element definitions: • <xs:element name="lastname" type="xs:string"/> • <xs:element name="age" type="xs:integer"/> • <xs:element name="dateborn" type="xs:date"/>

  41. Common XML Schema Data Types • XML Schema has a lot of built-in data types. Here is a list of the most common types: • xs:string • xs:decimal • xs:integer • xs:boolean • xs:date • xs:time

  42. Declare Default and Fixed Values for Simple Elements • Simple elements can have a default value OR a fixed value set. • A default value is automatically assigned to the element when no other value is specified. In the following example the default value is "red": • <xs:element name="color" type="xs:string" default="red"/> A fixed value is also automatically assigned to the element. You cannot specify another value. In the following example the fixed value is "red": • <xs:element name="color" type="xs:string" fixed="red"/>

  43. Attributes(Another simple type) • All attributes are declared as simple types. • Only complex elements can have attributes!

  44. What is an Attribute? • Simple elements cannot have attributes. • If an element has attributes, it is considered to be of complex type. • But the attribute itself is always declared as a simple type. • This means that an element with attributes always has a complex type definition.

  45. How to Define an Attribute • The syntax for defining an attribute is: • <xs:attribute name="xxx" type="yyy"/>where xxx is the name of the attribute and yyy is the data type of the attribute. Here are an XML element with an attribute: • <lastname lang="EN">Smith</lastname> And here are a corresponding simple attribute definition: • <xs:attribute name="lang" type="xs:string"/>

  46. Declare Default and Fixed Values for Attributes • Attributes can have a default value OR a fixed value specified. • A default value is automatically assigned to the attribute when no other value is specified. In the following example the default value is "EN": • <xs:attribute name="lang" type="xs:string" default="EN"/> A fixed value is also automatically assigned to the attribute. You cannot specify another value. In the following example the fixed value is "EN": • <xs:attribute name="lang" type="xs:string" fixed="EN"/>

  47. Creating Optional and Required Attributes • All attributes are optional by default. To explicitly specify that the attribute is optional, use the "use" attribute: • <xs:attribute name="lang" type="xs:string" use="optional"/> To make an attribute required: • <xs:attribute name="lang" type="xs:string" use="required"/>

  48. Restrictions • As we will see later, simple types can have ranges put on their values • These are known as restrictions

  49. Complex Types

  50. Complex Elements • A complex element is an XML element that contains other elements and/or attributes. • There are four 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!

More Related