Você está na página 1de 7

XML

1. Definition
2. Elements types
3. Well Formed XML document
4. Validate XML document

Definition

‫ والـ‬server ‫ تحتوي على سجالت لنقلها على االنترنت بين الـ‬text files ‫ هدفها هو إعادة تنظيم هيكلية قواعد البيانات من اجل بناء ملفات نصية‬XML ‫لغة‬
smart phone application ‫ والهواتف الذكية‬client
XML is a documentation language used to re-organize the database and build text files with records in order to
send/receive them by internet between Client/Server and mobile application

XML document main syntax and example:

Declaration Statement

<?xml version=“1.0” encoding=“utf-8” standalone=“yes” ?>


<Stock>
Root Element
<Product ID=“102”>
<Designation>Nescafé</Designation>
<Price>112</Price>
<Qty>50</Qty>
</Product>
‫هيكلية هرمية‬
<Product> Hierarchical Schema
<Designation>Nido</Designation> . . . ‫اب وابن‬
<Price>320</Price>
<Qty>10</Qty>
<Discount />
</Product>
</Stock>

Note:
version= “1.0” attribute is always 1.0 and must be written in the declaration statement
encoding and standalone are optional so they can used both or one of them or none of them

XML document must have one root example in the previous exercise the root is <Stock>
Elements Types

There are four main elements types:


1. Simple Content Model
2. Element Content Model
3. Mixed Content Model (Complex Content Model)
4. Empty Content Model

Examples:

Element Type Description + Example


Simple Content Model <Designation>Nido</Designation>

<Designation> is a simple content model because it contains just text or numbers

Element Content Model <Product>


<Designation>. . . </Designation>
<Price>. . . </Price>
. . .
</Product>

<Product> is an element content model because it contains sub elements

Mixed Content Model <Address>


<City>Tripoli</City>
Tawjih street
CIS Bldg
</Address>

The <Address> is a mixed content model, because it contains sub elements and text
Empty Content Model <Discount></Discount>
OR
<Discount />
Well Formed XML document

 All XML elements must have a closing tag


 All XML elements must be properly nested (cannot overlap)
 XML documents can have only one root element with good hierarchical schema ‫هيكلية هرمية‬
 Elements names must obey XML naming conventions
 XML is case sensitive
 Attribute values must always be quoted (single quote or double quotes)
 XML document should be declared

All XML elements must be properly nested (cannot overlap)

Illegal Example: ‫مثال خطأ‬

<P>
XML stands for <B>Extensible Markup <U>Language</B></U>
</P>

Corrected Example: ‫مثال صحيح‬

<P>
XML stands for <B>Extensible Markup <U>Language</U></B>
</P>

Elements names must obey XML naming conventions

Elements names must obey the following rules:

 Names can start with letter or underscore, but cannot start with numbers or other punctuation characters
 After the first character, numbers are allowed, as are the characters “-” and “.”
 Names cannot contain spaces
 Names cannot contain the “:” (unless used in namespace)
 Names cannot start with the letters “xml” in uppercase, lowercase or mixed, so you cannot start a name with
“xml”, “XML”, “XmL” or any other combination
 There cannot be a space after the opening “<” character, the name of the element must come after it. However
there can be a space before the closing “>” character if desired.
Example: < Price>  ‫خطأ‬
<Price >  ‫صح‬
 The space is not allowed between /> i.e <Discount / >  illegal ‫خطأ‬ <Discount /> ‫صح‬
 Do care to the sensitivity of the opening and closing elements
 Comments are written as following:
<!-- This is a comment -->
Validate XML document
To validate the XML document content, we have to use other languages such as:
1. DTD (Document Type Definition)
2. Schema

DTD (Document Type Definition)


This language is written in Capital letters to validate the XML elements, could be:
 Internally
 Externally
Example:
Given following xml document:
<Stock>
<Product ID=“102”>
<Designation>Nescafé</Designation>
<Price>112</Price>
<Qty>50</Qty>
</Product>
<Product>
<Designation>Nido</Designation>
<Price>320</Price>
<Qty>10</Qty>
<Discount />
</Product>
</Stock>

Write the internal DTD to validate the previous xml document

Solution:
Internally

Products.xml
<?xml version=“1.0” encoding=“utf-8” standalone=“no” ?>
<!DOCTYPE Stock [
<!ELEMENT Stock (Product+)>
<!ELEMENT Product (Designation, Price, Qty, Discount?)>
<!ELEMENT Designation (#PCDATA)>
<!ELEMENT Price (#PCDATA)>
<!ELEMENT Qty (#PCDATA)>
<!ELEMENT Discount EMPTY>
]>
<Stock>
. . .
</Stock>

Externally
Products.dtd
<!ELEMENT Stock (Product+)>
<!ELEMENT Product (Designation, Price, Qty, Discount?)>
<!ELEMENT Designation (#PCDATA)>
<!ELEMENT Price (#PCDATA)>
<!ELEMENT Qty (#PCDATA)>
<!ELEMENT Discount EMPTY>

Products.xml
<?xml version=“1.0” encoding=“utf-8” standalone=“no” ?>
<!DOCTYPE Stock [SYSTEM “Products.dtd”]>
<Stock>
. . .
</Stock>
Conclusion:
Element Type Written in DTD
Simple Content Model <!ELEMENT Designation (#PCDATA)>

Example:
<Designation>Nido</Designation>
Element Content Model

Example1: Example1:
<College> <!ELEMENT College (Student+)>
<Student>Ahmad</Student> <!ELEMENT Student (#PCDATA)>
<Student>Raed</Student
</College>

Example2: Example2:
<College> <!ELEMENT College (Student, Teacher)+>
<Student>Mhd</Student> <!ELEMENT Student (#PCDATA)>
<Teacher>Salem</Teacher> <!ELEMENT Teacher (#PCDATA)>
<Student>Nancy</Student>
<Teacher>Rami</Teacher>
</College>

Example3: Example3:
<College> <!ELEMENT College (Student+, Teacher+)>
<Student>Abed</Student>
<Student>Israa</Student>
<Teacher>Raed</Teacher>
<Teacher>Diana</Teacher>
</College>
Note:
Comma (,)  sequence ‫خلف بعض‬
Example4:
<College>
<Student> | (OR)  ‫ او‬choice
<FName>Rima</FName>
<Middle>Mhd</Middle> Cardinalities:
<LName>Intabli</LName> ? 0–1
</Student>
* 0–n
<Student>
<LName>Rifaii</LName> + 1-n
<Middle>Saleh</Middle>
<FName>Jamal</FName>
</Student>

<Student>Israa Sherkawi</Student>
</College>
Example4 Solution:
<!ELEMENT College (Student+)>
<!ELEMENT Student (#PCDATA)|((FName, Middle, LName)|(LName, Middle, FName))>

<!ELEMENT FName (#PCDATA)>


<!ELEMENT Middle (#PCDATA)>
<!ELEMENT LName (#PCDATA)>
EMPTY ELEMENT <!ELEMENT Discount EMPTY>

<Discount></Discount>
‫او‬
<Discount />
Mixed Content Model <!ELEMENT Address (#PCDATA | City |B)*>

<Address>
<City>Tripoli</City>
Tawjih Street
<B>CIS</B> Bldg
</Address>
How to validate attributes in DTD?
Example + syntax:

<College Phone=“01-820821”>
<Student ID=“100” Photo=“&Tar;”>Tarek Saad</Student>
<Student ID=“102” Photo=“&Rim;” Nationality=“Lebanese”>Rima Kouwatli</Student>
<Student ID=“103” Photo=“&Ah;”>Ahmad Rifaii</Student>
<Class Room=“303” Section=“2” Lang=“EN” />
<Class Room=“304” Section=“1” Lang=“FR” />
<Teacher Advisor= “100, 102”>Salam Naboulsi</Teacher>
<Book ISBN=“21-28b-33” Covers=“&co1; &co2;”>XML for Beginners</Book>
</College>

Note:
ID in student is an ID
Room attribute is an ID
Lang attribute could be “EN” or “FR”

Description
The data type of the attributes could be:
1. CDATA
2. NMTOKEN, NMTOKENS  Text with multiple lines, feeding spaces
3. ID
4. IDREF, IDREFS
5. Enumeration
6. ENTITY, ENTITIES

Solution:

<!NOTATION xyz SYSTEM “Photoshop.exe”>


<!ENTITY Tar SYSTEM “tarek.jpg” NDATA xyz>
<!ENTITY Rim SYSTEM “Rim.jpg” NDATA xyz>
<!ENTITY Ah SYSTEM “Ahmad.jpg” NDATA xyz>
<!ENTITY co1 SYSTEM “Cover1.jpg” NDATA xyz>
<!ENTITY co2 SYSTEM “Cover2.jpg” NDATA xyz>
<!ELEMENT College (Student+, Class+, Teacher+, Book+)>
<!ELEMENT Student (#PCDATA)>
<!ELEMENT Class #EMPTY>
<!ELEMENT Teacher (#PCDATA)>
<!ELEMENT Book (#PCDATA)>
<!ATTLIST College Phone CDATA #REQUIRED>
<!ATTLIST Student ID ID #REQUIRED
Photo ENTITY #REQUIRED
Nationality CDATA #IMPLIED>
<!ATTLIST Class Room ID #REQUIRED
Section CDATA #REQUIRED
Lang (EN | FR) #REQUIRED>
<!ATTLIST Teacher Advisor IDREFS #REQUIRED>
<!ATTLIST Book ISBN CDATA #REQUIRED
Covers ENTITIES #REQUIRED>

Note:
NDATA  Not Parsed data which mean it is not text

Você também pode gostar