Nothing Special   »   [go: up one dir, main page]

XML Schema Ket

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 28

XML schema

XML Schema is an XML-based alternative to DTDs.


An XML Schema describes the structure of an XML document.
The XML Schema language is also referred to as
XML Schema Definition (XSD).
What is an XML Schema?

• The purpose of an XML Schema is to define the legal building blocks of an XML
document, just like a DTD.
• An XML Schema:
• defines elements that can appear in a document
• defines attributes that can appear in a document
• defines which elements are child elements
• defines the order of child elements
• defines the number of child elements
• defines whether an element is empty or can include text
• defines data types for elements and attributes
• defines default and fixed values for elements and attributes
XML Schemas are the Successors of DTDs

• We think that very soon XML Schemas will be used in most Web applications as a
replacement for DTDs. Here are some reasons:
• XML Schemas are extensible to future additions
• XML Schemas are richer and more powerful than DTDs
• XML Schemas are written in XML
• XML Schemas support data types
• XML Schemas support namespaces
• XML Schema is a W3C Recommendation
• XML Schema became a W3C Recommendation 02. May 2001
XML Schemas Support Data Types

• With support for data types:


• It is easier to describe allowable document content
• It is easier to validate the correctness of data
• It is easier to work with data from a database
• It 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
XML Schemas use XML Syntax

• Some benefits of that XML Schemas are written in XML:


• You don't have to learn a new language
• You can use your XML editor to edit your Schema files
• You can use your XML parser to parse your Schema files
• You can manipulate your Schema with the XML DOM
• You can transform your Schema with XSLT
XML Schemas Secure Data Communication

• When sending data from a sender to a receiver, it is essential that both parts have
the same "expectations" about the content.
• With XML Schemas, the sender can describe the data in a way that the receiver
will understand.
• A date like: "03-11-2004" will, in some countries, be interpreted as 3.November
and in other countries as 11.March.
• However, an XML element with a data type like this:
• <date type="date">2004-03-11</date>
• ensures a mutual understanding of the content, because the XML data type "date"
requires the format "YYYY-MM-DD".
XML Schemas are Extensible

• XML Schemas are extensible, because they are written in XML.


• With an extensible Schema definition you can:
• Reuse your Schema in other Schemas
• Create your own data types derived from the standard types
• Reference multiple schemas in the same document
Well-Formed is not Enough

• A well-formed XML document is a document that conforms to the XML syntax rules,
like:
• it must begin with the XML declaration
• it must have one unique root element
• start-tags must have matching end-tags
• elements are case sensitive
• all elements must be closed
• all elements must be properly nested
• all attribute values must be quoted
• entities must be used for special characters
• Even if documents are well-formed they can still contain errors, and those errors can
have serious consequences.
• Think of the following situation: you order 5 gross of laser printers, instead of 5 laser
printers. With XML Schemas, most of these errors can be caught by your validating
software.
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

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

• <?xml version="1.0"?>
• <note xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com note.xsd">
<to>Tove</to>
<from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this
weekend!</body>
</note>
The <schema> element is the root element of every XML Schema.

• xmlns:xs="http://www.w3.org/2001/XMLSchema"
– indicates that the elements and data types used in the schema come from the
"http://www.w3.org/2001/XMLSchema" namespace. It also specifies that the
elements and data types that come from the
"http://www.w3.org/2001/XMLSchema" namespace should be prefixed with
xs:
targetNamespace="http://www.w3schools.com"

• indicates that the elements defined by this schema (note, to, from, heading, body.)
come from the "http://www.w3schools.com" namespace.
xmlns="http://www.w3schools.com"

• indicates that the default namespace is "http://www.w3schools.com".


elementFormDefault="qualified"

• indicates that any elements used by the XML instance document which were
declared in this schema must be namespace qualified.
What is a Simple Element?

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

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

• You can also add restrictions (facets) to a data type in order to limit its content, or
you can require the data to match a specific pattern.
• <lastname>Refsnes</lastname> <age>36</age> <dateborn>1970-03-
27</dateborn>

• <xs:element name="lastname" type="xs:string"/> <xs:element name="age"


type="xs:integer"/> <xs:element name="dateborn" type="xs:date"/>
• Simple elements may have a default value OR a fixed value specified.
• A default value is automatically assigned to the element when no other value is
specified.
• In the following example the default value is "red":
• <xs:element name="color" type="xs:string" default="red"/>A fixed value is also
automatically assigned to the element, and you cannot specify another value.
• In the following example the fixed value is "red":
• <xs:element name="color" type="xs:string" fixed="red"/>
What 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 is always declared as a simple type.
How to Define an Attribute?

• <xs:attribute name="xxx" type="yyy"/>


where xxx is the name of the attribute and yyy specifies the data type of the
attribute. XML Schema has a lot of built-in data types. The most common types
are:
• xs:string
• xs:decimal
• xs:integer
• xs:boolean
• xs:date
• xs:time
• <lastname lang="EN">Smith</lastname>And here is the corresponding attribute
definition:
• <xs:attribute name="lang" type="xs:string"/>
Default and Fixed Values for Attributes

• A default value is automatically assigned to the attribute when no other value is


specified
• A fixed value is also automatically assigned to the attribute, and you cannot specify
another value.
Optional and Required Attributes

• Attributes are optional by default. To specify that the attribute is required,


use the "use" attribute:
• <xs:attribute name="lang" type="xs:string" use="required"/>
• When an XML element or attribute has a data type defined, it puts restrictions on
the element's or attribute's content.
• If an XML element is of type "xs:date" and contains a string like "Hello World",
the element will not validate.
• With XML Schemas, you can also add your own restrictions to your XML elements
and attributes. These restrictions are called facets. You can read more about facets
in the next chapter.
• Restrictions are used to define
acceptable values for XML elements or
attributes. Restrictions on XML
elements are called facets.
Restrictions on Values
• <xs:element
name="age"><xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType></xs:element>
Restrictions on a Set of Values
• To limit the content of an XML element to
a set of acceptable values, we would use
the enumeration constraint.
• The example below defines an element
called "car" with a restriction. The only
acceptable values are: Audi, Golf, BMW:
• <xs:element
name="car"><xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType></xs:element>

You might also like