Professional Documents
Culture Documents
XML
XML tags identify the data and used to store and organize
What is Markup?
Markup is information added to a document that enhances its
meaning in certain ways, in that it identifies the parts and how they
relate to each other.
More specifically, a markup language is a set of symbols that can
3
Example 1
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
</note>
4
Example 2
<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<calories>650</calories>
</food>
<food>
<name>French Toast</name>
<price>$4.50</price>
5
<description>Thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<calories>950</calories>
</food>
</breakfast_menu>
6
XML is a software and hardware-independent tool for storing and
transporting data.
What is XML?
XML stands for EXtensible Markup Language
7
The Difference between XML and HTML
XML Separates Data from Presentation: XML does not carry any
The same XML data can be used in many different presentation scenarios.
Because of this, with XML, there is a full separation between data and
presentation.
8
The Difference between XML and HTML
1. XML is extensible: XML essentially allows you to create your own
language, or tags, that suit your application.
9
XML Tree Structure
XML documents are formed as element trees.
An XML tree starts at a root element and branches from the root to child
elements.
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
The terms parent, child, and sibling are used to describe the relationships
between elements. 10
XML Tree
document.
Example
<bookstore>
<price>. 12
Cont.
The <book> elements have 4 child elements: <title>,< author>, <year>,
<price>.
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
13
XML Syntax Rules
The syntax rules of XML are very simple and logical. The rules are
In HTML, some elements might work well, even with a missing closing
tag:
<p>This is a paragraph.
<br>
14
XML Syntax Rules
In XML, it is illegal to omit the closing tag.
<p>This is a paragraph.</p>
<br />
The XML prolog does not have a closing tag. This is not an error. The
XML tags are case sensitive. The tag <Letter> is different from the tag
<letter>.
Opening and closing tags must be written with the same case: 15
XML Syntax Rules
<Message>This is incorrect</message>
<message>This is correct</message>
In the example above, "Properly nested" simply means that since the
<i> element is opened inside the <b> element, it must be closed inside
the <b> element. 16
XML Syntax Rules
5. XML Attribute Values Must be quoted In HTML, you might see
improperly nested elements:XML elements can have attributes in
name/value pairs just like in HTML. In XML, the attribute values
must always be quoted.
CORRECT:
INCORRECT:
<note date="12/11/2007">
<note date=12/11/2007>
<to>Tove</to>
<to>Tove</to>
<from>Jani</from>
<from>Jani</from>
</note>
</note>
The error in the first document is that the date attribute in the note element
is not quoted. 17
6. Entity References
like "<" inside an XML element, it will generate an error because the
parser interprets it as the start of a new element.
To avoid this error, replace the "<" character with an entity reference:
<message>salary < 1000</message>
Only < and & are strictly illegal in XML, but it is a good habit to replace > with > as well.
18
Comments in XML
<!-- This is a comment --> Two dashes in the middle of a comment are not
allowed.
19
XML Elements
Example <price>29.99</price>
• text
• attributes
<element></element> 20
XML Naming Rules
Element names cannot start with the letters xml (or XML, or
Xml, etc)
and periods
21
Best Naming Practices
Create short and simple names, like this: <book_title> not like this:
<the_title_of_the_book>.
Avoid "-". If you name something "first-name", some software may think
Avoid ".". If you name something "first.name", some software may think
Non-English letters like éòá are perfectly legal in XML, but watch out for
XML elements can have attributes, just like HTML. Attributes are
can be used.
For a person's gender, the <person> element can be written like this:
<person gender="female">
23
XML Elements vs. Attributes XML Elements vs. Attributes
Example 1 <person>
<firstname>Anna</firstname> <firstname>Anna</firstname>
<lastname>Smith</lastname> <lastname>Smith</lastname>
</person> </person>
Name Conflicts
In XML, element names are defined by the developer. This often results in
a conflict when trying to mix XML documents from different XML
applications.
25
This XML carries HTML table information:
Example Example
<table> <table>
conflict.
Both contain a <table> element, but the elements have different content
and meaning.
differences.
27
This XML carries HTML table information:
Example Example
<h:table> <f:table>
In the example above, there will be no conflict because the two <table>
elements have different names. 28
XML Namespaces - The xmlns Attribute
When using prefixes in XML, a namespace for the prefix must be defined.
element.
<h:table xmlns:h="http://www.w3.org/TR/html4/">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table> 29
XML Namespaces - The xmlns Attribute
When a namespace is defined for an element, all child elements with the
xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="http://www.w3schools.com/furniture">
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
30
</h:table>
XML Namespaces - The xmlns Attribute
information.
The most common URI is the Uniform Resource Locator (URL) which
31
XML Namespaces - The xmlns Attribute
<table xmlns="http://www.w3.org/TR/html4/">
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
32
Well Formed XML Documents
33
Valid XML Documents
document.
There are two different document type definitions that can be used
with XML:
A document type definition defines the rules and the legal elements
and "Valid".
The order in which they can appear. Element attributes and whether
they are optional or mandatory. Whether attributes can have default
values.
35
DTD Example
<!DOCTYPE note
<!ELEMENT to (#PCDATA)>
]>
36
XML DTD
!DOCTYPE note defines that the root element of the document is note
!ELEMENT note defines that the note element must contain the
"#PCDATA“
37
Advantages of using DTD
Documentation - You can define your own format for the XML files.
such as:
1. Internal DTD
2. External DTD
When a DTD is declared within the file it is called Internal DTD and if
Internal DTD
Rules
The document type declaration must appear at the start of the document
The Name in the DTD must match the element type of the root element.
40
Declaration <?xml version="1.0“ encoding="UTF-8" standalone="yes"?>
DOCTYPE declaration <!DOCTYPE note [
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
</note> 41
Example 2 of internal DTD:
Declaration <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<name>Tanmay Patil</name>
<company>TutorialsPoint</company>
<phone>(011) 123-4567</phone>
42
</address>
External DTD
Syntax
43
Example 1 of External DTD:
XML Declaration <?xml version="1.0 " encoding="UTF-8" standalone="no"?>
<note>
<to>Tove</to>
DTD name
<from>Jani</from>
</note>
44
Example 2 of External DTD:
Declaration
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<address>
<name>Tanmay Patil</name>
DTD name
<company>TutorialsPoint</company>
XML Document Body
<phone>(011) 123-4567</phone>
</address>
You can refer to an external DTD by either using system identifiers or public
identifiers. 45
DTD – COMPONENTS
document.
Attributes
Entities
Empty content
Element content
Mixed content
46
Element Content Types
47
Rules
We need to follow certain rules if there is more than one element content:
distinct order.
For example:
2. Choices: Suppose you need to allow one element or another, but not
both. In such cases you must use the pipe | character. The pipe functions
as an exclusive OR.
For example:
49
DTD – ATTRIBUTES
Syntax
50
Example
Declaration <?xml version="1.0" encoding="UTF-8" standalone=“yes" ?>
]>
<address>
</address>
51
Rules of Attribute Declaration
All attributes used in an XML document must be declared in the Document
52
Rules of Attribute Declaration
Within each attribute declaration, you must specify how the value will appear
III. is Required
IV. is Implied
CSS is easy to learn and style sheets can be included directly in xml documents
54
Cont.
Multiple style sheets can be written to provide for different output on the same
formatting device.
External style sheet which is saved in an external file and is referenced using
the link tag in the xml document .
XSL is a special style sheet mechanism created specifically for XML documents
55
Cont.
CSS is the primary style language used to direct the display of XML documents
document.
56
Example 1
<?xml version="1.0" encoding="UTF-8"?>
<CATALOG>
<CD>
<TITLE>Picture book</TITLE>
<ARTIST>Simply Red</ARTIST>
<COUNTRY>EU</COUNTRY>
<COMPANY>Elektra</COMPANY>
<PRICE>7.20</PRICE>
<YEAR>1985</YEAR>
</CD>
57
</CATALOG>
Today.css TITLE {
} ARTIST {
CD { display: block;
margin-left: 0; }
display: block;
color: #000000;
58
margin-left: 20pt;
Cont.
The style sheet processing instruction is added to the beginning of the XML
document so that the XML document can locate its attached style sheet.
Type indicates that a text file is being converted into css file.
59
Cont.
To use HTML tags in the xml document. All the tags have to be prefixed with
<HTML:A>..</HTML:A>
Here A refers to the <A> ……</A> anchor element of HTML. This tag is
60