Unit 7 XML
Unit 7 XML
Difference Between:
HTML XML
➢ It was designed for displaying data on ➢ It was designed to store and transport data.
web pages.
➢ It focuses on presentation of data. ➢ It focuses on structure and meaning of data.
➢ It uses predefined tags (ex: <div>, <h1>, ➢ It allows users to create custom tags (ex:
etc). <name>, <book>, <address>, etc).
➢ It is flexible with the syntax. ➢ It is strict with the syntax.
➢ It is not self-descriptive. ➢ It is self-descriptive with custom tags.
➢ It is not extensible, limited to ➢ It is extensible I.e. users can create any type
predefined tags. of necessary tags.
Example: Afai lekhne, ya thau pugdaina! Example: Afai lekhne, ya thau pugdaina!
XML Element:
XML elements are the building blocks of an XML document, defined by tags and used to store
data. An element consists of an opening tag, content, and a closing tag.
<name> Bishal </name>. Elements can contain text, attributes, other nested elements, or be
empty.
Tags are used to define XML elements. They indicate the start and end of an element and are
enclosed within angle brackets (< >). <name> is starting tag, </name> is closing tag from above
<name> element.
Attributes are best suited for meta-data (language, version, type), flags (active), and other small
descriptors (role, src, alt).
We should avoid the use of Attributes and use child elements because of the following reasons.
1. Attributes cannot hold complex or nested data. While, child elements can.
2. Overusing attributes can reduce the readability of the document.
3. Attributes provide limited flexibility. Changing attributes values in future is hard.
4. Attributes cannot describe the structure but child elements can.
Comments in XML:
Comments in XML are used to include notes or explanations within the code, which are ignored
by XML parsers. The syntax of XML comment is given as:
<! – – this is comment Text….. – – >
The first line of code is called XML declaration. We should always write this first line in all XML
documents. Version and encoding are the attributes that says we are using XML version 1.0 and
utf-8 encoding technique.
<rootElement> is the root element of the XML document. There is only one root element in
whole XML document. We can do nesting of elements as well as shown above.
XML namespace:
XML namespaces are a way to avoid element name conflicts in XML documents, especially when
combining elements from multiple XML vocabularies (I.e. elements from different schemas or
applications). A XML namespace is defined within the opening tag of an element using “xmlns”
attribute with a specific prefix. The value of this attribute is URI.
Syntax:
<rootElement xmlns:prefix = “https://example.com/prefix” >
…..
</rootElement>
The xmlns stands for XML namespace. The xmlspace is used to declare namespace. Prefix acts as
label for the namespace provided by the URI. This prefix is used before the element names to
indicate they belong to the specified namespace. “https://example.com/prefix” is a namespace
URI. For each namespace, this link and prefix should be unique.
Example:
Consider we have following XML tables. One is HTML table, and another is furniture table.
When, we try to combine these 2 tables within one XML document, name conflict arises. This
name conflict can be resolved using namespace as.
<rootElement>
<h:table xmlns:h="http://www.example.com/fruits">
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
Note:
Mathi ko example chai resolution of name conflict when combining 2 elements in one XML
document ko ho, if exam ma namespace and example matra aaye, first ma definition lekhne and
then HTML ko fruit list wala table ko code lekhnu and tyo table ko XML equivalent code
lekhdinu.. XML code chai yei mathi ko example dekhi lekhnu, tesko lagi: furniture wala code (5
lines) lai xodera sabai lekhnu….
Note:
When we write elements name inside parentheses (2nd line), it mean the element must contain
specified elements as child. When we write ‘#PCDATA’ inside parentheses, it means that the
element will contain text data but not any element as its child. If we want to perform element
nesting on child elements also, then we have to write elements name instead of ‘#PCDATA’.
Generally, if any element doesn’t have any child elements, we will use #PCDATA inside its
parentheses.
External DTD:
An External DTD is defined in a separate file, allowing it to be reused across multiple XML
documents. It is useful for larger projects or when the same structure needs to be applied to
multiple XML documents. The DTD is stored in a separate file with “.dtd” extension and is
referred in the XML document using “SYSTEM” or “PUBLIC” in the <! DOCTYPE …. > declaration.
DTD have many limitations such as lack of data types, no namespaces support, limited error
reporting, once element structure is defined; changing the structure is difficult especially in large
documents.
<library>
<book>
<title>XML Basics</title>
<author>John Doe</author>
<year>2021</year>
<price>29.99</price>
</book>
</library>
This block of code is written in “library.xml” file. And Here, the DTD file is referred in 2nd line
using SYSTEM “name_of_DTD_file”as.
Note:
In <! ELEMENT library (book*)>, we have written asterisk (*). This means, that the library root
element can have 0 or more book elements.
The Simple data types are used to define elements that contains text values without any nested
elements. The most common simple data types are given as:
xs:string, xs:integer, xs:float, xs:boolean, xs:date, xs:time, xs:dateTime.
The complex Type is used to define elements that contains other elements or attributes. An
element that contains nested elements or attributes is of “complex type”
To define complex type element in XML Schema, we use:
<xs:element name = ‘elementName’>
<xs:complexType>
…..
</xs:complexType>
</xs:element>
To define simple data type element in XML, we use:
<xs:element name = ‘elementName’ type= “simpleDataType”/>
</xs:schema>
Note:
<xs:sequence> is used because we want everything in sequence.
XML DOM:
The XML DOM is a standard for how to get, change, add, or delete XML elements. All XML
elements can be accessed through the XML DOM.
Example:
Consider in a Library, there are many books, with title and author. Write DTD + XML code and
XML Schema + XML code for it.
Ans:
The DTD and XML code are given as:
<?xml version="1.0" encoding="UTF-8"?>
<!- - DTD code - ->
<!DOCTYPE library [
<!ELEMENT library (book+)>
<!ELEMENT book (title, author)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
]>
<!- - XML code - ->
<library>
<book>
<title>DBMS </title>
<author >512 </author >
</book>
<book>
<title> DSA </title>
<author >600 </author >
</book>
</library>
Note:
<xs:element name="book" maxOccurs="unbounded">
In this line from above XML Schema, we have written maxOccurs attribute, this attribute specifies
how many time the ‘book’ element should be made, when its value is set to “unbounded’ we can
make 0 or more “book” elements. The default value of this attribute is 1,
So, if we don’t write this attribute, then only one book element can be made. Making more than
one book element will show an error.
Example:
Write an XML file representing book information, including attributes like title, author, ISBN,
publisher, edition, and price with a DTD to validate the XML file.
(8 marks qn, 2074 year)
Ans:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE library [
<!ELEMENT library (book+)>
<!ELEMENT book (title, author, ISBN, publisher, edition, price)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT ISBN (#PCDATA)>
<!ELEMENT publisher (#PCDATA)>
<!ELEMENT edition (#PCDATA)>
<!ELEMENT price (#PCDATA)>
]>
<library>
<book>
Example:
Create a XML document to store voter ID, name, address, and date of birth details. Also create
DTD to validate the XML document. (8 marks qn, 2075)
Ans:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE voters [
<!ELEMENT voters (voter+)>
<!ELEMENT voter (voterID, name, address, dateOfBirth)>
<!ELEMENT voterID (#PCDATA)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT address (#PCDATA)>
<!ELEMENT dateOfBirth (#PCDATA)>
]>
<voters>
<voter>
<voterID>20021 </voterID>
<name>Bishal Bhat </name>
<address>Dodhara chandani</address>
<dateOfBirth>2002-12-17</dateOfBirth>
</voter>
<voter>
<voterID>20022 </voterID>
<name>Sara Ali Khan </name>
<address>Dodhara chandani </address>
<dateOfBirth>2000-4-2</dateOfBirth>
</voter>
</voters>
Explaination:
The first line is XML is declaration which specifies the version of XML is being used and what
character encoding is being used. The second line <xs:schema> is the root element of XML
schema, which declares XML schema namespace, allowing the use of schema-specific elements
prefixed with xs (like xs:element, xs:complexType, etc.)
<xs:element> allows us to define a new element. <xs:complexType> means the element is of
complex type. <xs:sequence> means we want the child elements in sequence. <xs:string> refers
to simple data type.
Order Indicator:
Order indicator defines the sequence in which child elements should appear in the XML
document. The most common order indicator is <xs:sequence>.
Example:
<xs:element type = “person”>
<xs:complexType>
<xs:sequence>
<xs:element name= ‘firstName’ type=‘xs:string’ />
<xs:element name= ‘lastName’ type=‘xs:string’ />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="class">
<xs:complexType>
<xs:sequence>
<xs:element name="student" maxOccurs="3">
<!-- A maximum of 3 student elements -->
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string" />
<xs:element name="lastname" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Example:
Generate XML and XML schema. Class is a root Element, class has a child element Student,
student has 3 child elements: fname, lname, and age. The data types of fname and lname is
string and age is int. (12 marks, 2078)
Ans:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
Example:
Write an XML schema to describe “address” as an element and “city, state, street, and house
no” as its attributes. Write XML code also. (12 marks, 2079)
Ans:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="address"> <!-- Address element -->
<xs:complexType>
<xs:attribute name="city" type="xs:string"/>
<xs:attribute name="state" type="xs:string"/>
<xs:attribute name="street" type="xs:string"/>
<xs:attribute name="houseNo" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:schema>
Note:
Previouly we use <xs:element name= “elementName” type= “dataType”/>. Here, used
<xs:attribute name="state" type="xs:string"/> are same as element ones.
***