0% found this document useful (0 votes)
13 views29 pages

Lecture 7

Uploaded by

manchestermilf1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views29 pages

Lecture 7

Uploaded by

manchestermilf1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

LECTURE 7

Knowledge Representation : AI 303


By
Dr.Ashraf Hendam
OUTLINE
• Document Type Definition (DTD)
• DTD example
• XML Schema (XSD)
• Association of XSD with XML
• XML Schema Data types
• Comparison between DTD and XML schema
Document Type Definition (DTD)
The DTD is declared before the root element
Example
<?xml version="1.0"?>
<!DOCTYPE root_element [
<!ELEMENT root_element (Structure)>
<!ELEMENT Test (#PCDATA)>
]>
<!ELEMENT element-name structure>
elements
where “structure” can be
EMPTY means an element don’t have children
#PCDATA means only atomic type for elements parseable character data.
The name of another element
+ sign indicates one or more frequency
* indicates the element can appear zero or more number of times.
? Indicates elements can occur zero or once.
| logical OR operator
Document Type Definition (DTD)
Attribute
<!ATTLIST element-name attribute-name attribute-type default-value>
Attribute-type
Value Explanation
CDATA The value is character data
ID The value is an unique id
IDREF The value is the id of another element
IDREFS The value is a list of other ids
Attributes default-value
#REQUIRED The attribute value must be included in the element
#IMPLIED The attribute is optional and does not have to be included
#FIXED value The attribute value is fixed
<!ATTLIST element name
first CDATA #REQUIRED
middle CDATA #IMPLIED
last CDATA #REQUIRED>
]>
DTD example
Given the following two XML files

<pizza type=“class1” action=“well done” >


<dough> middle ordinary </dough>
<dough>crispy edges</dough>
<topping>oregano </topping>
</pizza>

<pizza type=“class2” >


< onion > fried </ onion >
< onion > raw </ onion >
<cheese> ordinary </cheese>
<dough> middle ordinary </dough>
<topping>oregano </topping>
</pizza>

Create a DTD file for the previous two XML


DTD example
Solution
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE pizza [
<!ELEMENT pizza (onoin*, cheeze?, dough+, topping)>
<!ELEMENT dough (#PCDATA)>
<!ELEMENT onion (#PCDATA)>
<!ELEMENT cheeze (#PCDATA)>
<!ELEMENT topping (#PCDATA)>
<!ATTLIST pizza
type CDATA #REQUIRED
action CDATA #IMPLIED
]>
]>
XML Schema (XSD)
• XML Schema Definition is defined as a language to describe the
structure of the XML document.
• It means that all the XML Standards are defined in the XSD and
they are written in XML.
• It is recommended by W3C to replace the Document Type
Definition (DTD).
• The schema defines their types or built-in types.
• XML Schema is also allowed to check whether a given XML
document is valid with syntactic Confirmation.
XML Schema (XSD)
• XML Schema is a significantly richer language for defining the
structure of XML documents
• Syntax based on XML itself, so separate tools to handle them
not needed
• Reuse and refinement of schemas => can expand or delete
existing schemas
• Sophisticated set of data types, compared to DTDs, which only
supports strings
XML Schema (XSD)

</xs:schema>
XML Schema (XSD)
• An XML schema is an element with an
opening tag like
<schema
"http://www.w3.org/2000/10/XMLSchema"
version="1.0">
• Structure of schema elements
– Element and attribute types using data types
Association of XSD with XML
Association of your XSD with XML
You must declare the XMLSchema-instance namespace.
The xsi:noNamespaceSchemaLocation attribute defines the URL of your
XSD
XSD file: recipe-no-ns.xsd
<?xml version="1.0" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="list">
XML: recipe-no-ns.xml
<?xml version="1.0" ?>
<list
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="recipe-no-ns.xsd">
<recipe> ....
</list>
Association of XSD with XML

Association of XSD with XML


This is more popular for various reasons (e.g. most XML languages
require a namespace declaration anyhow).
1. Both the XML and the XSD file must contain a namespace
declaration for your domain
2. The XML file must contain in addition: a namespace declaration for
XMLSchema-instance
a xsi:schemaLocation attribute that tells where to find the XSDs.
Association of XSD with XML
Association of XSD with XML
<?xml version="1.0"?>
<list
xmlns="http://myrecipes.org/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://myrecipes.org/ recipe.xsd"
>
<recipe>
<meta> .....</meta>
......
</recipe>
</list>
XML Schema Data types
Built-in data types
– Numerical data types: integer, short, etc.
– String types: string, ID, IDREF, CDATA, etc.
– Date and time data types: time, month, etc.
User-defined data types
– simple data types, which can’t use elements or attributes
– complex data types, which can use them
XML Schema Data types
There are two types of data types in XML schema.
Simple Type
The simple Type allows you to have text-based elements. It
contains less attributes, child elements, and cannot be left empty.
Complex Type
The complex Type allows you to hold multiple attributes and
elements. It can contain additional sub elements and can be left
empty.
XML Schema Data types

Simple Type
A Simple types consists of :
• Element: It contains simple element as well as other element
content.
• Attribute: It’s a type used in Simple data types.
• Restriction: It has acceptable values in the respective XML
document.
XML Schema Data types
Simple Type
• Element
• A simple element is defined as follows
<xs : element name = “ hgfhj” type = “ xxxx” />
• An element declaration is an association of a name with a type
definition
• The name property and holds the description about the element.
The type includes few pre-defined types like xs : integer, xs : date,
xs : string, xs : Boolean.
• The appearance of an element is defined by a cardinality which
are specified using attributes like minOccurs and maxOccurs.
XML Schema Data types
Simple Type
• Element
<xsd:element name="Description" type="xs:string"/>
<xsd:element name="Price" type="xs:decimal"/>
<xsd:element name="Quantity" type="xs:integer"/>
XML Schema Data types
Simple Type
• Element
<element name="email"/>
<element name="head“ minOccurs="1“ maxOccurs="1"/>
<element name="to" minOccurs="1"/>
Cardinality constraints:
– minOccurs="x" (default value 1)
– maxOccurs="x" (default value 1)
– Generalizations of *,?,+ offered by DTDs
XML Schema Data types
Simple Type
• Restriction
• The elements which have some restrictions are called facets.
• It defines the accepting elements.
• It takes data types like enumeration, length, max inclusive,
exclusive.
<xs:element name="bookno">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:pattern value="[1-8 ][1-8][1-8][1-8]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
XML Schema Data types
Simple Type
Restriction
<xsd:element name="element name">
<xsd:simpleType>
<xsd:restriction base="xsd: Integer">
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="10"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
XML Schema Data types
Schema Datatype Restrictions
Constraint Description
enumeration Defines a list of acceptable values
length Specifies the exact number of characters or list items allowed. Must be
equal to or greater than zero
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
than or equal to this value)
maxLength Specifies the maximum number of characters or list items allowed. Must
be equal to or greater than zero
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)
minLength Specifies the minimum number of characters or list items allowed. Must
be equal to or greater than zero
totalDigits Specifies the exact number of digits allowed. Must be greater than zero
pattern Defines the exact sequence of characters that are acceptable
XML Schema Data types
Restriction
<xsd:element name="Size">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="small"/>
<xsd:enumeration value="medium"/
<xsd:enumeration value="large"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
XML Schema Data types
Simple Type
• Attributes
The attribute by itself declares it has simple types. If an element
has specified attributes then it is a complex type.
Syntax of attribute:
<xs : attribute name =” order” type =” string/integer/Boolean”/>
XML document with attribute.
<product id = “2123” > Body Wash </product>
XSD line for the above XML document.
<xs :attribute name =” product “ type =xs :int”/>
If attributes are needed then it is important to assign the keyword
‘use’ to the statement.
<xs :attribute name =” id “ type =xs :int” use=”required” />
XML Schema Data types
• Complex data types
• Simple elements only contain data, complex
elements can hold much more.
• Complex elements can contain attributes,
child elements, and additional indicators that
focus on the order of child elements and how
often they can occur.
XML Schema Data types
Complex data types
are defined from existing data types by defining some attributes (if
any) and using:
– sequence, a sequence of existing data type elements (order is
important)
– all, a collection of elements that must appear (order is not
important)
– choice, a collection of elements, of which one will be chosen(Only
one child element from the list can appear)
XML Schema Data types
<xs:element name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string" />
<xs:element name="author" type="xs:string" />
<xs:element name="isbn" type="xs:integer" />
<xs:element name="genre" type ="xs:string" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
Comparison between DTD and XML schema
Features DTD XML Schema
Powerful (44 + your own derived
Data types Little (10, mostly attribute values)
data types)
Overall complexity low high
Association with XML document DOCTYPE declaration Namespace declaration
File suffix *.dtd *.xsd

You might also like