260 likes | 512 Views
XML Schemas. Chapter 16. Introduction to Schemas. Schemas are more powerful than DTDs They can describe complex restrictions on elements and attributes. A Schema is a formal description of what comprises a valid document. An XML Schema is written in XML
E N D
XML Schemas Chapter 16
Introduction to Schemas • Schemas are more powerful than DTDs • They can describe complex restrictions on elements and attributes. • A Schema is a formal description of what comprises a valid document. • An XML Schema is written in XML • Formal description of what comprises a valid XML document
Introduction to Schemas Pt. 2 • Instance Document – XML document described by schema • If it satisfies schema constraints, document is schema-valid. • xsi:schemaLocation attribute contains list of namespaces within element and URLs of the schemas that validate elements in namespaces. • xsi:noNamespaceSchemaLocation attribute contains URL for schema containing elements not in any namespace.
Schemas Versus DTDs • W3C XML Schema standard includes: • Simple and complex data types • Type derivation and inheritance • Element occurrence constraints • Namespace aware element and attribute declarations. • You can declare your own data types as well • You can inherit from existing data types and reuse types from other schemas. • Better restrict number and sequence of child elements that appear in a given location even when using mixed content. • DTD attributes can have default/fixed values. Schema elements can have default values: <xs: element name=“myName” type=“xs:string” default=“Joe” /> • Unlike default attribute values, default and fixed element values are only added to the document if the element is present. • However, there are no entities in schemas. However the fixed attribute can be used to represent another value.
Namespace Issues • Schemas provide explicit support for namespaces since its recommendation was passed AFTER the namespace recommendation was passed. • Schemas validate against the combination of the namespace URI and local name rather than by the prefixed name. • Namespaces are used within instance documents to include directives to the schema processor. • SchemaLocation and noNamespaceSchemaLocation must be associated with official XML Schema instance namespace URI: http://www.w3.org/2001/XMLSchema-instance or else the schema processor won’t recognize the instruction. • Information preceded with xs: is part of the xml schema instance namespace URI. • Any information without a prefix is part of the structure being defined, unless it is in a user defined namespace.
Document Organization • Every schema document that doesn’t contain namespaces consists of a single root xs:schema element. • xs:schema contains declarations for all elements/attributes that can appear in valid instance document. • XMLelements in a n XML schema must belong to the XML schema namespace at http://www.w3.org/2001/XMLSchema • This namespace is usually associated with the xs prefix. • It is an error to declare two elements with global scope (root elements)
Annotations • Parsers are not obliged to keep comments intact when parsing XML documents. • Use the optional xs:annotation element generally as the first child element. • Include any combination of xs:documentation (human-readable information) and xs:appinfo (machine-readable information) elements as child elements.
xs:documentation Element • See example on 258-259 • Permits xml:lang attribute to identify message’s language • xml:lang can also be added to xs:schema element to set document’s language • xs:documentation can contain any well formed XML
xs:appinfo Element • Little difference between this element and xs:documentation. • Should contain application specific extension information related to a particular schema element. • For example XML text could be added to help an application generate a tool tip. See example on bottom of 259.
Element Declarations • xs:element is widely used schema declaration • <xs:element name=”fullName” type=“xs:string”> indicates that the document will contain an element named name that is of type string and is named “fullName”
Referencing Predefined Elements • Global elements make a schema easier to read. • You can define elements after the annotation. • When the element is going to be used, add the ref attribute to link to the predefined element<xs:element name=“brand” type=“xs:string” />…<xs:complexType> <xs:sequence>-Indicates element order <xs:element ref=“brand” minOccurs=“1” />
Scope • You can define several different elements with the same name as long as they are used in different contexts. • This is another benefit of using Predefined elements. You only have to define the element once and then you can reuse.
Simple Types • Simple content equates with basic data types such as String, integer, etc. • Simple types cannot contain nested content. • See table 16-1 on 260-261 for a list of built in simple schema types. • Attributes can NEVER contain elements so they will always use simple types. • An element that is declared as a simple type CANNOT have any attributes. • The default simple type is “xs:anytype”
Attribute Declarations • Attributes are declared using the xs:attribute element. • May be declared globally or locally. • The code sample on the bottom of 261 shows the work required to define an element that contains an attribute. • the element contains: • <xs:complextype> - indicates the element is now complex. • <xs:simpleContent> - indicates the element’s simple content appears next. • <xs:extension base=“xs:string”>-indicates the data type of the simple content. • <xs:attribute name=“language” type=“xs:language”>-indicates the attribute’s name, and simple type. • use can be optional/required or prohibited, default is similar to #IMPLIED and fixed is similar to #FIXED
Attribute Groups • Similar to DTD’s parameter entities • Names group of xs:attribute declarations or references to other attribute groups. • Can be referenced from complex type definitions. • Must be declared as a global element with a unique name attribute. • Referenced within a complex type by including another xs:attributeGroup element with a ref attribute that matches the group name. • The example on 262 shows how attribute groups can be reused.
Element Groups • You can define a group of elements using <xs:group name=“thegroup”> <xs:sequence> …….. • <xs:group ref=“thegroup” • Groups can be used within choices to require one group or the other.
Complex Types • Schemas assign types to each declared element and attribute. • Complex types may contain nested elements and attributes. • Only elements can have complex types. • When building large and complex schemas, data types will need to be shared among different elements. • Named types facilitate reuse. • If you surround the complex type’s elements with the <xs:all>empty element all of the elements are required. • If you surround the complex type’s elements with the <xs:choice> empty element, then only one of the elements can be included. • <xs:sequence> indicates that the elements should appear in the order in which they were defined. • If you set the xs:complexType mixed attribute to “false”, the complex type can only contain elements. setting it to true allows mixed content to be displayed.
Occurrence Constraints • You can explicitly set the minimum and maximum number of times an element may occur at a particular point in a document using minOccurs and maxOccurs attributes of xs:element element. • See example on middle of page 267. • Default value for both attributes is 1 • If you just set minOccurs to 0 and leave out maxOccurs, then the element may appear 0 or 1 times, similar to using the ? . • You can set maxOccurs to “unbounded”. This means the maximum is unlimited. Like * or + • You an set minOccurs and maxOccurs for xs:choice giving you increased power.
Named Types • The simplest way to create a new type is to start with a complex type and give it a name. • You then define an element of the type you just created. At this point you can also set other attributes such as minOccurs or maxOccurs. • <xs:complexType name=“myNamedType”>…..</xs:complexType> • <xs:element name=“item” type=“myNamedType”> • The new type is NOT prefixed with xs!
Restrictions • You can create a new type out of one that already exists. • You accomplish this by adding conditions to one of XML Schema’s built-in types. • <xs:simpleType name=“USCurrency”> <xs:restriction base=“xs:decimal”> <xs:fractionDigits value=“2” /> </xs:restriction></xs:simpleType> • <xs:element name=“lastPrice” type=“USCurrency” />
Restrictions Part Two • The restriction element tells the processor that we’re creating a subset of an existing base type, decimal • The subtype in the example must have two digits after the decimal point. • Simple types can be restricted on the basis of one or more facets (requirements)
Restrictions Part Three • minInclusive – Minimum value • maxInclusive – Maximum value • minExclusive– Number must be one higher than the value • maxExclusive—Number must be one lower than the value • totalDigits—Required number of digits including before and after the decimal point. • fractionDigits – Required number of digits after the decimal point • length – Refers to total characters or total items in a list • minlength/maxlength • pattern -- regular expression • enumeration – Set of potential values • whiteSpace – set to preserve, data is untouched, set to replace tabs line feeds and carriage returns are replaced with spaces, set to collapse, multiple spaces are replaced with single space.
Enumeration • You can utilize facets by creating an enumerated type to restrict choices for an element or attribute. • <xs:attribute name=“demand”> <xs:simpleType> <xs:restriction base=“xs:string”> <xs:enumeration value=“low” /> <xs:enumeration value=“high” />……..
Lists • It’s similar to an enumeration, however it allows multiple values separated by spaces • Using the demand attribute that contains the restricted enumerations: • <xs:simpleType name=“demandList” <xs:list itemType=“demand” /></xs:simpleType> • <xs:simpleType name=“theDemands” <xs:restriction base=“demandList”> <xs:maxLength value=“2” /> </xs:restriction></xs:simpleType> • INSIDE OF XML DOCUMENT:<theDemands> low high</theDemands>
Adding Attributes to Simple Types by Extension • It is difficult to include elements in a schema that have both attributes and text. • You can’t specify the text type in a simple element, because elements with attributes aren’t simple. • If you define element as complex, you can add attributes easily, but the only way to add content is to add a child element. • You get around the problem by extending the simple type and adding attributes to its model.
Extending a Simple Type: Example • <xs:element name=“originalOwner> <xs:complexType><xs:simpleContent><xs:extension base=“xs:string”> <xs:attribute name=“confirmed” default=“no” /> </xs:extension></xs:simpleContent></xs:complexType></xs:element> • Because it has an attribute, you must declare originalOwner as a complex type. However the xs:simpleContent tag indicates that the element should contain text and no child elements • SimpleContent derives from the base type String. In our example it’s extended to include the confirmed attribute.