Unit 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

What is XML

XML is the acronym for eXtensible Markup Language.

XML is simple text-based format. It is used to represent structured information such as


documents, catalogue, books, student records etc.

XML is a mark-up language based on tags within angled brackets, similar to HTML. Here tags
are not predefined. We need to specify our own tags. These tags are self-explanatory.

XML is designed to carry data, not to display it.

Characteristics of XML

There are many significant characteristics of XML that are beneficial for a range of systems.

i. Extensible: using XML, it is possible to create our own self-descriptive tags according
application need.
ii. carries the data, does not present it : XML allows you to store the data regardless of
how it will be displayed.
iii. Open standard: XML was created by the World Wide Web Consortium (W3C) and is
freely available as an open standard.
iv. Structured format:
It is possible to precisely determine the arrangement, organisation and expressing of
data in the file.
v. Validation: It is possible to validate XML file using document type declaration(DTD)
vi. Described format: In XML file, every piece of data has a name that is readable by both
humans and machines.
vii. discoverable format: it is possible to parse XML file and understand the structure and
relationships between the items
viii. platform independent: XML is a software and hardware-independent to send, receive
or store data.
ix. XML is a universal format.

Rules for XML Declaration/ XML document must meet these criteria:

i. XML document must begin with the XML declaration. Xml declaration start with <?xml
and end with ?>. XML declaration also contains various parameters. The sequence in
which the parameters are placed is crucial. Version, encoding, and standalone are in
the correct order.
ii. The XML declaration must be the first statement in the XML document.
iii. Elements can't overlap. In other words, element should properly nested.
iv. A document can have exactly one root element.
v. All attributes value must be put in single or double quotes
vi. Comments can’t be placed inside tags.
vii. Tag Names cannot start with a number or punctuation character. However, Tag Names
can contain letters, numbers, and other characters.
viii. Tag Names cannot contain spaces.
ix. Xml tags are case-sensitive. For example, <book>, <Book>, <BOOK> all are different.

XML syntax (Declaration, Tags, elements)


XML declaration:

XML declaration sets parameters for basic XML parsing. Itcontains various parameters. The
syntax of XML declaration is :

<?xml

version = "version_information"

encoding = "encoding_declaration"

standalone = "standalone_status"

?>

Here,

Parameter Description Value

Version Specifies the XML version 1.0

Encoding It specify encoding method used in the document. UTF-8, UTF-16, ISO-10646-UCS-
The default encoding is UTF-8. 2, ISO-10646-UCS-4, ISO-8859-1
to ISO-8859-9

Standalone It tells the XML parser whether the document's Yes, no


content is based on data from an external source.
The value is set to "no" by default. The value "yes"
indicates no external declarations are necessary
to parse the page.

Declaration examples:

Example 1: Following line declare XML document with version 1.0.

<?xml version = "1.0" ?>

Example 2: Following line declare XML document with version 1.0, encoding UTF-8 and
standalone = no.
<?xml

version = "1.0"

encoding = "UTF-8"

standalone = "no" ?>

Example 3: Following example show XML declaration with all parameters defined in single
quotes

<?xml version = '1.0' encoding = 'UTF-8' standalone = 'no' ?>

XML Tags

XML tags are the building blocks of an XML document. In XML, XML tags specify the scope of
an element. They can also be used to add comments and specify environment-specific
parameters.

XML Tag and HTML tags are similar, but XML is more flexible then HTML. HTML tags are
predefined while XML tags are user defined.

(i) Starting Tag

Every XML element is marked by a start-tag. It is specified using tag name within angular
brackets (< >) . For example:

<book>

(ii) Ending Tag

Every element has end-tag. It is specified using "/" before the name of an element in
angular brackets. For example:

</book>

(iii) Empty Tag

An element which has no content is called as empty. It is denoted by in two ways:

(1) Nothing between staring tag and closing tag. In other words, start-tag immediately
followed by an end-tag: For example

<book> </book>

(b) tag name end with "/" within angular brackets. For example:

<book />
(iv) root tag:

There is exactly one root element in any XML document. All of the other elements are
enclosed within it. The root element in HTML is the < html> element, while root element
can be anything in an XML file.

XML elements:
XML elements are the building blocks of an XML document. The XML Element is collection of
opening-tag, data and closing-tag. i.e. it contain everything from the element's start tag to
the element's end tag. Each XML document contains one or more elements.

Elements contain following:


• text
• attributes
• other elements
• mix of the above
Example of XML elements:
Following is the examples of XML element:
<title>Advanced in web technology </title>
<publisher issdn="101175" >Oxford </publisher>
<street_name> bari faliya </street_name>

XML document
An XML document consists of a prolog and body:

(i) prolog:

It includes an XML declaration and an optional reference to external structuring


documents

(ii)body:

Body consist of a number of elements.

(i) Document Prolog Section

Prolog Section is first and most important part of an XML Document. It indicate that
document is marked up in XML.

XML Prolog includes


• XML declaration,
• DOCTYPE and comments,
• information about the character encoding used, processing instructions,
• reference to either a document type definition (DTD) or XML Schema
The prolog is an optional section of the XML document. If a prolog section is included in an
XML document, it must appear before the root element.

This section contains two parts:

i. XML declaration. (it is discussed in previous section)

ii. Document Type Definition (DTD): provide advanced settings


These both parts are optional.

Example of prolog:

Following example illustrates the prolog:


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE booklist SYSTEM "booklist.dtd">

XML
declaration

<?xml version="1.0" encoding="UTF-8" ? > prolog


[…]

<book> root element

<title>Advanced in web technology </title>


Body
<author> jmp </author>
<publisher issdn="101175" Oxford </publisher>
</book>

Attribute: name value

Figure 1.1: example of XML document

Document Type Definition (DTD)


Document Type definition is used to specify the set of rules or instructions for current XML
Document structure as per the requirement. Set of rules or instructions can be defined in a
separate file and this file can be used to validate XML document.

DTD define entities or default attribute values. It supports validation, a special mode of
parsing that checks grammar and vocabulary of markup.

Example of XML document


Example 1:
<?xml version="1.0" ?>

<Color>
<!-- this is comment -->
<Option1 Name='Blue'/>
<Option Name="Yellow"/>
<Option Name="Crimson"/>
</Color>

Example 2:
<?xml version="1.0" ?>
<address>
<street_no> 10 </street_no>
<street_name> bari faliya </street_name>
<district> bharuch </district>
</address>

Example 3: nested element


<?xml version="1.0" ?>
<address>
<street_info>
<street_name>bari faliya</street_name>
<district> bharuch</district>
</street_ info>
</address>

Questions:

1. What is XML?
2. Write Characteristic XML.
3. Write note on XML document prolog section.
4. Write note on XML document element section
5. Write rules of XML declaration with example

MCQ

Select correct answer.


1. XML stand for:
A. eXtra Markup Language B. eXtensible Markup Language
C. Example Markup Language D. X-Markup Language
Ans: B

2. Which statement is true about XML?


A. All XML elements must have a closing tag
B. All XML elements must be lower case
C. All XML documents must have a DTD
D. All given
Ans: A

3. correct syntax of defining the XML version is:


A. <xml version="version_no" />
B. <?xml version=" version_no " ?>
C. <?xml version=" version_no " />
D. None of the above
Ans: B

4. How to give Comment in XML document?


A. <?-- -->
B. <!-- --!>
C. <!-- -->
D. </-- -- >
Ans: C

5. DTD stand for:


A. Direct Type Definition
B. Document Type Definition
C. Document Type Declaration
D. Dynamic Type Definition
Ans: B

6. Which of the following XML is correct?


A. <myElement myAttribute="some_Value"/>
B. <myElement myAttribute='some_Value'/>
C. <myElement myAttribute=’some_Value">
D. <myElement myAttribute="some_Value’/>
Ans: A

7. Can attributes of XML element have multiple values?


A. Yes B. No

Ans: B

8. Which of the following XML fragments are well-formed?


A. <myElement myAttribute="some_Value"/>
B. <myElement myAttribute='some_Value'/>
C. <myElement myAttribute=’some_Value">
D. <myElement myAttribute="some_Value’/>
Ans: A

9. How to add the attribute named Title to the <book> tag?


A. <book attribute Title =”advanced web”>
B. <book Title attribute =” advanced web”>
C. <book Title attribute_type=” advanced web”>
D. <book Title =” advanced web” >
Ans: D

10. XML tags are Case Sensitive:


A. true B. false
Ans: A true

11. it easier to process XML than HTML


A. true B. false
Ans: A true

12. XML document is Well formed if _______


A. contain an comment
B. it contains a root element
C. it contains one or more elements
D. must contain one or more elements and root element must contain all other elements
Ans D

13. Which statement is true about XML?


A. All XML elements must be properly closed
B. All XML elements must be lower case
C. All XML documents must have a DTD
D. All given
Ans: A

14. Which statement is NOT true?

A. White-space is not preserved in XML


B. XML elements can be empty
C. XML elements must be properly nested
D. XML documents must have a root tag
Ans: A

15. XML preserves white spaces: true or false?


A. true B. false
Ans: A. true

16. XML elements cannot be empty


A. true B. false
Ans: B

17. XML attribute values must always be enclosed in quotes


A. true B. false
Ans: A. true

18. What is the correct syntax of the declaration which defines the XML version?
A. <?xml version="1.0"?>
B. <xml version="1.0" />
C. <?xml version="1.0" />
D. <xml version="1.0" ?>

Ans: A. <?xml version="1.0"?>

19. Can you say following is a "well formed" XML document?


<?xml version="1.0"?>
<book>
<title>advanced web</ title >
<author>JMP</author >
</book>

A. yes B. no
Ans: A. yes

21. Which is not a correct name for an XML element?


A. <book name>
B. <Age>
C. <NAME>
D. None
Ans: A. <book name>

You might also like