Академический Документы
Профессиональный Документы
Культура Документы
3 4
DWAX 2010.1 DWAX 2010.1
1
Declaring a Namespace Declaring a Namespace
A namespace is a defined collection of A namespace can be declared in the prolog or
as an element attribute. The syntax to declare a
element and attribute names.
namespace in the prolog is:
Declares a namespace with the prefix “pat” and An abstract resource is one that doesn’t have
the URI http://uhosp/patients/ns. any physical existence, the URI is used as an
identifier.
The URI is not a Web address. A URI identifies
a physical or an abstract resource. The URI http://uhosp/patients/ns is simply a text
identifier.
7 8
DWAX 2010.1 DWAX 2010.1
2
Applying a Namespace to an Element Applying a Namespace to an Element
Once it has been declared and its URI specified, Prefixed names are called qualified
the namespace is applied to elements and names and an element name without a
attributes by inserting the namespace prefix namespace prefix is called an unqualified
before each element name that belongs to the
namespace. name.
3
Declaring a Namespace as an Element Attribute Declaring a Namespace as an Element Attribute
…applies the namespace
http://uhosp/patients/ns namespace to the They are unqualified elements, though,
Patient element and all of its child because they lack a namespace prefix.
elements.
Declaring a namespace by adding it as an
While the “pat” prefix was only added to attribute of the document’s root element
the Patients element name, the XML places all elements in the namespace.
parser considers the other elements parts
of the Patients namespace and they inherit
the namespace.
13 14
DWAX 2010.1 DWAX 2010.1
15 16
DWAX 2010.1 DWAX 2010.1
4
DTD’s / Schemas
DTDs:
Schemas – Not flexible enough to meet certain
programming needs
These lecture powerpoint slides are mostly based on • Cannot be manipulated (searched, transformed,
W3Schools tutorial on XML Schema etc.)
http://www.w3schools.com/schema/default.asp • Not XML documents – Use EBNF grammar
• Cannot properly validate the content of an element
17 18
DWAX 2010.1 DWAX 2010.1
5
XML Schema Elements
Simple Types Simple Types
– Contains only data, no other elements and has no attributes A simple element can not contain other elements or attributes.
Complex Types The format for defining a simple element is:
– Contains other elements and/or attribute(s)
<xs:element name=”xxx” type=”yyy”/>
(xxx is the name of the element, yyy is the data type of the element)
<?xml version=”1.0”?>
<email>This is my schema test</email>
21 22
DWAX 2010.1 DWAX 2010.1
<?xml version="1.0" encoding="UTF-8"?> – Tells us that the elements and data types used in the
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="email" type="xs:string"/>
schema, such as schema, element, complexType,
</xs:schema> sequence etc. comes from the
"http://www.w3.org/2001/XMLSchema" namespace.
Schema document: – Specifies that the elements and data types from this
An XML Schema document always has <schema> as the root element
namespace should be prefixed with xsd
When using schema, namespaces must be declared in both the XML
document and the XML schema document.
23 24
DWAX 2010.1 DWAX 2010.1
6
Instance document: Complex Types:
When an XML file uses an XML schema to
define its valid format, it is said to be an instance An element that contains other elements
document of that schema. All instance and/or attributes
documents must use the
http://www.w3.org/2001/XMLSchema-instance Four kinds:
namespace. – empty elements
xsi:noNamespaceSchemaLocation = – element that contain only other elements
“emailTest.xsd”
– indicates where the schema document resides. This – element that contain only text
can be on the local machine, or anywhere on the
internet, in this case on the local machine.It also – element that contain both text and other
indicates that there are no target namespace and that elements
the prefix will not be used to indicate that an element
belongs to a namespace.
25 26
DWAX 2010.1 DWAX 2010.1
27 28
DWAX 2010.1 DWAX 2010.1
7
Attributes:
Elements with attributes are considered to be complex
Declaring attributes to be optional or required (optional by default)
The format for defining an attribute is: <xs:attribute name=”lang” type=”xs:string”
<xs:attribute name=”xxx” type=”yyy”/> use=”optional”/>
(xxx is the name of the attribute, yyy is the data type of the attribute) <xs:attribute name=”lang” type=”xs:string”
use=”required”/>
XML element with attribute:
<lastname lang=”EN”>Smith</lastname>
Attribute definition:
<xs:attribute name=”lang” type=”xs:string”/>
8
Mixed Content
<letter>
Complex element with attribute and child element: Dear Mr.<name>John Smith</name>.
Your order <orderid>1032</orderid>
<shoe country="france"> will be shipped on <shipdate>2001-07-13</shipdate>.
<size>10</size> </letter>
</shoe>
Schema for letter:
Schema declaration: <xs:element name="letter">
<xs:element name="shoe"> <xs:complexType mixed="true">
<xs:complexType> <xs:sequence>
<xs:sequence> <xs:element name="name" type="xs:string"/>
<xs:element name="size" type="xs:integer"/> <xs:element name="orderid"
</xs:sequence> type="xs:positiveInteger"/>
<xs:attribute name="country" type="xs:string"/> <xs:element name="shipdate" type="xs:date"/>
</xs:complexType> </xs:sequence>
</xs:element> </xs:complexType>
</xs:element>
33 34
DWAX 2010.1 DWAX 2010.1
35 36
DWAX 2010.1 DWAX 2010.1
9
Creating a new type by extending an existing complex type, adding in some
elements:
<xs:element name="employee" type="fullpersoninfo"/>
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
37 38
</xs:complexType>
DWAX 2010.1 DWAX 2010.1
Facets
(Restrictions on content)
Enumeration constraints:
Used to control acceptable values for XML elements or
attributes <xs:element name="car">
<xs:element name="age"> <xs:simpleType>
<xs:simpleType> <xs:restriction base="xs:string">
<xs:restriction base="xs:integer"> <xs:enumeration value="Audi"/>
<xs:minInclusive value="0"/> <xs:enumeration value="Golf"/>
<xs:maxInclusive value="100"/> <xs:enumeration value="BMW"/>
</xs:restriction> </xs:restriction>
</xs:simpleType> </xs:simpleType>
</xs:element> </xs:element>
Age can not be lower than 0 or greater than 100
39 40
DWAX 2010.1 DWAX 2010.1
10
<xs:restriction base="xs:string">
<xs:pattern value="([a-z])*"/>
Restricting a series of values: Acceptable value: Zero or more occurrences of lowercase letters from a to z
“*” Zero or more
“+” One or more
<xs:element name="letter">
<xs:simpleType> <xs:restriction base="xs:string">
<xs:restriction base="xs:string"> <xs:pattern value="male|female"/>
<xs:pattern value="[a-z]"/>
Acceptable value: male OR female
</xs:restriction>
</xs:simpleType> <xs:restriction base="xs:string">
</xs:element> <xs:pattern value="[a-zA-Z0-9]{8}"/>
Acceptable value: One lowercase letter from a to z Acceptable value: exactly 8 characters in a row, either uppercase or
lowercase a to z or number from 0 to 9
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/> <xs:restriction base="xs:string">
<xs:minLength value="5"/>
Acceptable value: Three uppercase or lowercase letters from A to Z
<xs:maxLength value="8"/>
Acceptable value: minimum 5, maximum 8 characters
41 42
DWAX 2010.1 DWAX 2010.1
Constraint Description
Enumeration Defines a list of acceptable values
fractionDigits Specifies the maximum number of decimal places allowed. Must be
equal to or greater than zero
Indicators
Length Specifies the exact number of characters or list items allowed. Must be
equal to or greater than zero 7 types:
maxExclusive Specifies the upper bounds for numeric values (the value must be less
than this value)
maxInclusive Specifies the upper bounds for numeric values (the value must be less
All
than or equal to this value) Choice Order indicators
maxLength Specifies the maximum number of characters or list items allowed.
Must be equal to or greater than zero
Sequence
minExclusive Specifies the lower bounds for numeric values (the value must be
greater than this value)
minInclusive Specifies the lower bounds for numeric values (the value must be
greater than or equal to this value) maxOccurs
minLength Specifies the minimum number of characters or list items allowed. minOccurs Occurrence indicators
Must be equal to or greater than zero
Pattern Defines the exact sequence of characters that are acceptable
totalDigits Specifies the exact number of digits allowed. Must be greater than zero
whiteSpace Specifies how white space (line feeds, tabs, spaces, and carriage Group name
returns) are handled
AttributeGroup name Group indicators
(For more examples, see http://www.w3schools.com/schema/schema_facets.asp)43 44
DWAX 2010.1 DWAX 2010.1
11
Order Indicators – Used to define how elements should occur
<all> Specifies by default that the child elements can appear in Occurrence Indicators
any order and that each child element must occur once only – Used to define how often an element can occur
<choice> Specifies that only one of the child elements can occur
<sequence> Specifies that the child elements must appear in a
specific order 45 46
DWAX 2010.1 DWAX 2010.1
Group Indicators
Element groups– Used to define related sets of elements <xs:element name="person" type="personinfo">
47 48
DWAX 2010.1 DWAX 2010.1
12
Attribute Groups - Used to define related sets of attributes Data types in XML Schemas
13
time Data Type
<xs:element name="start" type="xs:time"/> dateTime Data Type:
<period>P5Y</period>
<period>P5Y2M10D</period>
55 56
DWAX 2010.1 DWAX 2010.1
14
Numeric Data Types
Note that all of the data types below derive from the Decimal data type (except for decimal itself)!
boolean Data Type
Name Description
Byte A signed 8-bit integer
<xs:attribute name="disabled" type="xs:boolean"/>
Decimal A decimal value
Int A signed 32-bit integer
<prize disabled="true">999</prize>
Integer An integer value
Long A signed 64-bit integer
negativeInteger An integer containing only negative values ( .., -2, -1.) - legal values for Boolean data types is “true”, “false”, 0 and 1
nonNegativeInteger An integer containing only non-negative values (0, 1, 2, ..)
nonPositiveInteger An integer containing only non-positive values (.., -2, -1, 0)
For more data types, see
positiveInteger An integer containing only positive values (1, 2, ..)
short A signed 16-bit integer
http://www.w3schools.com/schema/schema_dtypes_misc.asp
unsignedLong An unsigned 64-bit integer
unsignedInt An unsigned 32-bit integer
unsignedShort An unsigned 16-bit integer
unsignedByte An unsigned 8-bit integer
57 58
DWAX 2010.1 DWAX 2010.1
Part of family.xsd:
<xs:element name="person"> children.xsd:
<xs:complexType> <?xml version="1.0" encoding="ISO-8859-1"?>
<xs:sequence> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
<xs:element name="firstname" type="xs:string"/> targetNamespace="http://www.w3schools.com"
<xs:element name="lastname" type="xs:string"/> xmlns="http://www.w3schools.com"
<xs:any minOccurs="0"/> elementFormDefault="qualified">
</xs:sequence> <xs:element name="children">
</xs:complexType> <xs:complexType>
</xs:element> <xs:sequence>
<xs:element name="childname" type="xs:string"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
59 60
DWAX 2010.1 DWAX 2010.1
15
MyFamily.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<persons xmlns="http://www.microsoft.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:SchemaLocation="http://www.microsoft.com family.xsd
Element substitution
http://www.w3schools.com children.xsd"> Define a substitutionGroup in the XML schema. Declare a head
<person>
<firstname>Hege</firstname>
element, then other elements which state that they are
<lastname>Refsnes</lastname> substitutable for the head element
<children>
<childname>Cecilie</childname>
</children> <xs:element name="name" type="xs:string"/>
</person> <xs:element name="navn" substitutionGroup="name"/>
<person>
<firstname>Stale</firstname>
<lastname>Refsnes</lastname>
<xs:complexType name="custinfo">
</person> <xs:sequence>
</persons> <xs:element ref="name"/>
</xs:sequence>
For an example using the <anyAttribute> element, see </xs:complexType>
http://www.w3schools.com/schema/schema_complex_anyattribute.asp
<xs:element name="customer" type="custinfo"/>
61 <xs:element name="kunde" substitutionGroup="customer"/>62
DWAX 2010.1 DWAX 2010.1
<kunde>
<navn>John Smith</navn>
</kunde>
16
Resources
– w3schools – XML tutorial
http://www.w3schools.com/schema/default.asp Reading:
– XML Schema Part 0: Primer: Carey: Tutorial 4
http://www.w3.org/TR/xmlschema-0/
– XML Schema Part 1: Structures: W3Schools: Learn XSD
http://www.w3.org/TR/xmlschema-1/ http://www.w3schools.com/schema/default.asp
– XML Schema Part 2: Datatypes
http://www.w3.org/TR/xmlschema-2/
– Namespaces:
http://www.w3.org/TR/REC-xml-names/ Exercise:
– XML schema – introduction & examples Create a Schema for the following XML markup:
http://lucas.ucs.ed.ac.uk/xml-schema/
– XML Schema basics – IBM
http://www-106.ibm.com/developerworks/xml/library/xml-schema/ <?xml version="1.0"?>
– Using W3C XML Schema <book>
http://www.xml.com/lpt/a/2000/11/29/schemas/part1.html <title>Web Applications</title>
– Namespaces tutorial:
http://lucas.ucs.ed.ac.uk/tutorials/namespaces/ <author>John Doe</author>
– Namespaces FAQ: <chapters>
http://www.rpbourret.com/xml/NamespacesFAQ.htm <chapter>Introduction</chapter>
– Understanding XML Schema (MSDN)
<chapter>ASP</chapter>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnxml/html/understandxsd.asp
– Online Validator (will validate your xml files against their schema files):
<chapter>XML</chapter>
http://www.xmlvalidation.com/ </chapters>
</book>
65 66
DWAX 2010.1 DWAX 2010.1
17