XML (BScCSIT 5th Semester)
XML (BScCSIT 5th Semester)
XML (BScCSIT 5th Semester)
• <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
• 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
• 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
• 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
• 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 "Shotgun" 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>
• <note day="10" month="01" year="2008"
to="Tove" from="Jani" heading="Reminder"
body="Don't forget me this weekend!">
</note>
XML Namespace
<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
</root>
XML schema languages
• DTD
• XML Schema
Document Type Definition (DTD)