1 / 40

XML Schemas

XML Schemas. Useful Links. Schema tutorial links: http://www.w3schools.com/schema/default.asp http://www.xfront.com/ http://www.w3.org/TR/xmlschema-0/. Why Schemas. Enhanced datatypes 44+ versus 10 Can create your own datatypes

inoke
Download Presentation

XML Schemas

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 Schemas

  2. Useful Links • Schema tutorial links: • http://www.w3schools.com/schema/default.asp • http://www.xfront.com/ • http://www.w3.org/TR/xmlschema-0/

  3. Why Schemas • Enhanced datatypes • 44+ versus 10 • Can create your own datatypes • Example: "This is a new type based on the string type and elements of this type must follow this pattern: ddd-dddd, where 'd' represents a digit". • Written in the same syntax as instance documents: less syntax to remember • Can extend or restrict a type (derive new type definitions on the basis of old ones) • Can express sets, i.e., can define the child elements to occur in any order • Can specify element content as being unique (keys on content) and uniqueness within a region • Can define multiple elements with the same name but different content • Can define substitutable elements - e.g., the "Book" element is substitutable for the "Publication" element.

  4. Schemas and DTDs • One difference between XML Schemas and DTDs is that the XML Schema vocabulary is associated with a name (namespace). Likewise, the new vocabulary that you define must be associated with a name (namespace). • With DTDs neither set of vocabulary is associated with a name (namespace).

  5. Some XML Parsers with Schema Support • A comparison of some XML parsers: • http://webreference.com/xml/column22/2.html

  6. Example – BookCatalog.dtd • <!ELEMENT BookCatalog (Book)+> • <!ELEMENT Book (Title, Author, Date, ISBN, Publisher)> • <!ELEMENT Title (#PCDATA)> • <!ELEMENT Author (#PCDATA)> • <!ELEMENT Date (#PCDATA)> • <!ELEMENT ISBN (#PCDATA)> • <!ELEMENT Publisher (#PCDATA)>

  7. An Example Of A Schema DefinitionBookCatalog.xsd <?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="BookCatalog"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Book" minOccurs="1“ maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Book"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Title" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Author" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Date" minOccurs="1" maxOccurs="1"/> <xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Publisher" minOccurs="1" maxOccurs="1"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:schema>

  8. BookCatalog.xml <?xml version="1.0"?> <BookCatalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="BookCatalog.xsd"> <Book> <Title>Billions Of Stars</Title> <Author>Susan Boggs</Author> <Date>1983</Date> <ISBN>1-5555-555-2</ISBN> <Publisher>Anderson-Wells</Publisher> </Book> <Book> <Title>Adventures Of Freddie the Frog</Title> <Author>John Smith</Author> <Date>1977</Date> <ISBN>0-444-4444-4</ISBN> <Publisher>Kidder Publishing Co.</Publisher> </Book> </BookCatalog>

  9. Schema namespace • Schema element: All XML schemas have schema as the root element. • XML Schema namespace: The elements and datatypes that are used to construct schemas, like, schema, element, complexType, sequence and string come from the http://…/XMLSchema namespace

  10. Using namespacesExample – plants.xsd <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.MyDefs.com" xmlns="http://www.MyDefs.com" elementFormDefault="qualified"> <xsd:element name="plants"> <xsd:complexType> <xsd:sequence> <xsd:element ref="plant" minOccurs="1" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="plant"> <xsd:complexType> <xsd:sequence> <xsd:element name="plantName" minOccurs="1" maxOccurs="1"/> <xsd:element name="category" minOccurs="1" maxOccurs="1"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>

  11. Example – plants.xml <?xml version="1.0"?> <plants xmlns="http://www.MyDefs.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=“plants.xsd"> <plant> <plantName>Daisy</plantName> <category>perennial</category> </plant> </plants>

  12. Example – BookCatalog.xsd <?xml version="1.0"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.MyDefs.org" xmlns:bk="http://www.MyDefs.org" elementFormDefault="qualified"> <element name="BookCatalog"> <complexType> <sequence> <element ref="bk:Book" minOccurs="1" maxOccurs="unbounded"/> </sequence> </complexType> </element> <element name="Book"> <complexType> <sequence> <element ref="bk:Title"/> … <element ref="bk:Publisher"/> </sequence> </complexType> </element> <element name="Title" type="string"/> … <element name="Publisher" type="string"/> </schema>

  13. BookCatalog.xml <?xml version="1.0"?> <BookCatalog xmlns="http://www.MyDefs.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.MyDefs.org BookCatalog.xsd"> <Book> <Title>Billions Of Stars</Title> <Author>Susan Boggs</Author> <Date>1983</Date> <ISBN>1-5555-555-2</ISBN> <Publisher>Anderson-Wells</Publisher> </Book> <Book> <Title>Adventures Of Freddie the Frog</Title> <Author>John Smith</Author> <Date>1977</Date> <ISBN>0-444-4444-4</ISBN> <Publisher>Kidder Publishing Co.</Publisher> </Book> </BookCatalog>

  14. Simple vs Complex Types • A schema document can be divided into content types: Simple and complex types. • Simple types: can contain only text. There are several simple built-in types – String, Boolean, Float, Double, ID, IDREF, Entity etc. • Derived data types: The built-in data types can be customized to control the content. • Complex types: can contain elements that can contain other elements or attributes. • Local vs global declarations: Schema components, elements, attributes, named simple and complex types, declared under xsd:schema element are considered globally declared. A global declaration has to be explicitly referenced. • New elements can be declared within the context of a complex type, in which case they are considered locally declared.

  15. Simple Types: exampleA.xsd <?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="SimpleTypes"/> <xsd:element name="title" type="xsd:string"/> <xsd:element name="price" type="xsd:decimal"/> <xsd:element name="last-modified" type="xsd:string"/> <xsd:element name="translated" type="xsd:boolean"/> </xsd:schema>

  16. exampleA.xml <?xml version="1.0"?> <SimpleTypes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="exampleA.xsd"> <title>Billions Of Stars</title> <price>2000-01-02</price> <lastModified>1983</lastModified> <translated>true</translated> </SimpleTypes>

  17. Deriving From Simple Types • A new custom type can be derived a simple type. • Example: <xsd:simpleType name="ISBNType"> <xsd:restriction base="xsd:string"> -- refers to a simple type <xsd:pattern value="\d{1}-\d{5}-\d{3}-\d{1}"/> </xsd:restriction> </xsd:simpleType> • Patterns are specified using regular expressions (supports regular expressions from perl)

  18. <?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="CustomTypes"/> <xsd:simpleType name="ISBNType"> <xsd:restriction base="xsd:string"> <xsd:pattern value="\d{1}-\d{5}-\d{3}-\d{1}"/> </xsd:restriction> </xsd:simpleType> <xsd:element name="title" type="xsd:string"/> <xsd:element name="ISBN" type="ISBNType"/> </xsd:schema> <?xml version="1.0"?> <CustomTypes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="exampleB.xsd"> <title>Billions Of Stars</title> <ISBN> 2-12345-678-9 </ISBN> </CustomTypes> Example – Example3.xsd and Example3.xml

  19. Giving a choice of acceptable values <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="autobiography"/> <xsd:enumeration value="non-fiction"/> <xsd:enumeration value="fiction"/> </xsd:restriction> </xsd:simpleType>

  20. Defining Complex Types • Elements that can contain other elements or attributes. • There are four kinds of elements of complex type: • Elements that contain other elements and attributes, but no text. • Elements that are empty – possibly contain attributes but no other elements or text. • Elements of mixed content that can contain a combination of elements, attributes or text. • Elements that contain only text.

  21. Example - Elements containing elements only <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="BookCatalog"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> …. </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>

  22. Example – Elements containing elements only <?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="plants"> <xsd:complexType> <xsd:sequence> <xsd:element ref="plant" minOccurs="1" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="plant"> <xsd:complexType> <xsd:sequence> <xsd:element name="plantName" minOccurs="1" maxOccurs="1"/> <xsd:element name="category" minOccurs="1" maxOccurs="1"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>

  23. Referring to a group If we want to reuse a set of elements in several content –type definitions, we can use a model group to define a set of elements that can be repeated. They act rather like parameter entities in DTDs. A model group consists of element declarations, wildcards and other model groups. The minimum values for min and maxOccurences = 1. <xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:group ref="BookElements"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:group name="BookElements"> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:group>

  24. <xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:all> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:all> </xsd:complexType> </xsd:element> <?xml version="1.0"?> <Book> <Title>Billions Of Stars</Title> <Author>Susan Boggs</Author> <Date>1983</Date> <ISBN>1-5555-555-2</ISBN> <Publisher>Anderson-Wells</Publisher> </Book> <Book> <Title>Adventures Of Freddie the Frog</Title> <Author>John Smith</Author> <Date>1977</Date> <ISBN>0-444-4444-4</ISBN> <Publisher>Kidder Publishing Co.</Publisher> </Book> </BookStore> Using all

  25. Attributes • Attribute declarations: • An attribute is declared with <attribute>. • An attribute can be defined outside of a complex type and referenced in complex type. • Attribute types can be defined using primitive types. • Default, fixed, optional, required and prohibited can be used to set the values for attributes. • Example: • <attribute name=widgetPrice” type=“xsd:float” value=“10.00” use=“required” />

  26. <!ELEMENT BookCatalog(Book)+> <!ELEMENT Book (Title, Author+, Date, ISBN, Publisher)> <!ATTLIST Book Category (autobiography | non-fiction | fiction) #REQUIRED InStock (true | false) "false" Reviewer CDATA " "> <!ELEMENT Title (#PCDATA)> <!ELEMENT Author (#PCDATA)> <!ELEMENT Date (#PCDATA)> <!ELEMENT ISBN (#PCDATA)> <!ELEMENT Publisher (#PCDATA)> <!ELEMENT Month (#PCDATA)> <!ELEMENT Year (#PCDATA)>

  27. <xsd:element name="BookCatalog"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> <xsd:attributeGroup ref="BookAttributes"/> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:attributeGroup name="BookAttributes"> <xsd:attribute name="Category" use="required"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="autobiography"/> <xsd:enumeration value="non-fiction"/> <xsd:enumeration value="fiction"/> </xsd:restriction> </xsd:simpleType> </xsd:attribute> <xsd:attribute name="InStock" type="xsd:boolean" default="false"/> <xsd:attribute name="Reviewer" type="xsd:string" default="none"/> </xsd:attributeGroup> </xsd:schema>

  28. Uniqueness & Keys • DTDs provide the ID attribute datatype for uniqueness (i.e., an ID value must be unique throughout the entire document, and the XML parser enforces this). • XML Schema has much enhanced uniqueness capabilities: • enables you to define element content to be unique. • enables you to define non-ID attributes to be unique. • enables you to define a combination of element content and attributes to be unique. • enables you to distinguish between unique versus key. • enables you to declare the range of the document over which something is unique

  29. unique vs key • Key: an element or attribute (or combination thereof) which is defined to be a key must: • always be present (minOccurs must be greater than zero) • be not-false. • be unique • Key implies unique, but unique does not imply key

  30. Using ISBN as a Key • In the next example, we will use a Book's ISBN element as a key.

  31. Example <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.MyDefs.com" xmlns="http://www.myDefs.com" xmlns:bk="http://www.MyDefs.com" elementFormDefault="qualified"> <xsd:element name="BookCatalog"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> <!-- Within <BookCatalog> define a key, called PK. Select each <Book>, and within each <Book> the ISBN element is a key.“  <xsd:key name="PK"> <xsd:selector xpath="bk:Book"/> <xsd:field xpath="bk:ISBN"/> </xsd:key> </xsd:element> </xsd:schema>

  32. BookCatalog.xml <?xml version="1.0"?> <BookCatalog xmlns="http://www.MyDefs.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://www.MyDefs.com BookCatalog.xsd"> <Book> <Title>Billions Of Stars</Title> <Author>Susan Boggs</Author> <Date>1983</Date> <ISBN>1-5555-555-2</ISBN> <Publisher>Anderson-Wells</Publisher> </Book> <Book> <Title>Adventures Of Freddie the Frog</Title> <Author>John Smith</Author> <Date>1977</Date> <ISBN>0-444-4444-4</ISBN> <Publisher>Kidder Publishing Co.</Publisher> </Book> </BookCatalog>

  33. Notes about <key> • It must be nested within an <element> • It must come at the end of <element> (after the content model, and attribute declarations) • Use the <selector> element as a child of <key> to select a set of elements for which the key applies. • Use the <field> element as a child of <key> to identify the element or attribute that is to be the key • There can be multiple <field> elements.

  34. Example – meeting.xsd <xsd:element name="meeting"> <xsd:complexType> <xsd:sequence> <xsd:element name="participants"> <xsd:complexType> <xsd:sequence> <xsd:element name="Name" minOccurs="0" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="First" type="xsd:string"/> <xsd:element name="Last" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> <!-- The key is the combination of the First and Last name.  <xsd:key name="PK"> <xsd:selector xpath="meet:participants/meet:Name"/> <xsd:field xpath="meet:First"/> <xsd:field xpath="meet:Last"/> </xsd:key> </xsd:element>

  35. Example-meeting.xml <?xml version="1.0"?> <meeting xmlns="http://www.MyDefs.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.MyDefs.com Meeting.xsd"> <participants> <Name><First>John</First><Last>Smith</Last></Name> <Name><First>Mary</First><Last>Jones</Last></Name> </participants> </meeting>

  36. unique • The <unique> element is used exactly like the <key> element is used. It has a <selector> and one or more <field> elements, just like <key> has. • The only difference is that the schema validator will simply validate that, whenever present, the values are unique.

  37. Example <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.MyDefs.com" xmlns="http://www.myDefs.com" xmlns:bk="http://www.MyDefs.com" elementFormDefault="qualified"> <xsd:element name="BookCatalog"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> …. </xsd:complexType> <!– ISBN are unique  <xsd:unique name="UNIQ"> <xsd:selector xpath="bk:Book"/> <xsd:field xpath="bk:ISBN"/> </xsd:unique> </xsd:element> </xsd:schema>

  38. Referencing a key with a key ref • Similar to ID/IDREFS, you can define a keyref to link an element with a key. • If there are 2 fields in the key, then there must be 2 fields in the keyref, if there are 3 fields in the key, then there must be 3 fields in the keyref, etc. • Further, the fields in the keyref must match in type and position to the key.

  39. Example – keyRef.xsd <xsd:element name="rootElement"> <xsd:complexType> <xsd:sequence> <xsd:element name="elementOne" maxOccurs="unbounded"> <xsd:complexType> <xsd:attribute name="elementOneKey" type="xsd:integer" /> <xsd:attribute name="elementOneDesc" type="xsd:string" /> </xsd:complexType> <xsd:key name="elementOnePK"> <xsd:selector xpath=".//elementOne"/> <xsd:field xpath="@elementOneKey"/> </xsd:key> </xsd:element> <xsd:element name="elementTwo" maxOccurs="unbounded"> <xsd:complexType> <xsd:attribute name="elementTwoKey" type="xsd:integer" /> <xsd:attribute name="elementOneKey" type="xsd:integer" /> <xsd:attribute name="elementTwoDesc" type="xsd:string" /> </xsd:complexType> <xsd:keyref name="elementOneFK" refer="elementOnePK"> <xsd:selector xpath=".//elementTwo"/> <xsd:field xpath="@elementOneKey"/> </xsd:keyref> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element>

  40. Example – keyRef.xml <?xml version="1.0"?> <rootElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="Keyref.xsd"> <elementOne elementOneKey="11" elementOneDesc="Some"/> <elementOne elementOneKey="12" elementOneDesc="Other"/> <elementTwo elementTwoKey="99" elementOneKey="11" elementTwoDesc="Refers To Some"/> <elementTwo elementTwoKey="100" elementOneKey="12" elementTwoDesc="Refers To Other"/> </rootElement>

More Related