Schema

1,104 views
1,037 views

Published on

XML SCHEMA OVERVIEW

Published in: Technology, Travel
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,104
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
49
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Schema

  1. 1. SCHEMA OVERVIEW e-logistics 2009 Eduard Rodés Gubern Port de Barcelona
  2. 2. Purpose of XML Schemas <ul><li>Specify: </li></ul><ul><ul><li>the structure of instance documents </li></ul></ul><ul><ul><ul><li>“ this element contains these elements, which contains these other elements, etc&quot; </li></ul></ul></ul><ul><ul><li>the datatype of each element/attribute </li></ul></ul><ul><ul><ul><li>&quot;this element shall hold an integer with the range 0 to 12,000&quot; (DTDs don't do too well with specifying datatypes like this) </li></ul></ul></ul><ul><ul><li>XML Schema is an XML-based language used to create XML-based languages and data models. An XML schema defines element and attribute names for a class of XML documents. The schema also specifies the structure that those documents must adhere to and the type of content that each element can hold. </li></ul></ul><ul><ul><li>XML documents that attempt to adhere to an XML schema are said to be instances of that schema. If they correctly adhere to the schema, then they are valid instances. </li></ul></ul>
  3. 3. A First Look <ul><li>An XML schema describes the structure of an XML instance document by defining what each element must or may contain. An element is limited by its type. </li></ul>
  4. 4. what each element contains <ul><li>Elements can be of simple type or complex type. </li></ul><ul><li>Simple type elements can only contain text. They can not have child elements or attributes. </li></ul><ul><li>All the built-in types are simple types (e.g, xs:string). </li></ul><ul><li>Schema authors can derive simple types by restricting another simple type. For example, an email type could be derived by limiting a string to a specific pattern. </li></ul><ul><li>Simple types can be atomic (e.g, strings and integers) or non-atomic (e.g, lists). </li></ul><ul><li>Complex-type elements can contain child elements and attributes as well as text. </li></ul><ul><li>By default, complex-type elements have complex content, meaning that they have child elements. </li></ul><ul><li>Complex-type elements can be limited to having simple content, meaning they only contain text. They are different from simple type elements in that they have attributes. </li></ul><ul><li>Complex types can be limited to having no content, meaning they are empty, but they have may have attributes. </li></ul><ul><li>Complex types may have mixed content - a combination of text and child elements. </li></ul>
  5. 5. Schema – friend.xsd <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul><ul><li><xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema“ </li></ul><ul><li>targetNamespace=&quot;http://www.galactinav.com&quot; </li></ul><ul><li>xmlns=&quot;http://www.galactinav.com&quot; </li></ul><ul><li>elementFormDefault=&quot;qualified&quot;> </li></ul><ul><li><xsd:element name=&quot;friend&quot;> </li></ul><ul><li><xsd:complexType> </li></ul><ul><li><xsd:sequence> </li></ul><ul><li><xsd:element ref=&quot;name&quot; minOccurs=&quot;1&quot; maxOccurs=&quot;1&quot;/> </li></ul><ul><li><xsd:element ref=&quot;address&quot; minOccurs=&quot;1&quot; maxOccurs=&quot;unbounded&quot;/> </li></ul><ul><li></xsd:sequence> </li></ul><ul><li></xsd:complexType> </li></ul><ul><li></xsd:element> </li></ul><ul><li><xsd:element name=&quot;address&quot;> </li></ul><ul><li><xsd:complexType> </li></ul><ul><li><xsd:sequence> </li></ul><ul><li><xsd:element ref=&quot;street&quot; minOccurs=&quot;1&quot; maxOccurs=&quot;1&quot;/> </li></ul><ul><li><xsd:element ref=&quot;city&quot; minOccurs=&quot;1&quot; maxOccurs=&quot;1&quot;/> </li></ul><ul><li><xsd:element ref=&quot;country&quot; minOccurs=&quot;1&quot; maxOccurs=&quot;1&quot;/> </li></ul><ul><li><xsd:element ref=&quot;zip&quot; minOccurs=&quot;1&quot; maxOccurs=&quot;1&quot;/> </li></ul><ul><li></xsd:sequence> </li></ul><ul><li></xsd:complexType> </li></ul><ul><li></xsd:element> </li></ul><ul><li><xsd:element name=&quot;name&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li><xsd:element name=&quot;street&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li><xsd:element name=&quot;city&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li><xsd:element name=&quot;country&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li><xsd:element name=&quot;zip&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li></xsd:schema> </li></ul><ul><li><!ELEMENT friend (name, address+)> </li></ul><ul><li><!ELEMENT name (#PCDATA)> </li></ul><ul><li><!ELEMENT address (street, city, country, zip)> </li></ul><ul><li><!ELEMENT street (#PCDATA)> </li></ul><ul><li><!ELEMENT city (#PCDATA)> </li></ul><ul><li><!ELEMENT country (#PCDATA)> </li></ul><ul><li><!ELEMENT zip (#PCDATA)> </li></ul>
  6. 6. ATTLIST ELEMENT ID #PCDATA NMTOKEN ENTITY CDATA friend name zip address country city street This is the vocabulary that DTDs provide to define your new vocabulary (Source:Roger L. Costello)
  7. 7. element complexType schema sequence http://www.w3.org/2001/XMLSchema string integer boolean This is the vocabulary that XML Schemas provide to define your new vocabulary 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) [because DTDs pre-dated namespaces]. friend name zip address country city street http://www.galactinav.com ( targetNamespace ) (Source:Roger L. Costello)
  8. 8. XML instance document <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul><ul><li><friend xmlns=&quot;http://www.galactinav.com&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://www.galactinav.com friend.xsd&quot;> </li></ul><ul><li><name>El Soussy</name> </li></ul><ul><li><address> </li></ul><ul><li><street>Palestinian Gardens</street> </li></ul><ul><li><city>Alexandria</city> </li></ul><ul><li><country>EG</country> </li></ul><ul><li><zip>90210</zip> </li></ul><ul><li></address> </li></ul><ul><li></friend> </li></ul>If you want to validate the files http://tools.decisionsoft.com/schemaValidate / Or download xmlspy www.altova.com
  9. 9. Referencing a schema in an XML instance document BookStore.xml BookStore.xsd targetNamespace=&quot;http://www.books.org&quot; schemaLocation=&quot;http://www.books.org BookStore.xsd&quot; - defines elements in namespace http://www.books.org - uses elements from namespace http://www.books.org A schema defines a new vocabulary. Instance documents use that new vocabulary. (Source:Roger L. Costello)
  10. 10. Note multiple levels of checking BookStore.xml BookStore.xsd XMLSchema.xsd (schema-for-schemas) Validate that the xml document conforms to the rules described in BookStore.xsd Validate that BookStore.xsd is a valid schema document, i.e., it conforms to the rules described in the schema-for-schemas (Source:Roger L. Costello)
  11. 11. Default Value for minOccurs and maxOccurs <ul><li>The default value for minOccurs is &quot;1&quot; </li></ul><ul><li>The default value for maxOccurs is &quot;1&quot; </li></ul><xsd:element ref=&quot;Title&quot; minOccurs=&quot;1&quot; maxOccurs=&quot;1&quot;/> <xsd:element ref=&quot;Title&quot;/> Equivalent! (Source:Roger L. Costello)
  12. 12. Named Types <?xml version=&quot;1.0&quot;?> <xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot; targetNamespace=&quot;http://www.galactinav.com&quot; xmlns=&quot;http://www.galactinav.org&quot; elementFormDefault=&quot;qualified&quot;> <xsd:element name=“addressbook&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=“ friend &quot; maxOccurs=&quot;unbounded&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name=&quot;friend“> <xsd:complexType> <xsd:sequence> <xsd:element name=“name”/> <xsd:element name=“address&quot; type=“addressdata” maxOccurs=&quot;unbounded&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:complexType name=“addressdata” > <xsd:sequence> <xsd:element name=&quot;street&quot; type=“string”/> <xsd:element name=&quot;city&quot; type=“string”/> <xsd:element name=&quot;country&quot; type=“string”/> <xsd:element name=&quot;zip&quot; type=“string”/> </xsd:sequence> </xsd:complexType> </xsd:schema> The advantage of splitting out friend's element declarations and wrapping them in a named type is that now this type can be reused by other elements. Named type
  13. 13. Built-in datatypes (Source:W3C Recomendation)
  14. 14. Built-in datatypes Primitive Note: 'T' is the date/time separator INF = infinity NAN = not-a-number a NOTATION from the XML spec NOTATION a namespace qualified name QName http://www.xfront.com anyURI a base64 string base64Binary a hex string hexBinary format: --MM-- gMonth format: ---DD (note the 3 dashes) gDay format: --MM-DD gMonthDay format: CCYY gYear format: CCYY-MM gYearMonth format: CCYY-MM-DD date format: hh:mm:ss.sss time format: CCYY-MM-DDThh-mm-ss dateTime P1Y2M3DT10H30M12.3S duration 12.56E3, 12, 12560, 0, -0, INF, -INF, NAN double 12.56E3, 12, 12560, 0, -0, INF, -INF, NAN float 7.08 decimal {true, false} boolean &quot;Hello World&quot; string
  15. 15. Built-in datatypes Derived 1 to infinity positiveInteger 0 to 255 unsignedByte 0 to 65535 unsignedShort 0 to 4294967295 unsignedInt 0 to 18446744073709551615 unsignedLong 0 to infinity nonNegativeInteger -127 to 128 byte -32768 to 32767 short -2147483648 to 2147483647 int -9223372036854775808 to 9223372036854775808 long negative infinity to -1 negativeInteger negative infinity to 0 nonPositiveInteger 456 integer must be used only with attributes ENTITY must be used only with attributes IDREF must be used only with attributes ID part (no namespace qualifier) NCName   Name must be used only with attributes NMTOKENS must be used only with attributes NMTOKEN must be used only with attributes ENTITIES must be used only with attributes IDREFS any valid xml:lang value, e.g., EN, FR, ... language String w/o tabs, l/f, leading/trailing spaces, consecutive spaces token A string without tabs, line feeds, or carriage returns normalizedString
  16. 16. Attributes <ul><li>We take the html img attributes </li></ul><ul><li><ATTLIST img </li></ul><ul><ul><li>src CDATA #REQUIRED </li></ul></ul><ul><ul><li>alt CDATA #REQUIRED </li></ul></ul><ul><ul><li>height CDATA #IMPLIED </li></ul></ul><ul><ul><li>width CDATA #IMPLIED> </li></ul></ul><xs:element name=&quot;img&quot;> <xs:complexType> <xs:attributeGroup ref=&quot;attrs&quot;/> <xs:attribute name=&quot;src&quot; use=&quot;required&quot; type=&quot;URI&quot;/> <xs:attribute name=&quot;alt&quot; use=&quot;required&quot; type=&quot;Text&quot;/> <xs:attribute name=&quot;height&quot; type=&quot;Length&quot;/> <xs:attribute name=&quot;width&quot; type=&quot;Length&quot;/> </xs:complexType> </xs:element>

×