Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

XSD

1,703 views

Published on

XML Schema Definition Intro

XSD

  1. 1. XSD(XML Schema Definition)
  2. 2. What is an XML Schema? <ul><li>The purpose of an XML Schema is to define the legal building blocks of an XML document, just like a DTD. </li></ul><ul><li>  </li></ul><ul><li>An XML Schema: </li></ul><ul><li>Defines elements that can appear in a document </li></ul><ul><li>Defines attributes that can appear in a document </li></ul><ul><li>Defines which elements are child elements </li></ul><ul><li>Defines the order of child elements </li></ul><ul><li>Defines the number of child elements </li></ul><ul><li>Defines whether an element is empty or can include text </li></ul><ul><li>Defines data types for elements and attributes </li></ul><ul><li>Defines default and fixed values for elements and attributes </li></ul><ul><li>XML Schemas are extensible to future additions </li></ul><ul><li>XML Schemas are written in XML </li></ul><ul><li>XML Schemas support data types </li></ul><ul><li>XML Schemas support namespaces </li></ul>
  3. 3. An XML And Schema Document <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><note> </li></ul><ul><li><to>Tove</to> </li></ul><ul><li><from>Jani</from> </li></ul><ul><li><heading>Reminder</heading> </li></ul><ul><li><body>Don't forget me this weekend!</body> </li></ul><ul><li></note> </li></ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema </li></ul><ul><li>targetNamespace=http://www.w3schools.com </li></ul><ul><li>xmlns=http://www.w3schools.com </li></ul><ul><li>elementFormDefault=&quot;qualified&quot;> </li></ul><ul><li><xs:element name=&quot;note&quot;> </li></ul><ul><li><xs:complexType> </li></ul><ul><li><xs:sequence> </li></ul><ul><li><xs:element name=&quot;to&quot; type=&quot;xs:string&quot;/> </li></ul><ul><li><xs:element name=&quot;from&quot; type=&quot;xs:string&quot;/> </li></ul><ul><li><xs:element name=&quot;heading&quot; type=&quot;xs:string&quot;/> </li></ul><ul><li><xs:element name=&quot;body&quot; type=&quot;xs:string&quot;/> </li></ul><ul><li></xs:sequence> </li></ul><ul><li></xs:complexType> </li></ul><ul><li></xs:element> </li></ul><ul><li></xs:schema> </li></ul>
  4. 4. A Reference to an XML Schema <ul><li>XML document has a refering to an XML Schema: </li></ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><note xmlns=&quot;http://www.w3schools.com&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; </li></ul><ul><li>xsi:schemaLocation=&quot;http://www.w3schools.com note.xsd&quot;> </li></ul><ul><li><to>Tove</to> </li></ul><ul><li><from>Jani</from> </li></ul><ul><li><heading>Reminder</heading> </li></ul><ul><li><body>Don't forget me this weekend!</body> </li></ul><ul><li></note> </li></ul>
  5. 5. <schema> element <ul><li>The <schema> element is the root element of every XML Schema and may contain some attributes . </li></ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><ul><li><xs:schema xmlns:xs= http://www.w3.org/2001/XMLSchema </li></ul></ul><ul><ul><li>targetNamespace= http://www.w3schools.com </li></ul></ul><ul><ul><li>xmlns= http://www.w3schools.com </li></ul></ul><ul><ul><li>elementFormDefault=&quot;qualified&quot;> </li></ul></ul><ul><ul><li></xs:schema> </li></ul></ul><ul><ul><li>xmlns:xs= http://www.w3.org/2001/ XMLSchema </li></ul></ul><ul><ul><li>1. Indicates that the elements and data types used in the schema come from the &quot;http://www.w3.org/2001/XMLSchema&quot; namespace. </li></ul></ul><ul><ul><li>2. Specifies that the elements and data types that come from the &quot;http://www.w3.org/2001/XMLSchema&quot; namespace should be prefixed with xs: </li></ul></ul>
  6. 6. <ul><li>targetNamespace= http://www.w3schools.com </li></ul><ul><li>Indicates that the elements defined by this schema (note, to, from, heading, body.) come from the &quot;http://www.w3schools.com&quot; namespace. </li></ul><ul><li>xmlns= http://www.w3schools.com </li></ul><ul><ul><li>indicates that the default namespace is &quot;http://www.w3schools.com&quot;. </li></ul></ul><ul><li>elementFormDefault=&quot;qualified“ </li></ul><ul><ul><li>Indicates that any elements used by the XML instance document, which were declared in this schema must be namespace qualified. </li></ul></ul><ul><li>xsi:schemaLocation=&quot;http://www.w3schools.com note.xsd&quot; </li></ul><ul><ul><li>SchemaLocation attribute has two values. The first value is the namespace to use. The second value is the location of the XML schema to use for that namespace: </li></ul></ul>
  7. 7. <ul><li>XML Schemas define the elements of XML files. </li></ul><ul><li>Schema defines two types of XML elements. </li></ul><ul><li>1. Simple Type </li></ul><ul><li>2. Complex Type </li></ul><ul><li>A “ simple ” element is one that contains text and nothing else </li></ul><ul><li>A simple element cannot have attributes </li></ul><ul><li>A simple element cannot contain other elements </li></ul><ul><li>A simple element cannot be empty </li></ul>
  8. 8. Simple Element <ul><li>Syntax: </li></ul><ul><li><xs:element name=&quot; name &quot; type=&quot; type &quot; /> </li></ul><ul><li>where: </li></ul><ul><li>name is the name of the element </li></ul><ul><li>The most common values for type are </li></ul><ul><li> xs:boolean xs:integer xs:date xs:string xs:decimal xs:time </li></ul><ul><li>  </li></ul><ul><li>Other attributes a simple element may have: </li></ul><ul><li>  </li></ul><ul><li>default=&quot; default value &quot; if no other value is specified </li></ul><ul><li>fixed=&quot; value &quot; no other value may be specified </li></ul>
  9. 9. Defining an Attribute <ul><li>• Attributes themselves are always declared as simple types. </li></ul><ul><li>  </li></ul><ul><li>• An attribute is defined as </li></ul><ul><li> <xs:attribute name=&quot; name &quot; type=&quot; type &quot; /> </li></ul><ul><li>where: </li></ul><ul><li>name and type are the same as for xs:element </li></ul><ul><li>  </li></ul><ul><li>• Other attributes a simple element may have: </li></ul><ul><li>default=&quot; default value &quot; if no other value is specified </li></ul><ul><li>  </li></ul><ul><li>fixed=&quot; value “ no other value may be specified </li></ul><ul><li>use=&quot;optional&quot; the attribute is not required (default) </li></ul><ul><li>  </li></ul><ul><li>use=&quot;required&quot; the attribute must be present </li></ul>
  10. 10. Restrictions or Facets <ul><li>• The general form for putting a restriction on a text value is: </li></ul><ul><li><xs:element name=&quot; name &quot;> (or xs:attribute ) <xs:restriction base=&quot; type &quot;> ... the restrictions ... </xs:restriction> </xs:element> </li></ul><ul><li>  </li></ul><ul><li>• For example: </li></ul><ul><li><xs:element name=&quot;age&quot;> <xs:restriction base=&quot;xs:integer&quot;> <xs:minInclusive value=&quot;0&quot;> <xs:maxInclusive value=&quot;140&quot;> </xs:restriction> </xs:element> </li></ul>
  11. 11. <ul><li>Restrictions on numbers </li></ul><ul><li>  </li></ul><ul><li>minInclusive -- number must be ≥ the given value </li></ul><ul><li>minExclusive -- number must be > the given value </li></ul><ul><li>maxInclusive -- number must be ≤ the given value </li></ul><ul><li>maxExclusive -- number must be < the given value </li></ul><ul><li>totalDigits -- number must have exactly value digits </li></ul><ul><li>fractionDigits -- number must have no more than value digits after the decimal point </li></ul>
  12. 12. <ul><li>Resitriction on strings </li></ul><ul><li>  length -- the string must contain exactly value characters </li></ul><ul><li>minLength -- the string must contain at least value characters </li></ul><ul><li>maxLength -- the string must contain no more than value characters </li></ul><ul><li>pattern -- the value is a regular expression that the string must match </li></ul><ul><li><xs:element name=&quot;password&quot;> </li></ul><ul><li><xs:simpleType> </li></ul><ul><li><xs:restriction base=&quot;xs:string&quot;> </li></ul><ul><li><xs:pattern value=&quot;[a-zA-Z0-9] </li></ul><ul><li><xs:minLength value=&quot;5&quot;/> </li></ul><ul><li><xs:maxLength value=&quot;8&quot;/> </li></ul><ul><li></xs:restriction> </li></ul><ul><li></xs:simpleType> </li></ul><ul><li></xs:element> </li></ul>
  13. 13. <ul><li>Restrictions on Whitespace Characters </li></ul><ul><li>value=&quot;preserve&quot; Keep all whitespace </li></ul><ul><li>value=&quot;replace&quot; Change all whitespace characters to spaces </li></ul><ul><li>value=&quot;collapse&quot; Remove leading and trailing whitespace, and replace all sequences of whitespace with a single space </li></ul><ul><li><xs:element name=&quot;address&quot;> </li></ul><ul><li><xs:simpleType> </li></ul><ul><li><xs:restriction base=&quot;xs:string&quot;> </li></ul><ul><li><xs:whiteSpace value=&quot;preserve&quot;/> </li></ul><ul><li></xs:restriction> </li></ul><ul><li></xs:simpleType> </li></ul><ul><li></xs:element> </li></ul>
  14. 14. Enumeration <ul><li>An enumeration restricts the value to be one of a fixed set of values. </li></ul><ul><li><xs:element name=&quot;car&quot; type=&quot;carType&quot;/> </li></ul><ul><li><xs:simpleType name=&quot;carType&quot;> </li></ul><ul><li><xs:restriction base=&quot;xs:string&quot;> </li></ul><ul><li><xs:enumeration value=&quot;Audi&quot;/> </li></ul><ul><li><xs:enumeration value=&quot;Golf&quot;/> </li></ul><ul><li><xs:enumeration value=&quot;BMW&quot;/> </li></ul><ul><li></xs:restriction> </li></ul><ul><li></xs:simpleType> </li></ul><ul><li><xs:element name=&quot;car&quot;> </li></ul><ul><li><xs:simpleType> </li></ul><ul><li><xs:restriction base=&quot;xs:string&quot;> </li></ul><ul><li><xs:enumeration value=&quot;Audi&quot;/> </li></ul><ul><li><xs:enumeration value=&quot;Golf&quot;/> </li></ul><ul><li><xs:enumeration value=&quot;BMW&quot;/> </li></ul><ul><li></xs:restriction> </li></ul><ul><li></xs:simpleType> </li></ul><ul><li></xs:element> </li></ul>
  15. 15. Complex Element <ul><li>A complex element is an XML element that contains other elements and/or attributes. </li></ul><ul><li>There are four kinds of complex elements: </li></ul><ul><li>empty elements </li></ul><ul><li>elements that contain only other elements </li></ul><ul><li>elements that contain only text </li></ul><ul><li>elements that contain both other elements and text </li></ul><ul><li> <xs:element name=&quot; name &quot;> <xs:complexType> ... information about the complex type... </xs:complexType> </xs:element>  </li></ul><ul><li>Example: <xs:element name=&quot;person&quot;> <xs:complexType> <xs:sequence> <xs:element name=&quot;firstName&quot; type=&quot;xs:string&quot; /> <xs:element name=&quot;lastName&quot; type=&quot;xs:string&quot; /> </xs:sequence> </xs:complexType> </xs:element> </li></ul><ul><li><xs:sequence> Implies that elements must occur in this order </li></ul>
  16. 16. <ul><li>Complex Empty Elements </li></ul><ul><li>An empty complex element cannot have contents, only attributes. </li></ul><ul><li>Schema </li></ul><ul><li><xs:element name=&quot;product&quot;> </li></ul><ul><li><xs:complexType> </li></ul><ul><li><xs:attribute name=&quot;prodid“ type=&quot;xs:positiveInteger&quot;/> </li></ul><ul><li></xs:complexType> </li></ul><ul><li></xs:element> </li></ul><ul><li>XML </li></ul><ul><li><product prodid=&quot;1345&quot; /> </li></ul>
  17. 17. <ul><li>Complex Types Containing Elements Only </li></ul><ul><li>An &quot;elements-only&quot; complex type contains an element that contains only other elements. </li></ul><ul><li>Schema </li></ul><ul><li><xs:element name=&quot;person&quot; type=&quot;persontype&quot;/> </li></ul><ul><li><xs:complexType name=&quot;persontype&quot;> </li></ul><ul><li><xs:sequence> </li></ul><ul><li><xs:element name=&quot;firstname&quot; type=&quot;xs:string&quot;/> </li></ul><ul><li><xs:element name=&quot;lastname&quot; type=&quot;xs:string&quot;/> </li></ul><ul><li></xs:sequence> </li></ul><ul><li></xs:complexType> </li></ul><ul><li>XML </li></ul><ul><li><person> </li></ul><ul><ul><li><firstname>John</firstname> </li></ul></ul><ul><ul><li><lastname>Smith</lastname> </li></ul></ul><ul><li></person> </li></ul>
  18. 18. <ul><li>A mixed complex type element can contain attributes, elements, and text. </li></ul><ul><li>Schema </li></ul><ul><li><xs:element name=&quot;letter&quot;> </li></ul><ul><li><xs:complexType mixed=&quot;true&quot;> </li></ul><ul><li><xs:sequence> </li></ul><ul><li><xs:element name=&quot;name&quot; type=&quot;xs:string&quot;/> </li></ul><ul><li><xs:element name=&quot;orderid&quot; type=&quot;xs:positiveInteger&quot;/> </li></ul><ul><li><xs:element name=&quot;shipdate&quot; type=&quot;xs:date&quot;/> </li></ul><ul><li></xs:sequence> </li></ul><ul><li></xs:complexType> </li></ul><ul><li></xs:element> </li></ul><ul><li>XML </li></ul><ul><li><letter> </li></ul><ul><li>Dear Mr.<name>John Smith</name>.Your order <orderid>1032</orderid>will be shipped on <shipdate>2001-07-13</shipdate> </li></ul><ul><li></letter> </li></ul>
  19. 19. Indicators <ul><li>HOW elements are to be used in documents are controlled with indicators. </li></ul><ul><li>There are seven indicators: </li></ul><ul><li>Order indicators: </li></ul><ul><li>All </li></ul><ul><li>Choice </li></ul><ul><li>Sequence </li></ul><ul><li>Occurrence indicators: </li></ul><ul><li>maxOccurs </li></ul><ul><li>minOccurs </li></ul><ul><li>  </li></ul><ul><li>  Group indicators: </li></ul><ul><li>Group name </li></ul><ul><li>attributeGroup name </li></ul>
  20. 20. <ul><li>All Indicator </li></ul><ul><li>The <all> indicator specifies that the child elements can appear in any order, and that each child element must occur only once: </li></ul><ul><li><xs:element name=&quot;person&quot;> </li></ul><ul><li><xs:complexType> </li></ul><ul><li><xs:all> </li></ul><ul><li><xs:element name=&quot;firstname&quot; type=&quot;xs:string&quot;/> </li></ul><ul><li><xs:element name=&quot;lastname&quot; type=&quot;xs:string&quot;/> </li></ul><ul><li></xs:all> </li></ul><ul><li></xs:complexType></xs:element> </li></ul><ul><li>Choice Indicator </li></ul><ul><li>The <choice> indicator specifies that either one child element or another can occur out of N elements </li></ul>
  21. 21. <ul><li>maxOccurs Indicator </li></ul><ul><li>The <maxOccurs> indicator specifies the maximum number of times an element can occur: </li></ul><ul><li><xs:element name=&quot;person&quot;> </li></ul><ul><li><xs:complexType> </li></ul><ul><li><xs:sequence> </li></ul><ul><li><xs:element name=&quot;full_name&quot; type=&quot;xs:string&quot;/> </li></ul><ul><li><xs:element name=&quot;child_name&quot; type=&quot;xs:string&quot; maxOccurs=&quot;10&quot; minOccurs=&quot;0&quot;/> </li></ul><ul><li></xs:sequence> </li></ul><ul><li></xs:complexType> </li></ul><ul><li></xs:element> </li></ul>
  22. 22. <ul><li>Group Indicators </li></ul><ul><li>Group indicators are used to define related sets of elements. </li></ul><ul><li><xs:group name=&quot;persongroup&quot;> </li></ul><ul><li><xs:sequence> </li></ul><ul><li><xs:element name=&quot;firstname&quot; type=&quot;xs:string&quot;/> </li></ul><ul><li><xs:element name=&quot;lastname&quot; type=&quot;xs:string&quot;/> </li></ul><ul><li><xs:element name=&quot;birthday&quot; type=&quot;xs:date&quot;/> </li></ul><ul><li></xs:sequence> </li></ul><ul><li></xs:group> </li></ul><ul><li><xs:element name=&quot;person&quot; type=&quot;personinfo&quot;/> </li></ul><ul><li><xs:complexType name=&quot;personinfo&quot;> </li></ul><ul><li><xs:sequence> </li></ul><ul><li><xs:group ref=&quot;persongroup&quot;/> </li></ul><ul><li><xs:element name=&quot;country&quot; type=&quot;xs:string&quot;/> </li></ul><ul><li></xs:sequence> </li></ul><ul><li></xs:complexType> </li></ul>
  23. 23. Element Substitution <ul><li>With XML Schemas, one element can substitute another element . </li></ul><ul><li><xs:element name=&quot;name&quot; type=&quot;xs:string&quot;/> </li></ul><ul><li><xs:element name=&quot;navn&quot; substitutionGroup=&quot;name&quot;/> </li></ul><ul><li><xs:element name=&quot;name&quot; type=&quot;xs:string&quot;/> </li></ul><ul><li><xs:element name=&quot;navn&quot; substitutionGroup=&quot;name&quot;/> </li></ul><ul><li><xs:complexType name=&quot;custinfo&quot;> </li></ul><ul><li><xs:sequence> </li></ul><ul><li><xs:element ref=&quot;name&quot;/> </li></ul><ul><li></xs:sequence> </li></ul><ul><li></xs:complexType> </li></ul><ul><li><xs:element name=&quot;customer&quot; type=&quot;custinfo&quot;/> </li></ul><ul><li><xs:element name=&quot;kunde&quot; substitutionGroup=&quot;customer&quot;/> </li></ul><ul><li>A valid XML document (according to the schema above) could look like this: </li></ul><ul><li><customer> <name>John Smith</name> </customer> </li></ul><ul><li>OR </li></ul><ul><li><kunde> <navn>John Smith</navn> </kunde> </li></ul>

×