Title: INF 523 Web Technologies with XML
1INF 523 Web Technologies (with XML)
- Week 4
- Bahadir K. Akcam
- Fall 2006
2Week 3
- Namespaces
- XML Schema (XSD)
3XSD How To?
- lt?xml version"1.0"?gt
- ltnote xmlns"http//www.albany.edu"
xmlnsxsi"http//www.w3.org/2001/XMLSchema-instan
ce" xsischemaLocation"http//www.albany.edu
note.xsd"gt - lttogtTovelt/togt
- ltfromgtJanilt/fromgt
- ltheadinggtReminderlt/headinggt
- ltbodygtDon't forget me this weekend!lt/bodygt
- lt/notegt
4XSD How To?
- An XML Schema called note.xsd
- lt?xml version"1.0"?gt
- ltxsschema xmlnsxs"http//www.w3.org/2001/XMLSch
ema" targetNamespace"http//www.albany.edu"
xmlns"http//www.albany.edu"
elementFormDefault"qualified"gt - ltxselement name"note"gt
- ltxscomplexTypegt
- ltxssequencegt
- ltxselement name"to" type"xsstring"/gt
- ltxselement name"from" type"xsstring"/gt
ltxselement name"heading" type"xsstring"/gt
- ltxselement name"body" type"xsstring"/gt
- lt/xssequencegt
- lt/xscomplexTypegt
- lt/xselementgt
- lt/xsschemagt
5 Simple Element
- XML Schemas define the elements of your XML
files. - A simple element is an XML element that contains
only text. It cannot contain any other elements
or attributes. - 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.
6Simple Element
- Syntax
- ltxselement name"xxx" type"yyy"/gt
- Built in data types
- xsstring
- xsdecimal
- xsinteger
- xsboolean
- xsdate
- xstime
7Simple Element
- XML
- ltlastnamegtRefsneslt/lastnamegt
- ltagegt36lt/agegt
- ltdateborngt1970-03-27lt/dateborngt
- XSD
- ltxselement name"lastname" type"xsstring"/gt
- ltxselement name"age" type"xsinteger"/gt
- ltxselement name"dateborn" type"xsdate"/gt
8Default and Fixed Values for Simple Elements
- 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. - ltxselement name"color" type"xsstring"
default"red"/gt - A fixed value is also automatically assigned to
the element, and you cannot specify another
value. - ltxselement name"color" type"xsstring"
fixed"red"/gt
9XSD Attributes
- All attributes are declared as simple types.
- 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. - Syntax
- ltxsattribute name"xxx" type"yyy"/gt
- ltlastname lang"EN"gtSmithlt/lastnamegt
- ltxsattribute name"lang" type"xsstring"/gt
10XSD Attributes
- Default and Fixed Values for Attributes
- ltxsattribute name"lang" type"xsstring"
default"EN"/gt - ltxsattribute name"lang" type"xsstring"
fixed"EN"/gt - Optional and Required Attributes
- Attributes are optional by default. To specify
that the attribute is required, use the "use"
attribute - ltxsattribute name"lang" type"xsstring"
use"required"/gt
11Restrictions on Content
- 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 "xsdate" 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.
12XSD Restrictions/Facets
- Restrictions are used to define acceptable values
for XML elements or attributes. Restrictions on
XML elements are called facets.
13Restrictions on Values
- The following example defines an element called
"age" with a restriction. The value of age cannot
be lower than 0 or greater than 120 - ltxselement name"age"gt
- ltxssimpleTypegt
- ltxsrestriction base"xsinteger"gt
- ltxsminInclusive value"0"/gt
- ltxsmaxInclusive value"120"/gt
- lt/xsrestrictiongt
- lt/xssimpleTypegt
- lt/xselementgt
14Restrictions 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 - ltxselement name"car"gtltxssimpleTypegt
ltxsrestriction base"xsstring"gt
ltxsenumeration value"Audi"/gt
ltxsenumeration value"Golf"/gt
ltxsenumeration value"BMW"/gt
lt/xsrestrictiongt lt/xssimpleTypegtlt/xselementgt
15Restrictions on a Series of Values
- To limit the content of an XML element to define
a series of numbers or letters that can be used,
we would use the pattern constraint.
16Restrictions on a Series of Values
- The example below defines an element called
"letter" with a restriction. The only acceptable
value is ONE of the LOWERCASE letters from a to
z - ltxselement name"letter"gt
- ltxssimpleTypegt
- ltxsrestriction base"xsstring"gt
- ltxspattern value"a-z"/gt
- lt/xsrestrictiongt
- lt/xssimpleTypegt
- lt/xselementgt
17Restrictions on a Series of Values
- ltxselement name"initials"gt
- ltxssimpleTypegt
- ltxsrestriction base"xsstring"gt
- ltxspattern value"A-ZA-ZA-Z"/gt
- lt/xsrestrictiongt
- lt/xssimpleTypegt
- lt/xselementgt
- Defines an element called "initials" with a
restriction. The only acceptable value is THREE
of the UPPERCASE letters from a to z
18Restrictions on a Series of Values
- ltxspattern value"a-zA-Za-zA-Za-zA-Z"/gt
- Defines an element called "initials" with a
restriction. The only acceptable value is THREE
of the LOWERCASE OR UPPERCASE letters from a to z - ltxspattern value"xyz"/gt
- The only acceptable value is ONE of the following
letters x, y, OR z
19Restrictions on a Series of Values
- ltxspattern value"0-90-90-90-90-9"/gt
- The only acceptable value is FIVE digits in a
sequence, and each digit must be in a range from
0 to 9 - The acceptable value is zero or more occurrences
of lowercase letters from a to z - ltxspattern value"(a-z)"/gt
20Restrictions on a Series of Values
- The acceptable value is one or more pairs of
letters, each pair consisting of a lower case
letter followed by an upper case letter. For
example, "sToP" will be validated by this
pattern, but not "Stop" or "STOP" or "stop" - ltxspattern value"(a-zA-Z)"/gt
- ltxspattern value"malefemale"/gt
- The only acceptable value is male OR female
21Restrictions on a Series of Values
- There must be exactly eight characters in a row
and those characters must be lowercase or
uppercase letters from a to z, or a number from 0
to 9 - ltxspattern value"a-zA-Z0-98"/gt
22Restrictions on Whitespace Characters
- To specify how whitespace characters should be
handled, we would use the whiteSpace constraint. - The whiteSpace constraint is set to "preserve",
which means that the XML processor WILL NOT
remove any white space characters - ltxswhiteSpace value"preserve"/gt
- The whiteSpace constraint is set to "replace",
which means that the XML processor WILL REPLACE
all white space characters (line feeds, tabs,
spaces, and carriage returns) with spaces - ltxswhiteSpace value"replace"/gt
- The whiteSpace constraint is set to "collapse",
which means that the XML processor WILL REMOVE
all white space characters (line feeds, tabs,
spaces, carriage returns are replaced with
spaces, leading and trailing spaces are removed,
and multiple spaces are reduced to a single
space) - ltxswhiteSpace value"collapse"/gt
23Restrictions on Length
- To limit the length of a value in an element, we
would use the length, maxLength, and minLength
constraints. - ltxselement name"password"gt
- ltxssimpleTypegt
- ltxsrestriction base"xsstring"gt
- ltxsminLength value"5"/gt
- ltxsmaxLength value"8"/gt
- lt/xsrestrictiongt
- lt/xssimpleTypegtlt/xselementgt
- ltxslength value"8"/gt
- Must be exactly eight characters
24More Restrictions
- Restrictions for Datatypes
- Check end of the http//www.w3schools.com/schema/s
chema_facets.asp
25XSD Complex Elements
- A complex element contains other elements and/or
attributes. - There are four kinds of complex elements
- empty elements
- elements that contain only other elements
- elements that contain only text
- elements that contain both other elements and
text
26How to Define a Complex Element
- (1st way)
- ltemployeegt ltfirstnamegtJohnlt/firstnamegt
ltlastnamegtSmithlt/lastnamegt lt/employeegt - ltxselement name"employee"gt ltxscomplexTypegt
ltxssequencegt ltxselement name"firstname"
type"xsstring"/gt ltxselement name"lastname"
type"xsstring"/gt lt/xssequencegt
lt/xscomplexTypegt lt/xselementgt
27How to Define a Complex Element
- (2nd way)
- ltxselement name"employee" type"personinfo"/gtlt
xscomplexType name"personinfo"gt ltxssequencegt
ltxselement name"firstname" type"xsstring"/gt
ltxselement name"lastname" type"xsstring"/gt
lt/xssequencegt lt/xscomplexTypegt
28How to Define a Complex Element
- If you use the method described above, several
elements can refer to the same complex type, like
this - ltxselement name"employee" type"personinfo"/gt
ltxselement name"student" type"personinfo"/gt
ltxselement name"member" type"personinfo"/gtltxs
complexType name"personinfo"gt ltxssequencegt
ltxselement name"firstname" type"xsstring"/gt
ltxselement name"lastname" type"xsstring"/gt
lt/xssequencegt lt/xscomplexTypegt
29XSD Complex Empty Elements
- An empty complex element cannot have contents,
only attributes. - ltproduct prodid"1345" /gt
- 3 different ways
- ltxselement name"product"gt ltxscomplexTypegt
ltxsattribute name"prodid" type"xsposi
tiveInteger"/gt lt/xscomplexTypegt lt/xselementgt
30XSD Complex Elements Text Only Elements
- A complex text-only element can contain text and
attributes. - This type contains only simple content (text and
attributes), therefore we add a simpleContent
element around the content. - When using simple content, you must define an
extension OR a restriction within the
simpleContent element.
31XSD Complex Elements Text Only Elements
- ltxselement name"somename"gt ltxscomplexTypegt
ltxssimpleContentgt ltxsextension
base"basetype"gt .... .... lt/xsextensiongt
lt/xssimpleContentgt lt/xscomplexTypegt
lt/xselementgt - ltxselement name"somename"gt ltxscomplexTypegt
ltxssimpleContentgt ltxsrestriction
base"basetype"gt .... .... lt/xsrestrictiongt
lt/xssimpleContentgt lt/xscomplexTypegt
lt/xselementgt
32Example
- ltshoesize country"france"gt35lt/shoesizegt
- ltxselement name"shoesize"gt ltxscomplexTypegt
ltxssimpleContentgt ltxsextension
base"xsinteger"gt ltxsattribute
name"country" type"xsstring" /gt
lt/xsextensiongt lt/xssimpleContentgt
lt/xscomplexTypegt lt/xselementgt
33XSD Complex Types With Mixed Content
- A mixed complex type element can contain
attributes, elements, and text. - ltlettergt Dear Mr.ltnamegtJohn Smithlt/namegt. Your
order ltorderidgt1032lt/orderidgt will be shipped on
ltshipdategt2001-07-13lt/shipdategt. lt/lettergt - ltxselement name"letter"gt ltxscomplexType
mixed"true"gt ltxssequencegt ltxselement
name"name" type"xsstring"/gt ltxselement
name"orderid" type"xspositiveInteger"/gt
ltxselement name"shipdate" type"xsdate"/gt
lt/xssequencegt lt/xscomplexTypegt lt/xselementgt
34XSD Complex Types Indicators
- We can control HOW elements are to be used in
documents with indicators. - Indicators There are seven indicators
- Order indicators
- All
- Choice
- Sequence
- Occurrence indicators
- maxOccurs
- minOccurs
- Group indicators
- Group name
- attributeGroup name
35Order Indicators
- Order indicators are used to define the order of
the elements. - All Indicator
- The ltallgt indicator specifies that the child
elements can appear in any order, and that each
child element must occur only once - ltxselement name"person"gt ltxscomplexTypegt
ltxsallgt ltxselement name"firstname"
type"xsstring"/gt ltxselement name"lastname"
type"xsstring"/gt lt/xsallgt lt/xscomplexTypegt
lt/xselementgt
36Choice Indicator
- The ltchoicegt indicator specifies that either one
child element or another can occur - ltxselement name"person"gt ltxscomplexTypegt
ltxschoicegt ltxselement name"employee"
type"employee"/gt ltxselement name"member"
type"member"/gt lt/xschoicegt lt/xscomplexTypegt
lt/xselementgt
37Sequence Indicator
- The ltsequencegt indicator specifies that the child
elements must appear in a specific order - ltxselement name"person"gt ltxscomplexTypegt
ltxssequencegt ltxselement name"firstname"
type"xsstring"/gt ltxselement name"lastname"
type"xsstring"/gt lt/xssequencegt
lt/xscomplexTypegt lt/xselementgt
38Occurrence Indicators
- Occurrence indicators are used to define how
often an element can occur. - For all "Order" and "Group" indicators (any, all,
choice, sequence, group name, and group
reference) the default value for maxOccurs and
minOccurs is 1. - maxOccurs Indicator
- The ltmaxOccursgt indicator specifies the maximum
number of times an element can occur - To allow an element to appear an unlimited number
of times, use the maxOccurs"unbounded" statement - minOccurs Indicator
- The ltminOccursgt indicator specifies the minimum
number of times an element can occur
39Example
- ltxselement name"person"gt ltxscomplexTypegt
ltxssequencegt ltxselement name"full_name"
type"xsstring"/gt ltxselement
name"child_name" type"xsstring"
maxOccurs"10" minOccurs"0"/gt
lt/xssequencegt lt/xscomplexTypegt
lt/xselementgt
40Example XML File Myfamily.xml
- lt?xml version"1.0" encoding"ISO-8859-1"?gt
- ltpersons xmlnsxsi"http//www.w3.org/2001/XMLSche
ma-instance" xsinoNamespaceSchemaLocation"family
.xsd"gt - ltpersongt
- ltfull_namegtHege Refsneslt/full_namegt
ltchild_namegtCecilielt/child_namegt - lt/persongt
- ltpersongt
- ltfull_namegtTove Refsneslt/full_namegt
ltchild_namegtHegelt/child_namegt ltchild_namegtStalelt/c
hild_namegt - ltchild_namegtJimlt/child_namegt
- ltchild_namegtBorgelt/child_namegt
- lt/persongt
- ltpersongt
- ltfull_namegtStale Refsneslt/full_namegt
- lt/persongt
- lt/personsgt
41Example XSD Schema family.xsd
- lt?xml version"1.0" encoding"ISO-8859-1"?gt
- ltxsschema xmlnsxs"http//www.w3.org/2001/XMLSch
ema" elementFormDefault"qualified"gt - ltxselement name"persons"gt
- ltxscomplexTypegt
- ltxssequencegt
- ltxselement name"person" maxOccurs"unbounded"gt
- ltxscomplexTypegt
- ltxssequencegt
- ltxselement name"full_name" type"xsstring"/gt
- ltxselement name"child_name" type"xsstring"
minOccurs"0" maxOccurs"5"/gt - lt/xssequencegt
- lt/xscomplexTypegt
- lt/xselementgt
- lt/xssequencegt
- lt/xscomplexTypegt
- lt/xselementgt
- lt/xsschemagt
42Group Indicators
- Group indicators are used to define related sets
of elements. - Element Groups
- ltxsgroup name"groupname"gt ... lt/xsgroupgt
- You must define an all, choice, or sequence
element inside the group declaration.
43Group Indicators
- ltxsgroup name"persongroup"gt
- ltxssequencegt
- ltxselement name"firstname" type"xsstring"/gt
- ltxselement name"lastname" type"xsstring"/gt
- ltxselement name"birthday" type"xsdate"/gt
- lt/xssequencegt
- lt/xsgroupgt
- ltxselement name"person" type"personinfo"/gt
- ltxscomplexType name"personinfo"gt
- ltxssequencegt
- ltxsgroup ref"persongroup"/gt
- ltxselement name"country" type"xsstring"/gt
- lt/xssequencegt
- lt/xscomplexTypegt
44Attribute Groups
- Attribute groups are defined with the
attributeGroup declaration - ltxsattributeGroup name"groupname"gt ...
lt/xsattributeGroupgt
45Attribute Groups
- ltxsattributeGroup name"personattrgroup"gt
ltxsattribute name"firstname"
type"xsstring"/gt ltxsattribute name"lastname"
type"xsstring"/gt ltxsattribute name"birthday"
type"xsdate"/gt - lt/xsattributeGroupgt
- ltxselement name"person"gt ltxscomplexTypegt
ltxsattributeGroup ref"personattrgroup"/gt
lt/xscomplexTypegt - lt/xselementgt
46XSD The ltanygt Element
- The ltanygt element enables us to extend the XML
document with elements not specified by the
schema! - By using the ltanygt element we can extend (after
ltlastnamegt) the content of "person" with any
element - We have family.xsd, children.xsd, myfamily.xml
- family.xsd
- ltxselement name"person"gt ltxscomplexTypegt
ltxssequencegt ltxselement name"firstname"
type"xsstring"/gt ltxselement name"lastname"
type"xsstring"/gt ltxsany minOccurs"0"/gt
lt/xssequencegt lt/xscomplexTypegt lt/xselementgt
47XSD The ltanygt Element
- Now we want to extend the "person" element with a
"children" element. - A new XSD document children.xsd
- lt?xml version"1.0" encoding"ISO-8859-1"?gt
ltxsschema xmlnsxs"http//www.w3.org/2001/XMLSch
ema" targetNamespace"http//www.w3schools.com"
xmlns"http//www.w3schools.com"
elementFormDefault"qualified"gtltxselement
name"children"gt ltxscomplexTypegt
ltxssequencegt ltxselement name"childname"
type"xsstring" maxOccurs"unbounded"/gt
lt/xssequencegt lt/xscomplexTypegt
lt/xselementgtlt/xsschemagt
48XSD The ltanygt Element
- Myfamily.xml
- lt?xml version"1.0" encoding"ISO-8859-1"?gtltperso
ns xmlns"http//www.microsoft.com"
xmlnsxsi"http//www.w3.org/2001/XMLSchema-instan
ce" xsiSchemaLocation"http//www.microsoft.com
family.xsd http//www.w3schools.com
children.xsd"gtltpersongt ltfirstnamegtHegelt/firstna
megt ltlastnamegtRefsneslt/lastnamegt ltchildrengt
ltchildnamegtCecilielt/childnamegt lt/childrengt
lt/persongtltpersongt ltfirstnamegtStalelt/firstnamegt
ltlastnamegtRefsneslt/lastnamegt
lt/persongtlt/personsgt
49XSD The ltanyAttributegt Element
- The ltanyAttributegt element enables us to extend
the XML document with attributes not specified by
the schema. - Works similar to ltAnygt element
- family.xsd
- ltxselement name"person"gt ltxscomplexTypegt
ltxssequencegt ltxselement name"firstname"
type"xsstring"/gt ltxselement name"lastname"
type"xsstring"/gt lt/xssequencegt
ltxsanyAttribute/gt lt/xscomplexTypegt
lt/xselementgt
50XSD The ltanyAttributegt Element
- We want to extend the "person" element with a
"gender" attribute - attribute.xsd
- lt?xml version"1.0" encoding"ISO-8859-1"?gt
ltxsschema xmlnsxs"http//www.w3.org/2001/XMLSch
ema" targetNamespace"http//www.w3schools.com"
xmlns"http//www.w3schools.com"
elementFormDefault"qualified"gtltxsattribute
name"gender"gt ltxssimpleTypegt
ltxsrestriction base"xsstring"gt
ltxspattern value"malefemale"/gt
lt/xsrestrictiongt lt/xssimpleTypegt
lt/xsattributegtlt/xsschemagt
51XSD The ltanyAttributegt Element
- Myfamily.xml
- lt?xml version"1.0" encoding"ISO-8859-1"?gtltperson
s xmlns"http//www.microsoft.com"
xmlnsxsi"http//www.w3.org/2001/XMLSchema-instan
ce" xsiSchemaLocation"http//www.microsoft.com
family.xsd http//www.w3schools.com
attribute.xsd"gtltperson gender"female"gt
ltfirstnamegtHegelt/firstnamegt ltlastnamegtRefsneslt/l
astnamegt lt/persongtltperson gender"male"gt
ltfirstnamegtStalelt/firstnamegt ltlastnamegtRefsneslt
/lastnamegt lt/persongtlt/personsgt
52XSD Element Substitution
- With XML Schemas, one element can substitute
another element. - Element Substitution Choose Norwegian or English
- ltxselement name"name" type"xsstring"/gt
ltxselement name"navn" substitutionGroup"name"/gt
ltxscomplexType name"custinfo"gt
ltxssequencegt ltxselement ref"name"/gt
lt/xssequencegt lt/xscomplexTypegtltxselement
name"customer" type"custinfo"/gt ltxselement
name"kunde" substitutionGroup"customer"/gt
53XSD Element Substitution
- ltcustomergt ltnamegtJohn Smithlt/namegt lt/customergt
- ltkundegt ltnavngtJohn Smithlt/navngt lt/kundegt
- Block Element Substitution
- ltxselement name"name" type"xsstring"
block"substitution"/gt
54Assignment
- Due on November 20
- Developing an XSD document for your project
- Define your elements format
- Put restrictions on value
- Mininclusive, maxinclusive
- Limit the content Enumeration
- Restrictions on a series of values
- Use default or fixed values
- Be cautious about using the right formats such as
age element should be positive integer. - Use indicators
- Sequence, all, choice (at least one of them)
- Minoccurs, maxoccurs
- Use group indicators