o9schema
DESCRIPTION
Breve introducción a SchemaTRANSCRIPT
SCHEMA OVERVIEW
e-logistics2009
Eduard Rodés GubernPort de Barcelona
Purpose of XML Schemas Specify:
the structure of instance documents “this element contains these elements, which contains these other
elements, etc" the datatype of each element/attribute
"this element shall hold an integer with the range 0 to 12,000" (DTDs don't do too well with specifying datatypes like this)
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.
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.
A First Look
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.
what each element contains1. Elements can be of simple type or complex type.
2. Simple type elements can only contain text. They can not have child elements or attributes.
3. All the built-in types are simple types (e.g, xs:string).
4. 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.
5. Simple types can be atomic (e.g, strings and integers) or non-atomic (e.g, lists).
6. Complex-type elements can contain child elements and attributes as well as text.
7. By default, complex-type elements have complex content, meaning that they have child elements.
8. 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.
9. Complex types can be limited to having no content, meaning they are empty, but they have may have attributes.
10. Complex types may have mixed content - a combination of text and child elements.
Schema – friend.xsd<?xml version="1.0" encoding="UTF-8"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema“ targetNamespace="http://www.galactinav.com"
xmlns="http://www.galactinav.com" elementFormDefault="qualified"><xsd:element name="friend">
<xsd:complexType><xsd:sequence>
<xsd:element ref="name" minOccurs="1" maxOccurs="1"/><xsd:element ref="address" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence></xsd:complexType>
</xsd:element><xsd:element name="address">
<xsd:complexType><xsd:sequence>
<xsd:element ref="street" minOccurs="1" maxOccurs="1"/><xsd:element ref="city" minOccurs="1" maxOccurs="1"/><xsd:element ref="country" minOccurs="1" maxOccurs="1"/><xsd:element ref="zip" minOccurs="1" maxOccurs="1"/>
</xsd:sequence></xsd:complexType>
</xsd:element><xsd:element name="name" type="xsd:string"/><xsd:element name="street" type="xsd:string"/><xsd:element name="city" type="xsd:string"/><xsd:element name="country" type="xsd:string"/><xsd:element name="zip" type="xsd:string"/></xsd:schema>
<!ELEMENT friend (name, address+)><!ELEMENT name (#PCDATA)><!ELEMENT address (street, city, country, zip)><!ELEMENT street (#PCDATA)><!ELEMENT city (#PCDATA)><!ELEMENT country (#PCDATA)><!ELEMENT zip (#PCDATA)>
ATTLISTELEMENT
ID
#PCDATA
NMTOKEN
ENTITY
CDATA
friend
namezip
address
country
citystreet
This is the vocabulary that DTDs provide to define yournew vocabulary
(Source:Roger L. Costello)
elementcomplexType
schema
sequence
http://www.w3.org/2001/XMLSchema
string
integer
boolean
This is the vocabulary that XML Schemas provide to define yournew vocabulary
One difference between XML Schemas and DTDs is that the XML Schema vocabularyis associated with a name (namespace). Likewise, the new vocabulary that you define must be associated with a name (namespace). With DTDs neither set ofvocabulary is associated with a name (namespace) [because DTDs pre-dated namespaces].
friend
namezip
address
country
citystreet
http://www.galactinav.com (targetNamespace)
(Source:Roger L. Costello)
XML instance document<?xml version="1.0" encoding="UTF-8"?><friend xmlns="http://www.galactinav.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.galactinav.com friend.xsd">
<name>El Soussy</name><address>
<street>Palestinian Gardens</street><city>Alexandria</city><country>EG</country><zip>90210</zip>
</address></friend> If you want to validate the files
http://tools.decisionsoft.com/schemaValidate/
Or download xmlspy www.altova.com
Referencing a schema in an XML instance document
BookStore.xml BookStore.xsd
targetNamespace="http://www.books.org"schemaLocation="http://www.books.org BookStore.xsd"
- 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)
Note multiple levels of checking
BookStore.xml BookStore.xsd XMLSchema.xsd(schema-for-schemas)
Validate that the xml documentconforms to the rules describedin BookStore.xsd
Validate that BookStore.xsd is a validschema document, i.e., it conformsto the rules described in theschema-for-schemas
(Source:Roger L. Costello)
Default Value for minOccurs and maxOccurs The default value for minOccurs is "1" The default value for maxOccurs is "1"
<xsd:element ref="Title" minOccurs="1" maxOccurs="1"/>
<xsd:element ref="Title"/>
Equivalent!
(Source:Roger L. Costello)
<?xml version="1.0"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.galactinav.com" xmlns="http://www.galactinav.org" elementFormDefault="qualified"> <xsd:element name=“addressbook"> <xsd:complexType> <xsd:sequence> <xsd:element name=“friend" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="friend“>
<xsd:complexType><xsd:sequence>
<xsd:element name=“name”/><xsd:element name=“address" type=“addressdata” maxOccurs="unbounded"/>
</xsd:sequence></xsd:complexType>
</xsd:element><xsd:complexType name=“addressdata”>
<xsd:sequence><xsd:element name="street" type=“string”/><xsd:element name="city" type=“string”/><xsd:element name="country" type=“string”/><xsd:element name="zip" type=“string”/>
</xsd:sequence></xsd:complexType>
</xsd:schema>
Named type
The advantage ofsplitting out friend'selement declarationsand wrapping themin a named type isthat now this typecan be reused byother elements.
Named Types
Built-in datatypes
(Source:W3C Recomendation)
Built-in datatypesPrimitive
Note: 'T' is the date/time separator INF = infinity NAN = not-a-number
string "Hello World"
boolean {true, false}
decimal 7.08
float 12.56E3, 12, 12560, 0, -0, INF, -INF, NAN
double 12.56E3, 12, 12560, 0, -0, INF, -INF, NAN
duration P1Y2M3DT10H30M12.3S
dateTime format: CCYY-MM-DDThh-mm-ss
time format: hh:mm:ss.sss
date format: CCYY-MM-DD
gYearMonth format: CCYY-MM
gYear format: CCYY
gMonthDay format: --MM-DD
gDay format: ---DD (note the 3 dashes)
gMonth format: --MM--
hexBinary a hex string
base64Binary a base64 string
anyURI http://www.xfront.com
QName a namespace qualified name
NOTATION a NOTATION from the XML spec
Built-in datatypesDerived
normalizedString A string without tabs, line feeds, or carriage returnstoken String w/o tabs, l/f, leading/trailing spaces, consecutive spaceslanguage any valid xml:lang value, e.g., EN, FR, ... IDREFS must be used only with attributesENTITIES must be used only with attributesNMTOKEN must be used only with attributesNMTOKENS must be used only with attributesName NCName part (no namespace qualifier)ID must be used only with attributesIDREF must be used only with attributesENTITY must be used only with attributesinteger 456nonPositiveInteger negative infinity to 0negativeInteger negative infinity to -1long -9223372036854775808 to 9223372036854775808 int -2147483648 to 2147483647short -32768 to 32767byte -127 to 128nonNegativeInteger 0 to infinityunsignedLong 0 to 18446744073709551615unsignedInt 0 to 4294967295unsignedShort 0 to 65535unsignedByte 0 to 255positiveInteger 1 to infinity
Attributes We take the html img attributes <ATTLIST img
src CDATA #REQUIREDalt CDATA #REQUIREDheight CDATA #IMPLIEDwidth CDATA #IMPLIED>
<xs:element name="img"> <xs:complexType> <xs:attributeGroup ref="attrs"/> <xs:attribute name="src" use="required" type="URI"/> <xs:attribute name="alt" use="required" type="Text"/>
<xs:attribute name="height" type="Length"/> <xs:attribute name="width" type="Length"/> </xs:complexType></xs:element>