0% found this document useful (0 votes)
29 views18 pages

Chap 2 XML

Uploaded by

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

Chap 2 XML

Uploaded by

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

CHAP 2

XML
1) HOW TO CREATE XML FILE (name.xml) AND DATA-
tree like structure?
2) HOW TO CREATE DTD (Document Type Definition
FILE AND VALIDATE THE XML DATA
3) HOW TO RETRIEVE THE STORED DATA - XSLT
Introduction to XML
What is XML?
XML stands for eXtensible Markup Language and it is
used for storing and transferring data.
XML tags identify the data and are used to store and
organize the data, rather than specifying how to display it
like HTML tags, which are used to display the data.
XML allows you to create your own self-descriptive tags,
or language, that suits your application.
XML is a markup language that defines set of rules for
encoding documents in a format that is both human-
readable and machine-readable.
A markup language is a set of symbols that can be placed
in the text of a document to label the parts of that
document.
Differences between XML and HTML
XML and HTML were designed with different goals:
XML is designed to carry data emphasizing on what type of
data it is.
HTML is designed to display data emphasizing on how data
looks
XML tags are not predefined like HTML tags.
HTML is about displaying data, hence it is static whereas XML
is about carrying information, which makes it dynamic.
HTML – presentation lang, XML- db lang
HTML markup lang, XML- proves a framework for defining a
markup lang.
HTML – case insensitive lang, xml – case sensitive lang
HTML- tags are pre-defined, XML- tags are self descriptive
XML output is in tree structure
Structure of an XML Document
XML documents must contain a root element. This element is "the
parent" of all other elements.
The elements in an XML document form a document tree. The tree
starts at the root and branches to the lowest level of the tree.
All elements can have sub elements (child elements).
<? XML version=“1.0” encoding=“UTF-8” ?>
<root>
<child attribute=“value”>
<subchild>.....</subchild>
</child>
</root>
XML Tags are Case Sensitive
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
Example, we can see it as a document that can be used for sending
sender’s name (the value inside <from> tag), receiver’s name (the
value inside <to> tag) and the message (the value inside <msg> tag).
<?xml version="1.0" encoding="UTF-8"?>
<message>
<to>MyReader</to>
<from>Alice</from>
<msg>Welcome to SYCS</msg>
</message>
-----------------------------------------------------------------
<Book>
<Author>
<Title>ABC</ Title >
</Author>
</Book>
Entity References
Both, HTML and XML, have some symbols reserved
for their use, which cannot be used as content in XML
code. For example, < and > signs are used for opening
and closing XML tags. To display these special
characters, the character entities are used.
There are few special characters or symbols which are
not available to be typed directly from the keyboard.
Character Entities can also be used to display those
symbols/special characters.
<message>salary < 1000</message>
Replace the "<" character with an entity reference:
<message>salary &lt; 1000</message>
There are 5 pre-defined entity references in XML:

&lt; < less than

&gt; > greater than

&amp; & ampersand

&apos; ' apostrophe

&quot; " quotation mark


Comments in XML
The syntax for writing comments in XML is similar to that
of HTML:
<!-- This is a comment -->
Two dashes in the middle of a comment are not allowed:
<!-- This is an invalid -- comment -->
XML Elements
An XML element is everything from (including) the
element's start tag to (including) the element's end
tag.
<price>29.99</price>
Avoid using “xml” for creating self-descriptive tag.
An element can contain:
text
attributes
other elements
or a mix of the above
XML Attributes

• XML elements can have attributes, just like HTML.


• Attributes are designed to contain data related to a
specific element.
• Attribute values must always be quoted. Either single or
double quotes can be used.
Example:
<Class div=“A">
<Class div=‘A’>
• If the attribute value itself contains double quotes you can
use single quotes, like in this example:
Example:
<student details=‘101 “Alice" A'>
• or we can use character entities:
Example
<student details=‘101 &quot;Alice&quot; A'>
Example:
<bookstore>
<book category="children">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>400</price>
</book>
<book category="web">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>500</price>
</book>
</bookstore>
In the example above:
<title>, <author>, <year>, and <price> have text
content because they contain text (like 29.99).
<bookstore> and <book> have element contents,
because they contain elements.
<book> has an attribute (category="children").
XML Naming Rules (Identifier’s Rules)
XML elements must follow these naming rules:
Element names are case-sensitive
Element names must start with a letter or
underscore
Element names cannot start with the letters xml (or
XML, or Xml, etc)
Element names can contain letters, digits, hyphens,
underscores, and dot(.)
Element names cannot contain spaces. <Ab>
Any name can be used, no words are reserved
(except xml).
Document Type Definition (DTD)
What is DTD
DTD stands for Document Type Definition. It defines the legal
building blocks of an XML document. It is used to define
document structure with a list of legal elements and
attributes.
XML file is Valided and well-formed with DTD
employee.xml
<?xml version="1.0"?>
<!DOCTYPE employee SYSTEM "employee.dtd">
<employee>
<firstname>Ram</firstname>
<lastname>jane</lastname>
<email>[email protected]</email>
In the above example, the DOCTYPE declaration refers to
an external DTD file. The content of the file is shown in
below paragraph.
employee.dtd
<!ELEMENT employee (firstname,lastname,email)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT lastname (#PCDATA)>
<!ELEMENT email (#PCDATA)>
Description of DTD
<!DOCTYPE employee : It defines that the root element
of the document is employee.
<!ELEMENT employee: It defines that the employee
element contains 3 elements "firstname, lastname and
email".
<!ELEMENT firstname: It defines that the firstname
element is #PCDATA typed. (parse-able data type).
<!ELEMENT lastname: It defines that the lastname
element is #PCDATA typed. (parse-able data type).
<!ELEMENT email: It defines that the email element is
#PCDATA typed. (parse-able data type).
PCDATA: PARSEABLE CHARACTER DATA

You might also like