Ai 303:
knowledge
Representation
TA. Menna Sherif
What is XML?
• XML stands for eXtensible Markup language.
• Markup language is a computer language that uses tags
to define elements within a document.
• Xml is used for data presentation.
• Xml is exclusively designed to sent and receive data.
Practical benefits of XML in real-world
applications
• Enables different systems (like databases, applications, and
services) to communicate seamlessly. For instance, a web
application can easily exchange data with a backend service
using XML.
• Many software applications use XML for configuration files (e.g.,
web.xml in Java applications). This allows for easy adjustments
without modifying the codebase.
• Applications can serialize complex data structures to XML,
allowing for easy storage and retrieval. For instance, game
states or user settings can be saved in XML format.
• XML files can be easily tracked in version control systems (like
Git), allowing teams to collaborate on configurations or
structured documents without conflicts.
Difference between HTML and XML
• HTML • XML
Was designed to display Was designed to carry
data. data.
With focus on how data With focus on what data
look. is.
Tags are predefined. Tags are not predefined.
What is XML structure ?
1- Prolog :
1- contain XML declaration:
<?xml version="1.0" encoding="UTF-16"?>
2- And optional reference to (Document Type Definition) DTD.
<!DOCTYPE book SYSTEM "book.dtd">
2- Elements : consist of
– opening tag
– the content
– a closing tag
<name>Menna sherif</name>
Note: Element content can be text, or other elements, or nothing .
XML Syntax Rules
• XML documents must contain one root element.
• Each element contains an opening and a corresponding
closing tag.
• XML tags are Case Sensitive.
- XML Elements Rules:
• Element names are case-sensitive.
• Element names must start with a letter or underscore.
• Element names cannot start with the letter xml (or XML or
Xml, etc.).
• Element names can contain letters, digits, hyphens,
underscores, and periods.
• Element names cannot contain spaces.
- XML Attributes
• An attribute is a name-value pair inside the opening tag of
an element.
<text category = "message">Hello</text>
XML element is text, the category is the attribute name
and message is the attribute value.
<text category = "message" purpose ="Greet">Hello</text>
two attribute is used with different name. So, in a single
element multiple attribute is used having unique attribute
name.
But if we use two distinct element then we can use the
attribute having the same attribute name.
An XML Example
Email
-from ‘ali’, email ‘ali.com’
-to ‘ola’ , email ‘ola.com’
-subject ‘Where is your draft?’
-body ‘ola, where is the draft of the paper you promised
me last week?’
An XML Example
<email>
<head>
<from>
<name>ali</name>
<email>ali.com</email>
</from>
<to>
<name>ola</name>
<email>ola.com</email>
</to>
<subject> Where is your draft?’ </subject>
</head>
<body>
ola, where is the draft of the paper you promised
me last week?’
</body>
</email>
An XML Example
<email>
<head>
<from name="ali" email="ali.com"/>
<to name="ola" email="ola.com"/>
<subject>Where is your draft?</subject>
</head>
<body>
ola, where is the draft of the paper you promised
me last week?’
</body>
</email>
- XML Namespaces
• XML Namespaces provide a method to avoid element
name conflicts, Using:
Prefix
Default Namespaces
• While your original XML is technically valid, restructuring
it to avoid duplicate <names> elements can enhance
clarity and prevent potential semantic conflicts when it
comes to data processing or interpretation.
- XML Namespaces Example 1
<?xml version="1.0" encoding="UTF-16"?>
<root>
<names>
<name>ali</name>
<name>ahmed</name>
<name>kareem</name>
</names>
<names>
<name>ola</name>
<name>mai</name>
<name>mona</name>
</names>
</root>
- XML Namespaces Example 1 (cont…)
Element names will cause conflict
How we solve this conflict?
Using prefix
Using default namespace
- XML Namespaces Example 1 (cont..)
<?xml version="1.0" encoding="UTF-16"?>
<root>
<m:names>
<m:name>ali</m:name>
<m:name>ahmed</m:name>
<m:name>kareem</m:name>
</m:names>
<f:names>
<f:name>ola</f:name>
<f:name>mai</f:name>
<f:name>mona</f:name>
</f:names>
</root>
- XML Namespaces Example 1 (cont…)
Then open the xml file in the browser
Error because prefix (m,f) are not defined.
Then we should define them.
- XML Namespaces Example 1 (cont..)
<?xml version="1.0" encoding="UTF-16"?>
<root>
<m:names xmlns:m="url">
<m:name>ali</m:name>
<m:name>ahmed</m:name>
<m:name>kareem</m:name>
</m:names>
<f:names xmlns:f="url2">
<f:name>ola</f:name>
<f:name>mai</f:name>
<f:name>mona</f:name>
</f:names>
</root>
- XML Namespaces Example 1 (cont…)
Then open the xml file in the browser
Xml file appear with no errors.
-- We can also define prefixes in root tag
- XML Namespaces Example 1 (cont..)
<?xml version="1.0" encoding="UTF-16"?>
<root xmlns:m="url" xmlns:f="url2" >
<m:names >
<m:name>ali</m:name>
<m:name>ahmed</m:name>
<m:name>kareem</m:name>
</m:names>
<f:names >
<f:name>ola</f:name>
<f:name>mai</f:name>
<f:name>mona</f:name>
</f:names>
</root>
- XML Namespaces Example 1 (cont…)
Then open the xml file in the browser
Xml file appear with no errors.
- XML Namespaces Example 1 (cont..)
Note: The namespace URI is not used by the parser to look up information.
The purpose of using an URI is to give the namespace a unique name.
However, companies often use the namespace as a pointer to a web page
containing namespace information.
- XML Namespaces Example 1 (cont…)
Element names will cause conflict
How we solve this conflict?
Using prefix
Using default namespace
- XML Namespaces Example 1 (cont..)
<?xml version="1.0" encoding="UTF-16"?>
<root >
<names xmlns="url" >
<name>ali</name>
<name>ahmed</name>
<name>kareem</name>
</names>
<names xmlns="url2">
<name>ola</name>
<name>mai</name>
<name>mona</name>
</names>
</root>
- XML Namespaces Example 1 (cont…)
Then open the xml file in the browser
Xml file appear with no errors.