0% found this document useful (0 votes)
11 views25 pages

Lecture 6

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)
11 views25 pages

Lecture 6

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/ 25

LECTURE 6

Knowledge Representation : AI 303


By
Dr.Ashraf Hendam
OUTLINE
• Valid XML document
• Elements
• Attributes
Valid XML document
• Syntactic rules say nothing specific about the structure
of the document.
• It is necessary to define all the elements and attributes
names that may be used.
• The structure should also be defined: what values an
attribute may take, which elements may or must occur
within other elements.
Valid XML document
• Define all the elements and attributes names that may be
used.
• Define the structure:
-what values an attribute may take
-which elements may or must occur within other
elements, etc.
• If such structuring information exists, the document can be
validated
Valid XML document
1. XML should be behave according to predefined Document
Type Definition (DTD) or XML schema.
– DTDs () came first, was based on SGML’s approach
– XML Schema (aka XML Schema Definition, XSD) is
more recent and expressive
Document Type Definition (DTD)
• DTD is a set of markup declarations that define a
document type.
• The components of a DTD can be defined in a
separate file (external DTD) or within the XML
document itself (internal DTD).
• Usually it is better to use external DTDs, because
their definitions can be used across several
documents; otherwise duplication is inevitable, and
the maintenance of consistency over time becomes
difficult.
Document Type Definition (DTD)
Internal DTD within the XML document itself
The DTD is declared before the root element
Example
<?xml version="1.0"?>
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
</note>
Document Type Definition (DTD)
• External DTD
• DTD can be defined in a separate file
Example
<?xml version="1.0"?> note.dtd
<!DOCTYPE note SYSTEM "note.dtd"> <?xml version="1.0"?>
<note> <!ELEMENT note (to,from,heading,body)>
<to>Tove</to> <!ELEMENT to (#PCDATA)>
<from>Jani</from> <!ELEMENT from (#PCDATA)>
<heading>Reminder</heading> <!ELEMENT heading (#PCDATA)>
<body>Don't forget me this weekend!</body> <!ELEMENT body (#PCDATA)>
</note>
Elements
• <!DOCTYPE root-element
optional-external-reference
optional-internal-declarations>
Declaring an Element
• XML elements are declared with an element
declaration.
• An element declaration has the following syntax:
<!ELEMENT element-name structure>
• Example
<!ELEMENT ship (crew name, location, date)>
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
Elements
<!ELEMENT lecturer (name,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
Valid XML according to previous DTD
<lecturer>
<name>Paul Fodor</name>
<phone> +1 (123)456−7890 </phone>
</lecturer>
Elements
• The meaning of this DTD is as follows:
- The element types lecturer, name, and phone will be
used in the document
- A lecturer element contains a name element and a
phone element, in that order (sequence)
- A name element and a phone element may have any
content
- In DTDs, #PCDATA is the only atomic type for
elements
Elements
Example
<?xml version="1.0"?>
<!DOCTYPE name [
<!ELEMENT name (first, middle, last)>
<!ELEMENT first (#PCDATA)>
<!ELEMENT middle (#PCDATA)>
<!ELEMENT last (#PCDATA)>
]>
XML file
<name>
<given>Joseph</given>
<middle>John</middle>
<last>Fawcett</last>
</name>
Elements
Example
<?xml version="1.0"?>
<!DOCTYPE name [
<!ELEMENT name (first, middle, last)>
<!ELEMENT first (#PCDATA)>
<!ELEMENT middle (#PCDATA)>
<!ELEMENT last (#PCDATA)>
]>
XML file
<name>
<given>Joseph</given> Valid or not valid ,
<middle>John</middle> why and correct it
<last>Fawcett</last>
</name>
Elements
Example
<?xml version="1.0"?>
<!DOCTYPE name [
<!ELEMENT name (first, middle, last)>
<!ELEMENT first (#PCDATA)>
<!ELEMENT middle (#PCDATA)>
<!ELEMENT last (#PCDATA)>
]>
XML file
<name>
<middle>John</middle> Valid or not valid ,
<first>Joseph</first> why and correct it
<last>Fawcett</last>
</name>
Elements
Example ? Indicates elements can occur zero or once.
<!DOCTYPE pizza [
<!ELEMENT pizza (onion?, cheese, cheese,topping)>
<!ELEMENT onion (#PCDATA)>
<!ELEMENT cheese (#PCDATA)>
<!ELEMENT cheese (#PCDATA)>
<!ELEMENT topping (#PCDATA)>
]>
XML file Valid or not valid , why and correct it
<pizza> <pizza>
<onion> fried </onion> <onion> fried </onion>
<onion> grilled </onion>
<cheese> thin </cheese>
<cheese> thick </cheese> <cheese> thin </cheese>
<topping>oregano </topping> <cheese> thick </cheese>
</pizza> <topping>oregano </topping>
</pizza>
Elements
Example + sign indicates one or more frequency
<!DOCTYPE pizza [
<!ELEMENT pizza (onion+, cheese, cheese, topping)>
<!ELEMENT onion (#PCDATA)>
<!ELEMENT cheese (#PCDATA)>
<!ELEMENT cheese (#PCDATA)>
<!ELEMENT topping (#PCDATA)>
]>
XML file Valid or not valid , why and correct it
<pizza> <pizza>
<onion> fried </onion> <cheese> thin </cheese>
<cheese> thin </cheese> <cheese> thick </cheese>
<cheese> thick </cheese> <topping>oregano </topping>
<topping>oregano </topping> </pizza>
</pizza>
Elements
Example * indicates the element can appear zero or more number of times.
<!DOCTYPE pizza [
<!ELEMENT pizza (onion*, cheese, cheese, topping)>
<!ELEMENT onion (#PCDATA)>
<!ELEMENT cheese (#PCDATA)>
<!ELEMENT cheese (#PCDATA)>
<!ELEMENT topping (#PCDATA)>
]>
XML file Valid or not valid , why and correct it
<pizza> <pizza>
<onion> fried </onion> <cheese> thin </cheese>
<cheese> thin </cheese> <cheese> thick </cheese>
<cheese> thick </cheese> <topping>oregano </topping>
<topping>oregano </topping> </pizza>
</pizza>
Elements
• We express that a lecturer element contains either a
name element or a phone element as follows:
• <!ELEMENT lecturer (name|phone)>
• It gets more difficult when we wish to specify that a
lecturer element contains a name element and a phone
element in any order.
• We can only use the trick:
<!ELEMENT lecturer((name,phone)|(phone,name))>
• However, this approach suffers from practical
limitations (imagine ten elements in any order).
Elements
Example • | logical OR operator
<!DOCTYPE pizza [
<!ELEMENT pizza ((onion|cheese), topping)>
<!ELEMENT onion (#PCDATA)>
<!ELEMENT cheese (#PCDATA)>
<!ELEMENT topping (#PCDATA)>
]>
XML file
<pizza> Valid or not valid , why and correct it
<cheese> thin </cheese> <pizza>
<topping>oregano </topping> <topping>oregano </topping>
</pizza> </pizza>
Attributes
• Attributes provide extra information about elements.
• Attributes are placed inside the start tag of an element.
• Attributes come in name/value pairs.
Declaring Attributes
• Element attributes are declared with an ATTLIST
declaration.
Attributes
• An attribute declaration has the following syntax:
<!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

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
Attributes
Example
<!DOCTYPE name [
<!ELEMENT name >
<!ATTLIST name
first CDATA #REQUIRED
middle CDATA #IMPLIED
last CDATA #REQUIRED>
]>
XML file
<name first="Joseph" middle="John“ last="Fawcett" />
<name first="Joseph" last="Fawcett" />
Attributes
Attributes
Example
<!DOCTYPE name [
<!ELEMENT name >
<!ATTLIST name
first CDATA #REQUIRED
middle CDATA #IMPLIED
last CDATA #REQUIRED>
]>
XML file Valid or not valid , why and correct it
<name first="Joseph" middle="John“ last="Fawcett" />
<name middle="John“ last="Fawcett" />
<name first="Joseph" last="Fawcett" />

You might also like