1 / 48

XSD: XML Schema Language

XSD: XML Schema Language. Kanda Runapongsa ( krunapon@kku.ac.th ) Dept. of Computer Engineering Khon Kaen University. XML Schema & DTD. Like a DTD, its purpose is to define the legal building blocks of an XML document defines elements that can appear in a document

Download Presentation

XSD: XML Schema Language

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. XSD: XML Schema Language Kanda Runapongsa (krunapon@kku.ac.th) Dept. of Computer Engineering Khon Kaen University

  2. XML Schema & DTD • Like a DTD, its purpose is to define the legal building blocks of an XML document • defines elements that can appear in a document • defines attributes that can appear in a document • defines which elements are child elements 168493: XML and Web Services (II/2546)

  3. XML Schema & DTD (Cont.) • defines the order of child elements • defines the number of child elements • defines whether an element is empty or can include text • defines data types for elements and attributes • defines default and fixed values for elements and attributes 168493: XML and Web Services (II/2546)

  4. Advantages of XSD over DTD • XML Schemas use XML syntax • Do not need to learn a new syntax • Use any XML parser to parse XML schemas • XML Schemas support many data types and allow you to create new data types • No ‘Integer’ or ‘Double’ types in DTD 168493: XML and Web Services (II/2546)

  5. Adv. of XSD over DTD (Cont.) • XML Schemas allow you to group elements to control the recurrence of elements and attributes • In DTD, we cannot specify that the number of occurrences of elements must be at least 2 and at most 5 • XML Schemas support namespaces 168493: XML and Web Services (II/2546)

  6. The <schema> Element • The <schema> element is the root element of every XML Schema: <?xml version="1.0"?> <xs:schema> ... ... </xs:schema> 168493: XML and Web Services (II/2546)

  7. The <schema> Element (Cont.) <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://abc.com" xmlns=“http://abc.com" elementFormDefault="qualified"> ... ... </xs:schema> 168493: XML and Web Services (II/2546)

  8. Attributes of <Schema> xmlns:xs=“http://www.w3.org/2001/XMLSchema” • Indicates that the elements and data types used in the schema (schema, element,…) come from the http://www.w3.org/2001/XMLSchema namespace • These elements and data types should be prefixed with xs: 168493: XML and Web Services (II/2546)

  9. Attributes of <Schema> (Cont.) targetNamespace=“http://abc.com” • Indicates that the elements defined by this schema (Note, To, From,..) is in the "http://www.w3schools.com" namespace 168493: XML and Web Services (II/2546)

  10. Attributes of <Schema> (Cont.) xmlns=“http://abc.com” • Indicates that the default namespace is http://abc.com elementFormDefault="qualified“ • indicates that any elements used by the XML instance document which were declared in this schema must be namespace qualified. 168493: XML and Web Services (II/2546)

  11. XSD Simple Elements • A simple element • Can contain only text • Cannot contain any other elements • Cannot contain any attributes • Can text be any other types besides string? • It can be date, etc., or even a custom type that you can define yourself 168493: XML and Web Services (II/2546)

  12. Defining a 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. 168493: XML and Web Services (II/2546)

  13. Simple Elements: Example • Some XML elements • <lastname>Wasee</lastname> • <age>34</age> • Corresponding element definitions • <xs:element name=“lastname” type=“xs:string”/> • <xs:element name=“age” type=“xs:integer”/> 168493: XML and Web Services (II/2546)

  14. Built-in Data Types • xs:string • xs:decimal • xs:integer • xs:boolean • xs:date • xs:time 168493: XML and Web Services (II/2546)

  15. Simple Elements: Default Values • Simple elements can have a default value • A default value is automatically assigned to the element when no value is specified <xs:element name=“To” type=“xs:string” default=“students”/> 168493: XML and Web Services (II/2546)

  16. Simple Elements: Fixed Values • A fixed value is automatically assigned to elements • We cannot specify another value <xs:element name=“To” type=“xs:string” fixed=“students”/> 168493: XML and Web Services (II/2546)

  17. XSD Attributes • All attributes are declared as simple types • Only complex elements can have attributes • An element with attributes always has a complex type definition 168493: XML and Web Services (II/2546)

  18. Defining an Attribute • Syntax: <xs:attribute name=“xxx” type=“yyy”/> Where xxx is the name of the attribute and yyy is the data type of the attribute • Examples: XML:<lname lang=“EN”>Dee</name> XSD:<xs:attribute name=“lang” type=“xs:string”/> 168493: XML and Web Services (II/2546)

  19. Default & Fixed Values for Attributes • Attributes can have a default value or a fixed value specified • Examples: • <xs:attribute name=“lang” type=“xs:string” default=“EN”/> • <xs:attribute name=“lang” type=“xs:string” fixed=“EN”/> 168493: XML and Web Services (II/2546)

  20. Optional & Required Attributes • All attributes are optional by default • To explicitly specify that the attribute is optional or required, use the “use” attribute • Examples: • <xs:attribute name=“lang” type=“xs:string” use=“optional”/> • <xs:attribute name=“lang” type=“xs:string” use=“required”/> 168493: XML and Web Services (II/2546)

  21. Restrictions on Content • When an XML element or attribute has a type defined, it puts a restriction for the element’s or attribute’s content • We can also add our own restrictions to our XML elements and attributes • These restrictions are called facets 168493: XML and Web Services (II/2546)

  22. XSD Restrictions/Facets • Restrictions are used to control acceptable values for XML elements or attributes • Many kinds of restrictions/facets • Restrictions on values • Restrictions on white spaces • Restrictions on length 168493: XML and Web Services (II/2546)

  23. Restrictions on Values • This example defines an element called “age” with a restriction. <xs:element name=“age”> <xs:simpleType> <xs:restriction base=“xs:integer”> <xs:minInclusive value=“0”/> <xs:maxInclusive value=“60”/> … </xs:element> • What range the value of age is in? • Cannot be lower than 0 or greater than 60 168493: XML and Web Services (II/2546)

  24. Restriction on a Set of Values • To limit the content of an XML element to a set of acceptable values, we would use the enumeration constraint <xs:element name=“grade”> <xs:simpleType> <xs:restriction base=“xs:string”> <xs:enumeration value=“A”/> <xs:enumeration value=“B”/> … </xs:element> 168493: XML and Web Services (II/2546)

  25. Restrictions on a Series of Values • To limit the content to a series of numbers or letters, use the pattern constraint <xs:element name=“letter”> <xs:simpleType> <xs:restriction base=“xs:string”> <xs:pattern value=“[a-z]”/> … </xs:element> • What are the acceptable values of the “letter” element? 168493: XML and Web Services (II/2546)

  26. Restrictions on a Series of Values <xs:element name="letter"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="([a-z][A-Z])+"/> … </xs:element> • What are the acceptable values of the “letter” element? 168493: XML and Web Services (II/2546)

  27. Restrictions on a Series of Values <xs:element name="password“> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-zA-Z0-9]{8}"/> … <xs:element> • What are the acceptable values of the “password” element? 168493: XML and Web Services (II/2546)

  28. Restrictions on White Space Characters • To specify how white space characters should be handled, use the whiteSpace constraint <xs:element name="address"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:whiteSpace value="preserve"/> … </xs:element • The XML processor will not remove any white space characters 168493: XML and Web Services (II/2546)

  29. Restrictions on White Space Characters • Three values of whiteSpace • preserve: the XML processor will not remove any white space characters • replace: the XML processor will replace all white space characters with spaces • collapse: the XML processor will remove all white space characters (leading and trailing spaces are removed) 168493: XML and Web Services (II/2546)

  30. Restrictions on Length <xs:element name="password"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:length value="8"/> … </xs:element> • Can we specify the minimum and maximum length of the value? • Yes, use “minLength” and “maxLength” 168493: XML and Web Services (II/2546)

  31. What is a Complex Element? • A complex element is an XML element that contains other elements and/or attributes • 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 168493: XML and Web Services (II/2546)

  32. Complex Elements • Ex1: <product pid=“1345”/> • Ex2: <a><b>1</b><c>2</c></a> • Ex3: <food type=“dessert”>Ice cream</food> • Ex4: <description>Today is <date>11.13.2003</date></description> 168493: XML and Web Services (II/2546)

  33. Defining a Complex Element <xs:element name=“employee”> <xs:complexType> <xs:sequence> <xs:element name=“firstname” type=“xs:string”/> <xs:element name=“lastname” type=“xs:string”/> … </xs:element> 168493: XML and Web Services (II/2546)

  34. Sharing the Element Type <xs:element name=“employee” type=“personinfo”/> <xs:element name=“student” type=“personinfo”/> <xs:complexType name=“personinfo”> <xs:sequence> <xs:element name=“firstname” … </xs:element> 168493: XML and Web Services (II/2546)

  35. Defining Based on Other Types <xs:element name="employee" type="fullpersoninfo"/> <xs:complexType name="personinfo"> … </xs:complexType> <xs:complexType name="fullpersoninfo"> <xs:complexContent> <xs:extension base="personinfo"> <xs:sequence> <xs:element name="address" type="xs:string"/> … </xs:complexType> 168493: XML and Web Services (II/2546)

  36. Types for Empty Elements • XML: <product prodid="1345" /> • XSD: <xs:element name="product" type="prodtype"/> <xs:complexType name="prodtype"> <xs:attribute name="prodid" type="xs:positiveInteger"/> </xs:complexType> 168493: XML and Web Services (II/2546)

  37. Elements w/ Only Sub-Elements • XML:<p><fn>A</fn><ln>B</ln></p> • XSD: <xs:element name="p" type="persontype"/> <xs:complexType name="persontype"> <xs:sequence> <xs:element name="fn" type="xs:string"/> <xs:element name="ln" type="xs:string"/> </xs:sequence> </xs:complexType> 168493: XML and Web Services (II/2546)

  38. Elements w/ Only Text • XML: <shoesize>8</shoesize> • XSD: <xs:element name="shoesize" type="shoetype"/> <xs:complexType name="shoetype"> <xs:simpleContent> <xs:extension base="xs:integer"> … </xs:complexType> 168493: XML and Web Services (II/2546)

  39. Types w/ Mixed Content • XML: <letter>Today is <date>11.13.2003</date></letter> • XSD: <xs:element name="letter"> <xs:complexType mixed="true"> <xs:sequence> <xs:element name="shipdate" type="xs:date"/> … </xs:element> 168493: XML and Web Services (II/2546)

  40. Indicators • Order indicators: • All, choice, sequence • Occurrence indicators: • maxOccurs, minOccurs • Group indicators: • Group name • attributeGroup name 168493: XML and Web Services (II/2546)

  41. All Indicator • Specify by default that the child elements can appear in any order and each child element must occur once and only once <xs:element name=“person”> <xs:complexType> <xs:all> <xs:element name=“fname” type=“xs:string”/> … </xs:element> 168493: XML and Web Services (II/2546)

  42. Choice Indicator • Specify that either one child element or another can occur <xs:element name="person"> <xs:complexType> <xs:choice> <xs:element name="employee" type="employee"/> <xs:element name="member" type="member"/> ... </xs:element> 168493: XML and Web Services (II/2546)

  43. Sequence Indicator • Specify that the child elements must appear in a specific order <xs:element name="paper"> <xs:complexType> <xs:sequence> <xs:element name=“abstract" type="xs:string"/> <xs:element name=“introduction" type="xs:string"/> … </xs:element> 168493: XML and Web Services (II/2546)

  44. Occurrence Indicator • Define how often an element can occur • maxOccurs Indicator: maximum number of times an element can occur <xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string" maxOccurs="10"/> … </xs:element> 168493: XML and Web Services (II/2546)

  45. Occurrence Indicator (Cont.) • minOccurs Indicator: minimum number of times an element can occur <xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string" maxOccurs="10“ minOccurs=“0”/> … </xs:element> 168493: XML and Web Services (II/2546)

  46. Group Indicators • Define related sets of elements • Element Groups are defined with the group declaration, like this: <xs:group name=“groupname”> … </xs:group> • After defining a group, we can reference it in another complex type definition 168493: XML and Web Services (II/2546)

  47. Element Groups <xs:group name="persongroup"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> … </xs:group> <xs:complexType name="personinfo"> <xs:sequence> <xs:group ref="persongroup"/> <xs:element name="country" type="xs:string"/> … 168493: XML and Web Services (II/2546)

  48. Attribute Groups <xs:attributeGroup name="personattrgroup"> <xs:attribute name="firstname" type="xs:string"/> … </xs:attributeGroup> <xs:element name="person"> <xs:complexType> <xs:attributeGroup ref="personattrgroup"/> … </xs:element> 168493: XML and Web Services (II/2546)

More Related