XML Schema Ket

download XML Schema Ket

of 28

Transcript of XML Schema Ket

  • 8/9/2019 XML Schema Ket

    1/28

    XML schema

    XML Schema is an XML-based alternative to DTDs.An XML Schema describes the structure of an XMLdocument.The XML Schema language is also referred to as

    XML Schema Definition (XSD).

  • 8/9/2019 XML Schema Ket

    2/28

    W hat is an XML Schema?

    The purpose of an XML Schema is to define the legal building blocksof an XML document, just like a DTD.An XML Schema:defines elements that can appear in a documentdefines attributes that can appear in a document

    defines which elements are child elementsdefines the order of child elementsdefines the number of child elementsdefines whether an element is empty or can include textdefines data types for elements and attributesdefines default and fixed values for elements and attributes

  • 8/9/2019 XML Schema Ket

    3/28

    XML Schemas are the Successors of DTDs

    W e think that very soon XML Schemas will be used in most W ebapplications as a replacement for DTDs. Here are some reasons:XML Schemas are extensible to future additionsXML Schemas are richer and more powerful than DTDs

    XML Schemas are written in XMLXML Schemas support data typesXML Schemas support namespaces

  • 8/9/2019 XML Schema Ket

    4/28

    XML Schema is a W3C RecommendationXML Schema became a W3C Recommendation 02. May 2001

  • 8/9/2019 XML Schema Ket

    5/28

    XML Schemas Support Data Types

    W ith support for data types:It is easier to describe allowable document contentIt is easier to validate the correctness of dataIt is easier to work with data from a databaseIt is easier to define data facets (restrictions on data)It is easier to define data patterns (data formats)It is easier to convert data between different data types

  • 8/9/2019 XML Schema Ket

    6/28

    XML Schemas use XML Syntax

    Some benefits of that XML Schemas are written in XML:You don't have to learn a new languageYou can use your XML editor to edit your Schema filesYou can use your XML parser to parse your Schema filesYou can manipulate your Schema with the XML DOMYou can transform your Schema with XSLT

  • 8/9/2019 XML Schema Ket

    7/28

    XML Schemas Secure Data C ommunication

    W hen sending data from a sender to a receiver, it is essential thatboth parts have the same "expectations" about the content.W ith XML Schemas, the sender can describe the data in a way thatthe receiver will understand.A date like: "0 3 -11-2004" will, in some countries, be interpreted as3 .November and in other countries as 11.March.However, an XML element with a data type like this:2004-0 3 -11ensures a mutual understanding of the content, because the XMLdata type "date" requires the format "YYYY-MM-DD".

  • 8/9/2019 XML Schema Ket

    8/28

    XML Schemas are Extensible

    XML Schemas are extensible, because they are written in XML.W ith an extensible Schema definition you can:Reuse your Schema in other SchemasC reate your own data types derived from the standard typesReference multiple schemas in the same document

  • 8/9/2019 XML Schema Ket

    9/28

    W ell-Formed is not Enough

    A well-formed XML document is a document that conforms to the XMLsyntax rules, like:it must begin with the XML declarationit must have one unique root elementstart-tags must have matching end-tagselements are case sensitiveall elements must be closedall elements must be properly nestedall attribute values must be quotedentities must be used for special charactersEven if documents are well-formed they can still contain errors, and thoseerrors can have serious consequences.Think of the following situation: you order 5 gross of laser printers, instead

    of 5 laser printers.W

    ith XML Schemas, most of these errors can be caughtby your validating software.

  • 8/9/2019 XML Schema Ket

    10/28

    The note element is a complex type because it contains other elements.The other elements (to, from, heading, body) are simple types becausethey do not contain other elements.

  • 8/9/2019 XML Schema Ket

    11/28

    useit

    ToveJani Reminder Don't

    forget me this weekend!

  • 8/9/2019 XML Schema Ket

    12/28

    The element is the root element of every XML Schema.

    xmlns:xs="http://www.w 3 .org/2001/XMLSchema" indicates that the elements and data types used in the schema

    come from the "http://www.w 3 .org/2001/XMLSchema"namespace. It also specifies that the elements and data typesthat come from the "http://www.w 3 .org/2001/XMLSchema"namespace should be prefixed with xs:

  • 8/9/2019 XML Schema Ket

    13/28

    targetNamespace="http://www.w 3 schools.com"

    indicates that the elements defined by this schema (note, to, from,heading, body.) come from the "http://www.w 3 schools.com"namespace.

  • 8/9/2019 XML Schema Ket

    14/28

    xmlns="http://www.w 3 schools.com"

    indicates that the default namespace is"http://www.w 3 schools.com".

  • 8/9/2019 XML Schema Ket

    15/28

    elementFormDefault="qualified"

    indicates that any elements used by the XML instance documentwhich were declared in this schema must be namespace qualified.

  • 8/9/2019 XML Schema Ket

    16/28

    W hat is a Simple Element?

    A simple element is an XML element that can contain only text. Itcannot contain any other elements or attributes.

    However, the "only text" restriction is quite misleading. The text can beof many different types. It can be one of the types included in theXML Schema definition (boolean, string, date, etc.), or it can be acustom type that you can define yourself.

    You can also add restrictions (facets) to a data type in order to limitits content, or you can require the data to match a specific pattern.

  • 8/9/2019 XML Schema Ket

    17/28

    Refsnes 36 1970-03 -27

  • 8/9/2019 XML Schema Ket

    18/28

    Simple elements may have a default value OR a fixed valuespecified.A default value is automatically assigned to the element when no

    other value is specified.In the following example the default value is "red":A fixedvalue is also automatically assigned to the element, and you cannotspecify another value.In the following example the fixed value is "red":

  • 8/9/2019 XML Schema Ket

    19/28

    W hat is an Attribute?

    Simple elements cannot have attributes. If an element has attributes,it is considered to be of a complex type. But the attribute itself isalways declared as a simple type.

  • 8/9/2019 XML Schema Ket

    20/28

    How to Define an Attribute?

    where xxx is the name of the attribute and yyy specifies the datatype of the attribute. XML Schema has a lot of built-in data types. Themost common types are:xs:stringxs:decimalxs:integer xs:booleanxs:datexs:time

  • 8/9/2019 XML Schema Ket

    21/28

    SmithAnd here is thecorresponding attribute definition:

  • 8/9/2019 XML Schema Ket

    22/28

    Default and Fixed Values for Attributes

    A default value is automatically assigned to the attribute when noother value is specifiedA fixed value is also automatically assigned to the attribute, and youcannot specify another value.

  • 8/9/2019 XML Schema Ket

    23/28

    Optional and Required Attributes

    Attributes are optional by default. To specify that the attributeis required, use the "use" attribute:

  • 8/9/2019 XML Schema Ket

    24/28

    W hen an XML element or attribute has a data type defined, it putsrestrictions on the element's or attribute's content.If an XML element is of type "xs:date" and contains a string like "HelloW orld", the element will not validate.W ith XML Schemas, you can also add your own restrictions to your

    XML elements and attributes. These restrictions are called facets. Youcan read more about facets in the next chapter.

  • 8/9/2019 XML Schema Ket

    25/28

    Restrictions are used to defineacceptable values for XML elements or attributes. Restrictions on XMLelements are called facets.

  • 8/9/2019 XML Schema Ket

    26/28

    Restrictions on Values

  • 8/9/2019 XML Schema Ket

    27/28

    R estrictions on a Set of Values

    To limit the content of an XML element toa set of acceptable values, we would usethe enumeration constraint.The example below defines an elementcalled "car" with a restriction. The onlyacceptable values are: Audi, Golf, BMW:

  • 8/9/2019 XML Schema Ket

    28/28