120 likes | 285 Vues
Leonidas Fegaras. XML Schema. XML Schema. Replaced DTD Object-Oriented schemas Richer set of basic types Better ways of deriving new type declarations from old ones type extension type restriction Database-style key concept Uses namespaces Compatible with other standards. Base Types.
E N D
Leonidas Fegaras XML Schema
XML Schema • Replaced DTD • Object-Oriented schemas • Richer set of basic types • Better ways of deriving new type declarations from old ones • type extension • type restriction • Database-style key concept • Uses namespaces • Compatible with other standards
Base Types • string, float, double, decimal, boolean • timeDuration P10Y1M5T1H20M5S 10years+1month+5days+1hour+20'+5'' • binary • uriReference • QName • ID, IDref • A basic type may have properties, called facets • length, minLength, maxLength • minInclusive, maxInclusive, minExclusive, maxExclusive • enumeration for string • pattern for string • encoding for binary (eg, base64)
Predefined Derived Types • Derived by restrictions on facets <xsd:simpleType name='integer' base=xsd:decimal'> <scale value='0'> </xsd:simpleType> • or multiplied in lists <xsd:simpleType name='IDREFS' base='xsd:IDREF' derivedBy='xsd:list'/> • Other built-in derived types: • Name • int • long • short • byte • positiveInteger
Defining Simple Types • Enumeration <xsd:simpleType name='animal' base='xsd:string'> <xsd:enumeration value='dog'/> <xsd:enumeration value='cat'/> </xsd:simpleType> • Range restriction <xsd:simpleType name='employeeAge' base='xsd:positiveInteger'> <xsd:minInclusive value='18'/> <xsd:maxInclusive value='70'/> </xsd:simpleType> • Pattern <xsd:simpleType name='ssn' base='xsd:string'> <xsd:pattern value='\d{3}-\d{2}-\d{4}'> </xsd:simpleType> • List <xsd:simpleType name='animals' base='animal' derivedBy='xsd:list'/>
Elements • Example: <xsd:element name='employee'> <xsd:complexType> <xsd:sequence> <xsd:element name='name' type='xsd:string'/> <xsd:element name='address' type='xsd:string'/> <xsd:element name='ssn' type='ssn'/> </xsd:sequence> </xsd:complexType> </xsd:element> • It corresponds to the DTD: <!ELEMENT employee (name,address,ssn)> <!ELEMENT name (#PCDATA)> <!ELEMENT address (#PCDATA)> <!ELEMENT ssn (#PCDATA)>
Element Attributes • Element attributes can only appear inside xsd:complexType in an xsd:element <xsd:attribute name='ssn' type='ssn' use='required'/> <xsd:attribute name='father' type='xsd:string' use='optional'/> • Keys can be defined using XPath expressions <xsd:key name='ssn'> <xsd:selector>dept/person</xsd:selector> <xsd:field>@ssn</xsd:field> </xsd:key> • the selector defines the applicable elements • the field defines the data that are unique withing the applicable elements • xsd:unique may be used to define unique combination of values • Foreign keys are like IDRefs, but well-typed <xsd:keyref refer='ssn'> <xsd:selector>dept/person</xsd:selector> <xsd:field>@mother</xsd:field> <xsd:keyref>
Other Specifiers of xsd:element • Other attributes of xsd:element • minOccurs: minimum number of occurrences • default = '1' • maxOccurs: maximum number of occurrences • default = '1' if minOccurs is not given • default = '*' otherwise • optional is when minOccurs='0' and maxOccurs=‘1' • nullable = 'true' • ref = 'type-name' • Other attributes of xsd:complexType • content = 'empty' • content = 'mixed' • content = 'elementOnly' • derivedBy = 'extension' • derivedBy = 'restriction'
xsd:complexType • Any content <xsd:any> • Concatenation (A1, A2, ..., An) <xsd:sequence> A1 A2 ... An </xsd:sequence> • Alternation (A1 | A2 | ... | An) <xsd:choice> A1 A2 ... An </xsd:choice> • All but in any order (A1 & A2 & ... & An) <xsd:all> A1 A2 ... An </xsd:all>
Using xsd:complexType <xsd:simpleType name='state' base='xsd:string'> <xsd:enumeration value='TX'> <xsd:enumeration value='CA'> </xsd:simpleType> <xsd:simpleType name='zip' base='xsd:string'> <xsd:pattern value='\d{5}'> </xsd:simpleType> <xsd:complexType name='Address' content='elementOnly'> <xsd:sequence> <xsd:element name='name' type='xsd:string'/> <xsd:element name='street' type='xsd:string'/> <xsd:element name='state' type='state'/> <xsd:element name='zip' type='zip'/> </xsd:sequence> </xsd:complexType> <xsd:element name='PurchaseOrder'> <xsd:complexType content='elementOnly'> <xsd:sequence> <xsd:element name='From' type='Address'/> <xsd:element name='To' type='Address'/> </xsd:sequence> </xsd:complexType> </xsd:element>
Reusing Types • Extending types <xsd:complexType name='GraduateStudent' base='Student' derivedBy='extension'> <xsd:element name='gpa' type='xsd:positiveInteger'/> </xsd:complexType> • Restricting types <xsd:simpleType name="SKU"> <xsd:restriction base="xsd:string"> <xsd:pattern value="\d{3}-[A-Z]{2}"/> </xsd:restriction> </xsd:simpleType>
Using Schemas • Schema “po.xsd” <schema xmlns="http://www.w3.org/2001/XMLSchema"> <element name='PurchaseOrder'> <complexType content='elementOnly'> ... </complexType> </element> </schema> • Using targetNamespace: <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:po="http://lambda.uta.edu/PO" targetNamespace="http://lambda.uta.edu/PO"> <element name='PurchaseOrder'> <complexType content='elementOnly'> ... </complexType> </element> </schema> • XML data that conforms to this XML Schema: <?xml version="1.0"?> <po:PurchaseOrder xmlns:po="http://lambda.uta.edu/PO">