Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

XML (BScCSIT 5th Semester)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 39

AJAX and XML

Prepared by: Rishav Malla Thakuri


Introduction to XML

• XML stands for eXtensible Markup Language.


• XML was designed to store and
• XML is a markup language much like HTML transport
data.
• XML was designed to be self-descriptive.
• XML tags are not predefined. You must define your
own tags.
• Extensible Markup Language (XML) is a markup
language that defines a set of rules for encoding
documents in a format that is both human-readable
and machine-readable
Introduction to XML

• XML is not a replacement for HTML. XML and


HTML were designed with different goals:
 XML was designed to transport and store
data, with focus on what data is
 HTML was designed to display data, with
focus on how data looks
• HTML is about displaying information, while
XML is about carrying information.
Introduction to XML

• Maybe it is a little hard to understand, but XML does not DO


anything. XML was created to structure, store, and transport
information.
• The following example is a note to Tulsi, from Giri, stored
as XML:

• <note>
• <to>Tulsi</to>
• <from>Giri</from>
• <heading>Reminder</heading>
• <body>Don't forget to bunk web tech class at Patan!</body>
• </note>
Introduction to XML
• The note above is quite self descriptive. It has sender and
receiver information, it also has a heading and a message
body.
• But still, this XML document does not DO anything. It is
just information wrapped in tags. Someone must write a
piece of software to send, receive or display it.
• The tags in the example above (like <to> and
<from>) are not defined in any XML standard. These
tags are "invented" by the author of the XML document.
• That is because the XML language has no predefined
tags. However, the tags used in HTML are predefined.
HTML documents can only use tags defined in the HTML
standard (like <p>, <h1>, etc.).
• In contrast, XML allows the author to define his/her own
tags and his/her own document structure. The XML
processor can not tell us which elements and attributes
are valid. As a result we need to define the XML markup
we are using. To do this, we need to define the markup
language’s grammar. There are numerous “tools” that
can be used to build an XML language – some relatively
simple, some much more complex. They include
• DTD (Document Type Definition), RELAX, TREX,
RELAX NG, XML Sche ma, Schmatron, etc.
XML Simplifies Things

• It simplifies data sharing


• It simplifies data transport
• It simplifies platform changes
• It simplifies data availability
• Many computer systems contain data in incompatible formats.
Exchanging data between incompatible systems (or upgraded
systems) is a time-consuming task for web developers. Large
amounts of data must be converted, and incompatible data is often
lost.
• XML stores data in plain text format. This provides a software- and
hardware-independent way of storing, transporting, and sharing
data.
• XML also makes it easier to expand or upgrade to new operating
systems, new applications, or new browsers, without losing data.
• With XML, data can be available to all kinds of "reading machines"
like people, computers, voice machines, news feeds, etc.
XML Usages

• XML is used in many aspects of web development, often to


simplify data storage and sharing.

• XML Separates Data from HTML: If you


need to display dynamic data in your HTML document, it will
take a lot of work to edit the HTML each time the data
changes. With XML, data can be stored in separate XML files.
This way you can concentrate on using HTML for layout and
display, and be sure that changes in the underlying data
will not require any changes to the HTML. With a few lines of
JavaScript code, you can read an external XML file and update
the data content of your web page.
XML Usages
• XML Simplifies Data Sharing: In the real world, computer systems
and databases contain data in incompatible formats. XML data is stored
in plain text format. This provides a software- and hardware-independent
way of storing data. This makes it much easier to create data that can be
shared by different applications.
• XML Simplifies Data Transport: One of the most time-consuming
challenges for developers is to exchange data between incompatible
systems over the Internet. Exchanging data as XML greatly reduces this
complexity, since the data can be read by different incompatible applications.
• XML Simplifies Platform Changes: Upgrading to new systems
(hardware or software platforms), is always time consuming. Large
amounts of data must be converted and incompatible data is often lost.
XML data is stored in text format. This makes it easier to expand or upgrade
to new operating systems, new applications, or new browsers, without losing
data.
XML Usages
• XML Makes Your Data More Available: Different applications can access
your data, not only in HTML pages, but also from XML data sources. With XML,
your data can be available to all kinds of "reading machines" (Handheld computers,
voice machines, news feeds, etc), and make it more available for blind people, or
people with other disabilities.

• XML Used to Create New Internet Languages:


• A lot of new Internet languages are created with XML. Here are some examples:
• XHTML
• WSDL (Web Services Description Language) for describing available web services
• WAP and WML (Wireless Markup Language) as markup languages for handheld
• devices
• RSS (Really Simple Syndication / Rich Site Summary) languages for news feeds
• RDF (Resource Description Framework), a family of w3c spec, and OWL (Web
• Ontology Language) for describing resources and ontology
• SMIL (Synchronized Multimedia Integration Language) for describing multimedia
for the web.
XML Tree
• XML documents form a tree structure that starts at "the root" and
branches to "the leaves".
XML Tree
• XML documents use a self-describing and simple syntax:
• <?xml version="1.0" encoding="ISO-8859-1"?>
• <note>
• <to>Tulsi</to>
• <from>Giri</from>
• <heading>Reminder</heading>
• <body>Don't forget to bunk the web tech class at Patan!</body>
• </note>

• The first line is the XML declaration. It defines the XML version (1.0) and
the encoding used (ISO-8859-1 = Latin-1/West European character set). The
next line describes the root
• element of the document (like saying: "this document is a note"):
• <note>
• The next 4 lines describe 4 child elements of the root (to, from, heading, and body):
• <to>Tulsi</to>
• <from>Giri</from>
• <heading>Reminder</heading>
• <body>Don't forget to bunk the web tech class at Patan!</body>
• And finally the last line defines the end of the root element:
• </note>
• You can assume, from this example, that the XML document contains a note to Tulsi
from Giri.
• Thus, 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):
• <root>
• <child>
• <subchild>.....</subchild>
• </child>
• </root>
• Thus, 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):
• <root>
• <child>
• <subchild>.....</subchild>
• </child>
• </root>

• The terms parent, child, and sibling are used to describe the relationships
between
• elements. Parent elements have children. Children on the same level are called
siblings
• (brothers or sisters). All elements can have text content and attributes (just like in
HTML).
XML Syntax Rules
• The syntax rules of XML are very simple and logical. The rules are easy to learn, and
easy to use

• 1. All XML Elements Must Have a Closing Tag. In HTML, some elements may not
• have to have a closing tag, like;
• <p>This is a paragraph.
• <br>

• In XML, it is illegal to omit the closing tag. All elements must have a closing tag:
• <p>This is a paragraph.</p>
• <br />
• <hello> This is hello </hello>

• 2. 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:
• <Message>This is incorrect</message>
• <message>This is correct</message>
XML Syntax Rules

• 3. XML Elements Must be Properly Nested. In HTML, you might see


improperly nested elements:
• <b><i>This text is bold and italic</b></i>
• In XML, all elements must be properly nested within each other:
• <b><i>This text is bold and italic</i></b>

• 4. XML Documents Must Have a Root Element. XML documents must contain
one element that is the parent of all other elements. This element is called the
root
• element.
• <root>
• <child>
• <subchild>.....</subchild>
• </child>
• </root>
XML Syntax Rules

5. XML Attribute Values Must be Quoted. XML elements can have attributes
in
• name/value pairs just like in HTML. In XML, the attribute values must always be
• quoted. Study the two XML documents below. The first one is incorrect, the
• second is correct:
• <note date=06/01/2012>
• <to>Tulsi</to>
• <from>Giri</from>
• </note>
• <note date="06/01/2012">
• <to>Tulsi</to>
• <from>Giri</from>
• </note>
• The error in the first document is that the date attribute in the note element is not
• quoted.
XML Syntax Rules

6. Entity Reference. Some characters have a special meaning in XML. If


you place a character like "<" inside an XML element, it will generate
an error because the parser interprets it as the start of a new element. This
will generate an XML error:
• <message>if salary < 1000 then</message>
• To avoid this error, replace the "<" character with an entity reference:
• <message>if salary &lt; 1000 then</message>

• There are 5 predefined entity references in XML:


• &lt; < less than
• &gt; > greater than
• &amp; & ampersand
• &apos; ' apostrophe
• &quot; " quotation mark
XML Syntax Rules

• 7. Comments in XML. The syntax for writing comments in XML is


similar to that of
• HTML.
• <!-- This is a comment -->
• 8. White-space is preserved in XML. HTML truncates multiple
white-space
• characters to one single white-space:
• HTML: Hello Tulsi
• Output: Hello Tulsi
• With XML, the white-space in a document is not truncated.
XML Elements
• An XML document contains XML Elements. An XML element
is everything from (including) the element's start tag to (including)
the element's end tag.
• Example: <price>29.99</price>

• An element can contain:


• text
• attributes
• other elements
• or a mix of the above
XML Elements
• Consider an example;
• <bookstore>
• <book category="CHILDREN">
• <title>Harry Potter</title>
• <author>J K. Rowling</author>
• <year>2005</year>
• <price>29.99</price>
• </book>
• <book category="WEB">
• <title>Learning XML</title>
• <author>Erik T. Ray</author>
• <year>2003</year>
• <price>39.95</price>
• </book>
• </bookstore>

• In the example above, <bookstore> and <book> have element contents, because
they contain other elements. <book> also has an attribute (category="CHILDREN").
<title>, <author>, <year>, and <price> have text content because they contain text.
Empty XML Elements

• An element with no content is said to be empty.


• In XML, you can indicate an empty element like this:
• <element></element>
• You can also use a so called self-closing tag:
• <element />
XML Naming 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 periods
• Element names cannot contain spaces
• Any name can be used, no words are reserved (except
xml).
Best Naming Practices

• Make names descriptive. Names with an underscore separator are


nice:
• <first_name>, <last_name>.
• Names should be short and simple, like this: <book_title> not like this:
• <the_title_of_the_book>.
• Avoid "-" characters. If you name something "first-name," some software
may think you want to subtract name from first.
• Avoid "." characters. If you name something "first.name," some software may
think that "name" is a property of the object "first."
• Avoid ":" characters. Colons are reserved to be used for something called
namespaces (more later).
• XML documents often have a corresponding database. A good practice is to use
the
naming rules of your database for the elements in the XML documents.
• Non-English letters like éòá are perfectly legal in XML, but watch out for
problems
if your software vendor doesn't support them.
XML Attributes

• XML elements can have attributes, just like HTML. Attributes provide
additional information about an element. In HTML, attributes provide
additional information about elements:
• <img src="computer.gif">
• <a href="demo.asp">
• Attributes often provide information that is not a part of the data. In the
example below, the file type is irrelevant to the data, but can be important
to the software that wants to manipulate the element:
• <file type="gif">computer.gif
• </file>
XML Attributes
• Attribute values must always be quoted. Either single or double quotes can
be used. For a person's sex, the person element can be written like this:
• <person sex="male">
• or like this:
• <person sex='male'>
• If the attribute value itself contains double quotes you can use single quotes,
like in this
• example:
• <gangster name='Chota "Shotgun" Chetan'>
• or you can use character entities:
• <gangster name="Chota &quot;Shotgun&quot; Chetan">
XML Elements vs. Attributes
• Take a look at these examples:
• <person sex="male">
• <firstname>Jagdish</firstname>
• <lastname>Bhatta</lastname>
• </person>
• <person>
• <sex>male</sex>
• <firstname>Jagdish</firstname>
• <lastname>Bhatta</lastname>
• </person>
• In the first example sex is an attribute. In the last, sex is an element. Both
examples provide the same information. There are no rules about when to use
attributes or when to use elements. Attributes are handy in HTML. In XML my advice
is to avoid them. Use elements instead.
My Favorite Way
• The following three XML documents contain exactly the same information:
• A date attribute is used in the first example:
• <note date="2008-01-10">
  <to>Tove</to>
  <from>Jani</from>
</note>

• A <date> element is used in the second example:


• <note>
  <date>2008-01-10</date>
  <to>Tove</to>
  <from>Jani</from>
</note>

• An expanded <date> element is used in the third example: (THIS IS MY FAVORITE):


• <note>
  <date>
    <year>2008</year>
    <month>01</month>
    <day>10</day>
  </date>
  <to>Tove</to>
  <from>Jani</from>
</note>
Avoid XML Attributes?

• Some things to consider when using attributes are:


• attributes cannot contain multiple values (elements can)
• attributes cannot contain tree structures (elements can)
• attributes are not easily expandable (for future changes)
• Don't end up like this:

• <note day="10" month="01" year="2008"
to="Tove" from="Jani" heading="Reminder"
body="Don't forget me this weekend!">
</note>
XML Namespace

• XML Namespaces provide a method to avoid element name


conflicts.
• 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.
• This XML carries HTML table information:
• <table>
  <tr>
    <td>Apples</td>
    <td>Bananas</td>
  </tr>
</table>
XML Namespace

• This XML carries information about a table (a piece of furniture):


• <table>
  <name>African Coffee Table</name>
  <width>80</width>
  <length>120</length>
</table>
• If these XML fragments were added together, there would be a name
conflict. Both contain a <table> element, but the elements have different
content and meaning.
• A user or an XML application will not know how to handle these
differences.
Solving the Name Conflict Using a Prefix

• Name conflicts in XML can easily be avoided using a name prefix.


• This XML carries information about an HTML table, and a piece of furniture:
• <h:table>
  <h:tr>
    <h:td>Apples</h:td>
    <h:td>Bananas</h:td>
  </h:tr>
</h:table>

<f:table>
  <f:name>African Coffee Table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
</f:table>

• In the example above, there will be no conflict because the two <table> elements have
different names.
XML Namespaces - The xmlns Attribute

• The XML namespace is a special type of reserved XML attribute that you
place in an XML tag.
• The reserved attribute is actually more like a prefix that you attach to any
namespace you create. This attribute prefix is "xmlns:", which stands
for XML NameSpace. The colon is used to separate the prefix from your
namespace that you are creating.
• A namespace name is a uniform resource identifier (URI). Typically, the
URI chosen for the namespace of a given XML vocabulary describes a
resource under the control of the author or organization defining the
vocabulary, such as a URL for the author's Web server.
• However, the namespace specification does not require nor suggest that
the namespace URI be used to retrieve information; it is simply treated by an
XML parser as a string. For example, the document at
http://www.w3.org/1999/xhtml itself does not contain any code
XML Namespaces - The xmlns Attribute
• When using prefixes in XML, a so-called namespace for the prefix must be defined.
The
• namespace is defined by the xmlns attribute in the start tag of an element. The
namespace
• declaration has the following syntax. xmlns:pref

• <root>

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

<f:table xmlns:f="https://www.w3schools.com/furniture">
  <f:name>African Coffee Table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
</f:table>
XML Namespaces - The xmlns Attribute

• In the example above:


• The xmlns attribute in the first <table> element gives the h: prefix a
qualified namespace.
• The xmlns attribute in the second <table> element gives the f: prefix
a qualified namespace.
• When a namespace is defined for an element, all child elements with
the same prefix are associated with the same namespace.
Namespaces can also be declared in the XML root
element:
• <root xmlns:h="http://www.w3.org/TR/html4/"
xmlns:f="https://www.w3schools.com/furniture"> • Note: The namespace URI is
not used by the parser to look up
<h:table> information.
  <h:tr> • The purpose of using an URI is
    <h:td>Apples</h:td> to give the namespace a unique
    <h:td>Bananas</h:td> name.
  </h:tr> • However, companies often use
</h:table> the namespace as a pointer to a
web page containing namespace
<f:table> information.
  <f:name>African Coffee Table</f:name>
  <f:width>80</f:width>
  <f:length>120</f:length>
</f:table>

</root>
XML schema languages
• DTD
• XML Schema
Document Type Definition (DTD)

You might also like