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

Use Only: Introduction To XML

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

y

nl
O
se
Module 1

rU
te
en
C
Introduction to XML
h
ec
pt
rA
Fo
Module Overview

y
nl
O
In this module, you will learn about:

se
 Introduction to XML

rU
 Exploring XML

te
 Working with XML

en
 XML Syntax

C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 2 of 55


Lesson 1 – Introduction to XML

y
nl
O
In this first lesson, Introduction to XML, you will

se
learn to:

rU
 Outline the features of markup languages and list

their drawbacks.

te
 Define and describe XML.

en
 State the benefits and scope of XML.
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 3 of 55


Features of Markup Languages 1-3

y
nl
O
 Generalized Markup Language (GML) helps the

se
documents to be edited, formatted, and searched by

rU
different programs using its content-based tags.

te
en
 Standard Generalized Markup Language (SGML) is a
coding scheme for developing specialized markup
languages. C
h
ec
pt

 Hyper Text Markup Language (HTML) is used for


rA

sharing information by using hyperlinked text


documents.
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 4 of 55


Features of Markup Languages 2-3

y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 5 of 55


Features of Markup Languages 3-3

y
nl
Features

O
 GML describes the document in terms of its format,

se
structure and other properties.

rU
 SGML ensures that system can represent the data

te
in its own way.

en
 HTML used ASCII text, which allows the user to

C
use any text editor.
h
ec

Drawbacks
pt

 GML and SGML were not suited for data


rA

interchange over the web.


 HTML instructions is used to display content rather
Fo

than content they encompass.


@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 6 of 55
Evolution of XML 1-2

y
nl
O
 XML is a W3C recommendation.

se
rU
 It is a set of rules for defining semantic tags that
break a document into parts.

te
en

C
XML was developed over HTML.
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 7 of 55


Evolution of XML 2-2

y
nl
O
HTML XML

se
HTML was designed to display XML was designed to carry

rU
data. data.
HTML displays data and XML describes data and

te
focuses on how data looks. focuses on what data is.

en
HTML displays information. XML describes information.
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 8 of 55


Features of XML

y
nl
O
 XML stands for Extensible Markup Language

se
 XML is a markup language much like HTML

rU
 XML was designed to describe data

te
XML tags are not predefined

en

XML uses a Document Type Definition (DTD)



C
or an XML Schema to describe data
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 9 of 55


XML Markup 1-2

y
nl
O
 XML markup defines the physical and logical layout of

se
the document.

rU
XML markup divides a document into separate

te

information containers called elements.

en
C
A document consists of one outermost element,
h

ec

called root element that contains all the other


elements, plus some optional administrative
pt
rA

information at the top, known as XML declaration.


Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 10 of 55


XML Markup 2-2

y
nl
O
Code Snippet

se
<?xml version=”1.0” encoding=”iso-8859-1” ?>

rU
- <FlowerPlanet>
<Name>Rose</Name>

te
<Price>$1</Price>

en
<Description>Red in color</Description>
<Number>700</Number>

C
</FlowerPlanet> h
where,
ec

<Name>, <Price>,<Description> and <Number> tags are


elements
pt

<FlowerPlanet> and </FlowerPlanet> are the root elements


rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 11 of 55


Benefits of XML

y
nl
Data independence - separates the content from its presentation

O

se
 Easier to parse - absence of formatting instructions makes it easy to

rU
parse

te
 Reducing Server Load - semantic and structural information enables it

en
to be manipulated by any application, can now be performed by clients

processing tools C
Easier to create – can easily create with the most primitive text
h
ec

 Web Site Content – can be transformed into a number of other formats


pt
rA

 Remote Procedure Calls – protocol that allows objects on one computer


to call objects on another computer
Fo

 E-Commerce - can be used as an exchange format


@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 12 of 55
Lesson 2 – Exploring XML

y
nl
O
In this second lesson, Exploring XML, you will learn

se
to:

rU
 Describe the structure of an XML document.

 Explain the lifecycle of an XML document.

te
en
 State the functions of editors for XML and list the

popularly used editors.


C
 State the functions of parsers for XML and list names
h
ec

of commonly used parsers.


pt

 State the functions of browsers for XML and list the


rA

commonly used browsers.


Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 13 of 55


XML Document Structure 1-4

y
nl
O
 The two sections of an XML document are:

se
 Document Prolog

rU
 Root Element

te
 An XML document consists of a set of unambiguously

en
named "entities".

C
Every document starts with a "root" or document
entity. All other entities are optional.
h
ec

 A single entity name can represent a large amount of


pt

text. The alias name is used each time some text is


referenced and the processor expands the contents
rA

of the alias.
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 14 of 55


XML Document Structure 2-4

y
nl
O
Document Prolog

se
 Document prolog contains metadata and consists of two parts -
XML Declaration and Document Type Declaration.

rU
 XML Declaration specifies the version of XML being used.

te
 Document Type Declaration defines entities’ or attributes’ values

en
and checks grammar and vocabulary of markup.

Root Element C
h
Also called a document element.
ec

 It must contain all the other elements and content in the


pt

document.
rA

 An XML element has a start tag and end tag.


Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 15 of 55


XML Document Structure 3-4

y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 16 of 55


XML Document Structure 4-4

y
nl
O
Code Snippet

se
<?xml version=”1.0” encoding=”iso-8859-1”?>

rU
<!DOCTYPE Music_Library [
<!ELEMENT Music_Library (Title,Artist,Country,Price,Year)>
<!ELEMENT Title (#PCDATA)>
<!ELEMENT Artist (#PCDATA)>

te
<!ELEMENT Country (#PCDATA)>
<!ELEMENT Price (#PCDATA)>

en
<!ELEMENT Year (#PCDATA)>
<!ENTITY MS “Thatz life”>

C
]>
<Music_Library>
<Title>YLO</Title>
h
<Artist>&MS; - Jenny Dan</Artist>
ec

<Country>Germanyo</Country>
<Price>$12</Price>
pt

<Year>2002</Year>
</Music_Library>
rA

where,
The first block indicates xml declaration and document type declaration. Music_Library is
Fo

the root element.

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 17 of 55


Logical Structure 1-2

y
nl
The logical structure gives information about the elements and the

O

order in which they are to be included in the document.

se
It shows how a document is constructed rather than what it contains.

rU

Document Prolog forms the basis of the logical structure of the XML

te

document.

en
XML Declaration and Document Type Definition are its components.
C
 h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 18 of 55


Logical Structure 2-2

y
nl
O
Code Snippet

se
<?xml version=”1.0” encoding=”iso-8859-1” ?>

rU
te
Code Snippet

en
<!DOCTYPE Music_Library SYSTEM
“Mlibrary.dtd”>
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 19 of 55


XML Document Life Cycle

y
nl
O
1. XML document creation

se
2. Scanning

rU
3. Parsing

te
4. Access

en
5. Conversion into Application program
6. ModificationC
h
7. Serialization
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 20 of 55


Editors

y
nl
O
Main Functions:
Add opening and closing tags to the code

se

 Check for validity of XML

rU
 Verify XML against a DTD/Schema
Perform series of transforms over a document

te

 Color the XML syntax

en
 Display the line numbers

Complete the word


C
Present the content and hide the code
h

ec

Some popularly used editors are:


XMLwriter
pt

XML Spy
rA

 XML Pro
Fo

 XMLmind
 XMetal
@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 21 of 55
Parsers 1-2

y
nl
An XML parser:

O

Reads the document

se

 Verifies it for its well-formedness

rU
 After verification, converts it into a tree of elements

te
 Commonly used parsers:

en
 Crimson, Xerces, Oracle XML Parser, JAXP, MSXML

C
h
 Type of parsers:
ec

 Non Validating parser


pt

 Validating parser
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 22 of 55


Parsers 2-2

y
nl
O
Non Validating parser

se
 It checks the well-formedness of the document.

rU
 Reads the document and checks for its conformity

te
with XML standards.

en
Validating parser C
h
 It checks the validity of the document using DTD.
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 23 of 55


Browsers

y
nl
O
 Web browsers can format XML data and display it to

se
the user.

rU
 Other programs like database, Musical Instrument
Digital Interface (MIDI) program or a spreadsheet

te
program may present XML data accordingly.

en
C
Commonly used web browsers:
h

ec

 Netscape, Mozilla, Internet Explorer, Firefox, Opera


pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 24 of 55


Lesson 3 – Working with XML

y
nl
O
In this third lesson, Working with XML, you will learn

se
to:

rU
 Explain the steps towards building an XML document.

 Define what is meant by well-formed XML.

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 25 of 55


Creating an XML document

y
nl
O
 An XML document has three main components:

se
 Tags(markup) and text(content)

rU
 DTD or Schema
Formatting or display specifications

te

en
The steps to build an XML document are as follows:
C

1. Create an XML document in an editor


h
2. Save the XML document
ec

3. Load the XML document in a browser


pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 26 of 55


Exploring the XML document 1-3

y
nl
O
 Various building blocks of an XML document:

se
 XML Version Declaration

rU
 Document Type Definition

te
 Document instance in which the content is defined

en
by the mark up
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 27 of 55


Exploring the XML document 2-3

y
nl
O
se
rU
te
Characters are encoded using
various encoding formats. The

en
character encoding is declared in
encoding declaration.

C XML declaration tells the version of


h
XML, the type of character
encoding being used and the
ec

markup declaration used in the


XML document.
pt
rA

XML declaration should start with


the five character string, <?xml. It
indicates that the document is an
Fo

XML document.

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 28 of 55


Exploring the XML document 3-3

y
nl
O
se
rU
Indicates the presence of external

te
markup declarations. “Yes”
indicates that there are no

en
external markup declarations and
“no” indicate that external markup

C
declarations might exist.

Declares and defines the elements


h
used in the document class.
ec
pt

Defines the content of the XML


document.
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 29 of 55


Meaning in Markup

y
nl
O
 Structure

se
 Semantic

rU
 Style

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 30 of 55


Well-formed XML document 1-3

y
nl
O
 XML tags should be valid

se
 Length of the tags depend on the XML

rU
processors

te
 XML attributes should be valid

en
 XML documents should be verified
C
Minimum of one element is required
h

ec

 XML tags are case sensitive


pt

 Every start tag should end with end tag


rA

 XML tags should be nested properly


Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 31 of 55


Well-formed XML document 2-3

y
nl
O
Code Snippet

se
rU
<Book>
<Name>Good XML</Name>

te
<Cost>$20</Cost>
</Book>

en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 32 of 55


Well-formed XML document 3-3

y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 33 of 55


Lesson 4 – XML Syntax

y
nl
O
In this last lesson, XML Syntax, you will learn to:

se
 State and describe the use of comments and

rU
processing instructions in XML.
 Classify character data that is written between tags.

te
en
 Describe entities, DOCTYPE declarations and

attributes.
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 34 of 55


Comments 1-3

y
nl
O
 Used for the people to give information about the code

se
 Usually made for the content

rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 35 of 55


Comments 2-3

y
nl
O
 Rules:

se
 Comments should not include “-” or “

rU
 Comments should not be placed within a tag or

entity declaration

te
en
 Comments should not be placed before the XML

declaration
C
 Comments can be used to comment the tag sets
h
ec

 Comment should not be nested


pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 36 of 55


Comments 3-3

y
nl
O
Code Snippet

se
rU
<Name NickName=’John’>
<First>John</First>

te
<!--John is yet to pay the term fees-->
<Last>Brown</Last> <Semester>Final</Semester>

en
</Name>

C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 37 of 55


Processing Instructions 1-2

y
nl
O
 The main objective is to present some special

se
instructions to the application.

rU
 All the processing instructions should begin with an
identifier called target.

te
en
Syntax

<?PITarget <instruction>?>
C
h
ec

where,
pt
rA

PITarget is the name of the application that should receive the processing
instructions.
Fo

<instruction> is the instruction for the application

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 38 of 55


Processing Instructions 2-2

y
nl
O
Code Snippet

se
rU
<Name NickName=’John’>
<First>John</First>
<!--John is yet to pay the term fees-->

te
<Last>Brown</Last>

en
<?feesprocessor SELECT fees FROM
STUDENTFEES?>

C
<Semester>Final</Semester>
</Name>
h
ec

where,
pt

feesprocessor is the name of the application that receives the


rA

processing instruction

SELECT fees FROM STUDENTFEES is the instruction.


Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 39 of 55


Classification of character data

y
nl
O
 Character data describes the document’s actual

se
content with the white space.

rU
 The character data can be classified into:

te
 Character Data (CDATA)

en
 Parsed Character Data (PCDATA)

C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 40 of 55


PCDATA 1-2

y
nl
O
 The data that is parsed by the parser is called as

se
PCDATA.

rU
 The main objective is to present some special
instructions to the application.

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 41 of 55


PCDATA 2-2

y
nl
O
se
Code Snippet

rU
<Name nickname=’John’>

te
<First>John</First>

en
<!--John is yet to pay the term fees-->
<Last>Brown</Last>

C
<Semester>Final> 10 & <20</Semester>
</Name>
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 42 of 55


CDATA

y
nl
O
 CDATA part begins with a “<![CDATA[“ and ends

se
with “]]>”.

rU
 CDATA sections cannot be nested.

te
en
Code Snippet

C
<?xml version=”1.0” standalone=”yes”?>
h
<Svg>
ec
<Desc>Three shapes</Desc>
<Para>But the formula was <![CDATA[if (&x1 +
pt

&x2)]]> which
resulted in 7.</Para>
rA

</Svg>>
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 43 of 55


Entities 1-3

y
nl
O
 XML document is made up of large amount of information

se
called as entities.

rU
 Every entity consists a name and a value.

te
 Entity reference consists of an ampersand (&), the entity

en
name, and a semicolon (;).

C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 44 of 55


Entities 2-3

y
nl
O
Predefined Description Output

se
Entity

rU
&lt; produces the left angle <
bracket

te
&gt; produces the right angle >

en
bracket
&amp; C
produces the ampersand &
h
&apos; produces a single quote ‘
ec

character
pt

&quot; produces a double quote “


rA

character
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 45 of 55


Entities 3-3

y
nl
O
Code Snippet

se
<?xml version=”1.0”?>

rU
<!DOCTYPE Letter [
<!ENTITY address “15 Downing St Floor 1”>
<!ENTITY city “New York”>

te
]>

en
<Letter>
<To>&quot;Tom Smith&quot;</To>

C
<Address>&address;</Address>
<City>&city;</City>
h
<Body>
ec

Hi! How are you?


The sum is &gt; $1000
pt

</Body>
rA

<From>ARNOLD</From>
</Letter>
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 46 of 55


Entity Categories 1-3

y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 47 of 55


Entity Categories 2-3

y
nl
O
 General Entity

se
These entities are used within the document

rU

content.

te
Code Snippet

en
C
<<!ENTITY % ADDRESS “text that is to be
represented by an entity”>
h
ec

A well-formed parameter entity will look like a general entity, except that it will
include the “%” specifier.
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 48 of 55


Entity Categories 3-3

y
nl
O
 Parameter Entity

se
These entities are used only in the DTD.

rU

te
Code Snippet

en
C
<!DOCTYPE MusicCollection [
<!ENTITY R “Rock”>
h
<!ENTITY S “Soft”>
ec

<!ENTITY RA “Rap”>
<!ENTITY HH “Hiphop”>
pt

<!ENTITY F “Folk”>
rA

]>
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 49 of 55


DOCTYPE declarations 1-3

y
nl
O
 DTD File:

se
rU
Syntax

te
en
<! DOCTYPE name_of_root_element
SYSTEM “URL of the external DTD subset” [

]> C
Internal DTD subset
h
ec

where,
pt

name_of_root_element is the name of the root element


rA

SYSTEM is the url where the DTD is located


[Internal DTD subset] are declarations in the document
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 50 of 55


DOCTYPE declarations 2-3

y
nl
O
Code Snippet

se
rU
<?xml version=”1.0” encoding=”ISO-8859-1”?>
<!DOCTYPE program SYSTEM “HelloJimmy.dtd”>

te
<Program>

en
<Comments>
This is a simple Java Program. It will display

C
the message “Hello Jimmy,How are you?” on
execution.
h
</Comments>
ec

<Code> public static void main(String[] args) {


System.out.println(“Hello Jimmy,How are you?”);
pt

// Display the string.


}
rA

}
</Code>
Fo

</Program>

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 51 of 55


DOCTYPE declarations 3-3

y
nl
O
 Document Type Definition defines the elements in the document

se
rU
<!ELEMENT Program (comments, code)>
<!ELEMENT Comments (#PCDATA)>

te
<!ELEMENT Code (#PCDATA)>

en
Output:
C
 h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 52 of 55


Attributes

y
nl
O
 Attributes are case sensitive and must start with a

se
letter or underscore.

rU
te
Syntax

en
<elementName attName1=”attValue2”

C
attName2=”attValue2”...>
h
Code Snippet
ec

<?xml version=”1.0” ?>


pt

<Player Sex=”male”>
rA

<FirstName>Tom</FirstName>
<LastName>Federer</LastName>
Fo

</Player>

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 53 of 55


Summary 1-2

y
nl
O
 Introduction to XML

se
 XML was developed to overcome the drawbacks of earlier
markup languages.

rU
 XML consists of set of rules that describe the content to
be displayed in the document.

te
 XML markup contains the content in the information

en
containers called as elements.

Exploring XML
C
h

ec

 XML is divided into two parts namely, document prolog


and root element.
pt

 An XML editor creates the XML document and the parser


rA

validates the document.


Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 54 of 55


Summary 2-2

y
nl
O
 Working with XML

se
 XML document is divided into XML Version Declaration,
DTD and the document instance in which the markup

rU
defines the content.
 The XML markup is again categorized into structural,

te
semantic and stylistic.
 The output of the XML document is displayed in the

en
browser if it is well formed.

XML Syntax C
h

 Comments are used in the document to give information


ec

about the line or block of code.


pt

 The content in XML document is divided into markup and


character data.
rA

 The entities in XML are divided into general entities and


parameter entities.
Fo

 A DTD can be declared either internally or externally.

@ Aptech Limited Modern Markup for Data Interchange/ Module 1/ 55 of 55


Fo
rA
pt
ec
h
C
en
te
rU
Module 2

se
Namespaces

O
nl
y
Module Overview

y
nl
O
In this module, you will learn about:

se
 XML Namespaces

rU
 Working with Namespaces Syntax

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 2/ 2 of 20


Lesson 1 – XML Namespaces

y
nl
O
In this first lesson, XML Namespaces, you will

se
learn to:

rU
 Identify the need for a namespace.

 Define and describe namespaces in XML.

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 2/ 3 of 20


Duplicate Element Names

y
nl
O
 It allows developers to create their own elements

se
and attributes for their own projects.

rU
 Developer has to ensure the uniqueness of the
element names and attributes in a document.

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 2/ 4 of 20


Consequences of Duplicate Element Names

y
nl
O
 Name conflicts are inevitable from different

se
developers.

rU
 It is difficult for the browser to distinguish a
conflicting element.

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 2/ 5 of 20


Namespaces

y
nl
O
 Elements are distinguished by using namespaces.

se
A namespace is a collection of names.

rU

Namespaces allow the browser to:

te

en
 Combine documents from different sources
 C
Identify the source of elements or attributes
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 2/ 6 of 20


Lesson 2 – Working with Namespaces Syntax

y
nl
O
In this last lesson, Working with Namespaces

se
syntax, you will learn to:

rU
 Explain the syntax for XML namespaces.

 Discuss attributes and namespaces.

te
en
 Discuss how to use default namespaces.

C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 2/ 7 of 20


Prefixing element names

y
nl
O
 Prefixes in element names provide a means to

se
prevent name collisions.

rU
Code Snippet

te
en
<CD:Title> Feel </CD:Title>
and
C
<Book:Title> Returning to Earth </Book:Title>.
h
In the above example, both CD and Book are namespace prefixes.
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 2/ 8 of 20


Problems Posed by Prefixes

y
nl
O
 Duplication would still exist if prefixes are not unique

se
 To solve this problem, each namespace prefix is

rU
added to a Uniform Resource Identifier (URI)

te
en
Code Snippet

C
<S:Student xmlns:S=” http://www.spectrafocus.com
h
/student/”>
ec

<S:First>John</S:First>
<S:Last>Dewey</S:Last>
pt

<S:Title>Student</S:Title>
rA

</S:Student>
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 2/ 9 of 20


Namespace Syntax 1-3

y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 2/ 10 of 20


Namespace Syntax 2-3

y
nl
O
NamespacePrefix

se
 Used as a reference to the namespace

rU
 Prefixes must not begin with xmlns or xml
ElementName

te
en
 Specifies the name of the element

xmlns C
h
ec

 xmlns stands for XML namespace


pt

URI
rA

 URI is a string of characters which identifies an Internet


Fo

Resource

@ Aptech Limited Modern Markup for Data Interchange/ Module 2/ 11 of 20


Namespace Syntax 3-3

y
nl
O
Code Snippet

se
rU
<Auc:Books
xmlns:Auc=”http://www.auction.com/books”
xmlns:B=”http://www.books.com/HTML/1998/xml1”>

te
...

en
<Auc:BookReview>
<B:Table>
...
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 2/ 12 of 20


Placing attributes in a Namespace 1-3

y
nl
O
 Attributes belong to particular elements

se
 They are not a part of namespace, even if the

rU
element is within some namespace
If an attribute has no prefix, it has no namespace

te

en
 An attribute without a prefix is in default namespace
 C
If an attribute name has a prefix, its name is in the
h
namespace indicated by the prefix
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 2/ 13 of 20


Placing attributes in a Namespace 2-3

y
nl
O
Syntax

se
<Catalog xmlns:Book =

rU
“http://www.aptechworldwide.com”>
<Book:Booklist>

te
<Book:Title Book:Type = “Fiction”>Evening in
Paris</Book:Title>

en
<Book:Price>$123</Book:Price>
</Book:Booklist>
</Catalog>
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 2/ 14 of 20


Placing attributes in a Namespace 3-3

y
nl
O
Code Snippet

se
prefix:localname=’value’

rU
or
prefix:localname=”value”

te
where,

en
prefix is used as a reference to the namespace. Prefixes must
not begin with xmlns or xml
C
localname is the name of an attribute
h
value mentions a user defined value for an attribute
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 2/ 15 of 20


Default Namespaces 1-2

y
nl
O
MathML Document

se
 XML-based markup language to represent complex
mathematical expressions

rU
 Comes in two types:

te
As a markup language for presenting the layout of mathematical

en

expressions

the formula C
As a markup language for presenting the mathematical content of
h
ec

Code Snippet
pt

<MRow>
rA

<Mi>x</Mi>
<Mo>+</Mo>
Fo

<Mn>1</Mn>
</MRow>

@ Aptech Limited Modern Markup for Data Interchange/ Module 2/ 16 of 20


Default Namespaces 2-2

y
nl
O
Syntax

se
<elementName xmlns=’URL’>

rU
where,
elementName specifies the name of the element belonging to the same
namespace

te
URL specifies the namespace which is reference for a document or an HTML
page on the Web

en
Code Snippet
C
h
<Catalog xmlns:Book = “http://www.aptechworldwide.com”>
ec

<Book:Booklist>
<Book:Title Book:Type = “Fiction”>Evening in
pt

Paris</Book:Title>
<Book:Price>$123</Book:Price>
rA

</Book:Booklist>
</Catalog>
Fo

A default namespace using the xmlns attribute with a URI as its value

@ Aptech Limited Modern Markup for Data Interchange/ Module 2/ 17 of 20


Override Default Namespaces 1-2

y
nl
O
 Default namespace applies to the element on which it

se
was defined and all descendants of that element

rU
 New namespace definition overrides the previous one

te
and becomes the default for that element

en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 2/ 18 of 20


Override Default Namespaces 2-2

y
nl
O
Code Snippet

se
<Catalog xmlns = “http://www.aptechworldwide.com”>

rU
<Book>
<Title type = “Fiction”>Evening in Paris</Title>
<Price>$123</Price>

te
</Book>

en
<Book>
<Title type = “Non-Fiction”>Return to Earth</Title>

C
<Price xmlns = “http://www.aptech.ac.in”>$23</Price>
<Title type = “Non-Fiction”>Journey to the center of
h
the Moon</Title>
ec

<Price>$123</Price>
</Book>
pt

</Catalog>
rA

This namespace of price element applies only to it and overrides the


Fo

namespace in the catalog element

@ Aptech Limited Modern Markup for Data Interchange/ Module 2/ 19 of 20


Summary

y
nl
O
 XML Namespaces

se
 Namespaces distinguish between elements and attributes

with the same name from different XML applications.

rU
 It is a collection of names that can be used as element

te
names or attribute names in XML document.

en
 XML namespaces provide a globally unique name for a

element or attribute to avoid name collisions.


 C
Working with Namespaces syntax
h
 Namespaces are declared by an xmlns attribute whose
ec

value is the URI of the namespace.


pt

 If an attribute name has no prefix, it has no namespace.


rA

 A default namespace is used by an element and its child

elements if the element does not have a namespace prefix.


Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 2/ 20 of 20


Fo
rA
pt
ec
h
C
en
te
DTDs
rU
Module 3

se
O
nl
y
Module Overview

y
nl
O
In this module, you will learn about:

se
 Document Type Definition

rU
 Working with DTDs

te
 Valid XML Documents

en
 Declarations

C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 2 of 37


Lesson 1 – Document Type Definition

y
nl
O
In this first lesson, Document Type Definition, you

se
will learn to:

rU
 Define what is meant by a DTD.

 Identify the need for a DTD.

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 3 of 37


Definition of a DTD

y
nl
O
 It is a non XML document made up of element,

se
attribute and entity declarations.

rU
 It helps XML parsers to validate the XML document.

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 4 of 37


Need for a DTD

y
nl
O
 XML allows a user to define his/her own tag.

se
 Standardization of elements and attributes was

rU
needed.
A DTD can define all the possible combinations and

te

sequences for elements.

en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 5 of 37


Lesson 2 – Working with DTDs

y
nl
O
In this second lesson, Working with DTDs, you will

se
learn to:

rU
 Describe the structure of a DTD.

 Explain how to create a simple DTD.

te
en
 Describe what is meant by document type declarations.

C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 6 of 37


Structure of DTD

y
nl
O
 Element Declarations

se
 Attribute Declarations

rU
 Entity Declarations

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 7 of 37


Creating Internal DTDs 1-3

y
nl
O
 Declare all the possible elements

se
 Specify the permissible element children, if any

rU
 Set the order in which elements must appear

te
 Declare all the possible element attributes

en
 Set the attribute data types and values

C
Declare all the possible entities
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 8 of 37


Creating Internal DTDs 2-3

y
nl
O
Syntax

se
rU
<!ELEMENT element-name (element-content)>

te
<!ATTLIST element-name attribute-name attribute-

en
type default-value>

C
<!ENTITY entity-name “entity-value”>

h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 9 of 37


Creating Internal DTDs 3-3

y
nl
O
Code Snippet

se
rU
<!ELEMENT Mobile (Company, Model, Price,
Accessories)>

te
<!ELEMENT Company (#PCDATA)>
<!ELEMENT Model (#PCDATA)>

en
<!ELEMENT Price (#PCDATA)>
<!ELEMENT Accessories (#PCDATA)>

C
<!ATTLIST Model Type CDATA “Camera”>
<!ENTITY HP “Head Phones”>
h
<!ENTITY CH “Charger”>
ec

<!ENTITY SK “Starters Kit”>


pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 10 of 37


DOCTYPE Declarations 1-2

y
nl
O
 It specifies the name of the DTD and either its content or

se
location.

rU
 It begins with <!DOCTYPE and ends with a >.

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 11 of 37


DOCTYPE Declarations 2-2

y
nl
O
Syntax

se
rU
<!DOCTYPE name_of_root_element [ internal DTD
subset ]>
or

te
<!DOCTYPE name_of_root_element SYSTEM “URL of the

en
external DTD subset” >

C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 12 of 37


Types of DTDs 1-2

y
nl
O
 DTDs can be classified as Internal or External.

se
Internal DTDs

rU

It consists of the DTD name followed by the

te

DTD enclosed in square brackets.

en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 13 of 37


Types of DTDs 2-2

y
nl
O
 External DTDs

se
 It consists of the DTDs name followed by the

rU
SYSTEM keyword followed by the address

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 14 of 37


Lesson 3 – Valid XML Documents

y
nl
O
In this third lesson, Valid XML Documents, you will

se
learn to:

rU
 Define document validity.

 Describe in brief how to test for document validity.

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 15 of 37


Well-Formed XML documents 1-2

y
nl
O
 All elements must be enclosed by the root element

se
 All elements must have closing tags

rU
 All tags should be case sensitive

te
 All elements must be properly nested

en
 All attribute values must always be quoted
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 16 of 37


Well-Formed XML documents 2-2

y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 17 of 37


Valid XML documents 1-2

y
nl
O
 A valid XML document is a well-formed XML

se
document that adheres to its DTD.

rU
 The validity of an XML document is determined by

te
checking it against its DTD.

en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 18 of 37


Valid XML documents 2-2

y
nl
O
Code Snippet

se
<?xml version=”1.0” encoding=”ISO-8859-1”?>
<!DOCTYPE Mail [

rU
<!ELEMENT Mail (To, From, Date, Time, Cc, Bcc, Subject, Message, Signatu
re)>
<!ELEMENT To (#PCDATA)>
<!ELEMENT From (#PCDATA)>

te
<!ELEMENT Date (#PCDATA)>
<!ELEMENT Time (#PCDATA)>

en
<!ELEMENT Cc (#PCDATA)>
<!ELEMENT Bcc (#PCDATA)>
<!ELEMENT Subject (#PCDATA)>

C
<!ELEMENT Message (#PCDATA)>
<!ELEMENT Signature (#PCDATA)>
]>
h
<Mail>
<To> anne@xyz.com </To>
ec

<From> bob@xyz.com </From>


<Date> 27th February 2007 </Date>
<Time> 11:30 am </Time>
pt

<Cc> </Cc>
<Bcc> </Bcc>
rA

<Subject> Meeting at Main Conference Room at 4:30pm </Subject>


<Message> Hi, Kindly request you to attend the cultural body general meeting in
the main conference room at 4:30 pm. Please be present to learn about the new
activities being planned for the employees for this year. Yours sincerely, Bob
Fo

</Message>
<Signature> </Signature>
</Mail>

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 19 of 37


Testing XML for Validity 1-2

y
nl
O
 It can be determined by using a validating parser

se
such as Microsoft XML Core Services (MSXML) 6.0

rU
Parser.

te
 MSXML enables the Internet Explorer (IE) browser

en
to validate the code.
 C
The first image displays the validation result of a
h
valid mobile.xml file.
ec

The second image displays the validation result


pt

after the removal of the signature element.


rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 20 of 37


Testing XML for Validity 2-2

y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 21 of 37


Lesson 4 - Declarations

y
nl
O
In this last lesson, Declarations, you will learn to:

se
 Explain how to declare elements.

rU
 Explain how to declare attributes.

te
 Describe entity declaration in a DTD.

en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 22 of 37


Declaring Elements

y
nl
O
 XML elements are declared with an element declaration.

se
rU
Syntax

te
<!ELEMENT element-name element-rule>

en
where,
C
ELEMENT is the keyword,
element-name is the name of the element,
h
ec
element-rule can be one of the following: No Content, Only
Parsed Character Data, Any Contents, Children, Only One
pt

Occurrence, Minimum One Occurrence, Zero or More


Occurrences, Zero or One Occurrence, Either/Or Content or
rA

Mixed Content
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 23 of 37


Testing XML for Validity

y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 24 of 37


Declaring Attributes 1-2

y
nl
O
Syntax

se
rU
<!ATTLIST element-name attribute-name attribute-
type default-value>

te
where,

en
element-name is the element the attribute belongs

C
attribute-name is the name of the attribute
h
ec

attribute-type is type of data the attribute can accept


pt

default-value is the default value for the attribute


rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 25 of 37


Declaring Attributes 2-2

y
nl
O
Value Description

se
PCDATA Parsed character data
Character data

rU
CDATA
(en1|en2|..) Enumerated list

te
ID A unique id

en
IDREF Id of another element
IDREFS C
List of other ids
h
NMTOKEN Valid XML name
ec

NMTOKENS List of valid XML names


pt

ENTITY An entity
rA

ENTITIES List of entities


Fo

NOTATION Name of a notation


xml: Predefined xml value
@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 26 of 37
Specifying Attribute Values 1-3

y
nl
O
se
Value Description

rU
value Default value

te
#REQUIRED Value must be included

en
#IMPLIED Value does not have to be included
#FIXED
C
Value is fixed
h
en1|en2|... Listed enumerated values
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 27 of 37


Specifying Attribute Values 2-3

y
nl
O
Syntax

se
#IMPLIED

rU
<!ATTLIST element-name attribute-name attribute-
type #IMPLIED>

te
en
#REQUIRED
<!ATTLIST element-name attribute-name attribute-

C
type #REQUIRED>h
#FIXED
ec

<!ATTLIST element-name attribute-name attribute-


type #FIXED “value”>
pt

Enumerated Attribute Values


rA

<!ATTLIST element-name attribute-name


(en1|en2|..) default-value>
Fo

<!ATTLIST payment type (check|cash) “cash”>

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 28 of 37


Specifying Attribute Values 3-3

y
nl
O
Code Snippet

se
rU
Default Value
<!ATTLIST Model Type CDATA “Camera”>

te
#IMPLIED

en
<!ATTLIST Model Type CDATA “Camera” #IMPLIED>

#REQUIRED
C
<!ATTLIST Model Type CDATA #REQUIRED>
h
ec

#FIXED
<!ATTLIST Model Type CDATA #FIXED “Camera”>
pt
rA

Enumerated Attribute Values


<!ATTLIST Model Type (Camera|Bluetooth) “Camera”>
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 29 of 37


Entities in DTD 1-2

y
nl
O
 It is a placeholder that consists of a name and a value.

se
 It is declared once and then repeatedly used through out the

rU
document.

te
en
Syntax

Entity declaration:
C
h
ec
<!ENTITY entity-name “entity-value”>
pt

Entity Reference:
&entity-name;
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 30 of 37


Entities in DTD 2-2

y
nl
Code Snippet

O
se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 31 of 37


Kinds of Entity Declarations 1-4

y
nl
O
Internal Entity Declaration

se
The entity value is explicitly mentioned in the entity

rU

declaration.

te
en
Syntax
C
h
<!ENTITY entity-name “entity-value”>
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 32 of 37


Kinds of Entity Declarations 2-4

y
nl
O
Code Snippet

se
<!DOCTYPE Mobile [
<!ELEMENT Mobile (Company, Model, Price, Accessories)>

rU
<!ELEMENT Company (#PCDATA)>
<!ELEMENT Model (#PCDATA)>

te
<!ELEMENT Price (#PCDATA)>
<!ELEMENT Accessories (#PCDATA)>

en
<!ATTLIST Model Type CDATA “Camera”>
<!ENTITY HP “Head Phones”>
<!ENTITY CH “Charger”>
C
<!ENTITY SK “Starters Kit”>
h
]>
ec
<Mobile>
<Company> Nokia </Company>
pt

<Model Type=”Camera”> 6600 </Model>


<Price> 9999 </Price>
rA

<Accessories> &HP;, &CH; and a &SK; </Accessories>


</Mobile>
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 33 of 37


Kinds of Entity Declarations 3-4

y
nl
O
External Entity Declaration

se
A link or path to the entity value is mentioned in

rU

place of the entity value

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 34 of 37


Kinds of Entity Declarations 4-4

y
nl
O
Syntax

se
<!ENTITY entity-name SYSTEM “URI/URL”>

rU
<!DOCTYPE Mobile [

te
<!ELEMENT Mobile (Company, Model, Price, Accessories)>
<!ELEMENT Company (#PCDATA)>
<!ELEMENT Model (#PCDATA)>

en
<!ELEMENT Price (#PCDATA)>
<!ELEMENT Accessories (#PCDATA)>
<!ATTLIST Model Type CDATA “Camera”>
<!ENTITY HP SYSTEM “hp.txt”>
<!ENTITY CH SYSTEM “ch.txt”>
<!ENTITY SK SYSTEM “sk.txt”>C
h
]>
ec
<Mobile>
<Company> Nokia </Company>
<Model Type=”Camera”> 6600 </Model>
pt

<Price> 9999 </Price>


<Accessories> &HP;, &CH; and a &SK; </Accessories>
</Mobile>
rA

hp.txt
Head Phones
ch.txt
Fo

Charger
sk.txt
Starters Kit

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 35 of 37


Summary 1-2

y
nl
O
 Document Type Definition

se
 A DTD is a non XML document made up of

rU
element, attribute and entity declarations.

te
en
 Working with DTDs
C
 The DTD structure is composed of element

declarations, attribute declarations, and entity


h
ec

declarations.
pt

 A document type declaration declares that the


rA

XML file in which it is present adheres to a certain


DTD.
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 36 of 37


Summary 2-2

y
nl
O
 Valid XML Documents

se
 A well-formed XML document adheres to the basic

rU
XML syntax rules.
 A valid XML document is a well-formed XML

te
document that adheres to its DTD.

en
 Declarations C
h
ec

 XML elements are declared with an element

declaration in the DTD.


pt
rA

 An entity is a placeholder that consists of a name

and a value.
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 3/ 37 of 37


Fo
rA
pt
ec
h
C
en
te
rU
Module 4

se
XML Schema

O
nl
y
Module Overview

y
nl
O
In this module, you will learn about:

se
 XML Schema

rU
 Exploring XML Schemas

te
 Working with Complex Types

en
 Working with Simple Types

C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 2 of 63


Lesson 1 – XML Schema

y
nl
O
In this first lesson, XML Schema, you will learn to:

se
 Define and describe what is meant by schema.

rU
 Identify the need for schema.

te
 Compare and differentiate the features of DTDs and

en
schemas
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 3 of 63


XML Schema 1-2

y
nl
O
 It defines the valid building blocks of an XML document.
XML Schema language is referred as XML Schema Definition (XSD).

se

 It describes the data that is marked up, and also specifies the

rU
arrangement of tags and text.
 The dictionary defines a schema as “An outline or a model”.

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 4 of 63


XML Schema 2-2

y
nl
O
Code Snippet

se
<Book>

rU
<Title>Path To Paradise</Title>
<Author>David White</Author>

te
<Theme>Philosophy</Theme>
<Publisher>ABC Publication</Publisher>

en
<ISBN>11</ISBN>
<Price>$12481</Price>
C
<Edition>June 2000</Edition>
</Book>
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 5 of 63


XML Schema Objectives 1-2

y
nl
O
An XML Schema defines:

se
 Elements and attributes that can appear in a

rU
document
 Which elements are child elements

te
en
 The order and number of child elements

C
 Whether an element is empty or can include text
h
 Data types for elements and attributes
ec

 Default and fixed values for elements and attributes


pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 6 of 63


XML Schema Objectives 2-2

y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 7 of 63


How to write an XML Schema? 1-2

y
nl
O
XML File
The XML document contains a single element, <Message>.

se

XSD File

rU
 The file is saved with “.xsd” as the extension for storing schema
documents

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 8 of 63


How to write an XML Schema? 2-2

y
nl
O
Code Snippet

se
rU
XML File: message.xml
<?xml version= "1.0"?>

te
<Message>
Hello World!

en
</Message>

XSD File: message.xsd


<?xml version= "1.0"?> C
h
<xsd:schema xmlns:xsd= "http://www.w3.org/2001/XMLSchema
ec

">
<xsd:element name= "MESSAGE " type= "xsd:string "/>
pt

</xsd:schema>
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 9 of 63


What is an XML Schema? 1-3

y
nl
O
 XML schemas allow Web applications to exchange XML data
more robustly using the following range of new features:

se
 Schemas support data types

rU
 Schemas are portable and efficient
Schemas secure data communication

te

 Schemas are extensible

en
 Schemas catch higher-level mistakes

C
Schemas support Namespace
h
Schemas support data types
ec

 Support and ability to create the required datatypes has overcome the
pt

drawbacks of DTDs
Easy to define and validate valid document content and data formats
rA

 Easy to implement the restrictions on data


Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 10 of 63


What is an XML Schema? 2-3

y
nl
O
Schemas are portable and efficient

se
 Portable and efficient
 No need to learn any new language or any editor

rU
 Similar XML parser can be used to parse the Schema files

te
Schemas secure data communication

en
 Sender can specify the data in a way that the receiver will understand

Schemas are extensible


C
h
ec

 It is possible to reuse an existing schema to create another schema


It is possible to create own data types derived from the standard types
pt

Support reference of multiple schemas in the same document


rA


Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 11 of 63


What is an XML Schema? 3-3

y
nl
O
Schemas catch higher-level mistakes

se
 Catch higher-level mistakes that arise, such as a required field of
information is missing or in a wrong format, or an element name is

rU
mis-spelled

te
Schemas support Namespace

en
 Support for XML Namespaces allows the programmer to validate

C
documents that use markup from multiple namespaces
Constructs can be re-used from schemas already defined in a different
h

namespace
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 12 of 63


Comparing DTDs with Schemas 1-2

y
nl
O
 Drawbacks of using DTDs are:

se
DTDs are written in a non-XML syntax

rU

DTDs are not extensible

te

DTDs do not support namespaces

en


C
DTDS offer limited data typing
h
ec

Sample External DTD File: program.dtd


pt

<!ELEMENT program (comments, code)>


rA

<!ELEMENT comments (#PCDATA)>


<!ELEMENT code (#PCDATA)>
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 13 of 63


Comparing DTDs with Schemas 2-2

y
nl
O
Sample XML File with a reference to dtd: program.xml

se
rU
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE program SYSTEM "program.dtd">
<program>

te
<comments>

en
This is a simple Java Program. It will display the
message

C
"Hello world!" on execution.
</comments>
h
<code>
ec

public static void main(String[] args)


System.out.println("Hello World!"); // Display the
pt

string.
rA

</code>
</program>
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 14 of 63


Advantages of XML Schemas over DTD 1-3

y
nl
O
XML schema offers a range of new features:

se
 Richer data types

rU
 Archetypes

te
 Attribute grouping

en
 Refinable archetypes

C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 15 of 63


Advantages of XML Schemas over DTD 2-3

y
nl
O
Sample schema File: mail.xsd

se
rU
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.abc.com"

te
xmlns="http://www.abc.com"

en
elementFormDefault="qualified">
<xs:element name="mail">
<xs:complexType>
<xs:sequence> C
h
<xs:element name="to" type="xs:string"/>
ec

<xs:element name="from" type="xs:string"/>


<xs:element name="header" type="xs:string"/>
pt

<xs:element name="body" type="xs:string"/>


rA

</xs:sequence>
</xs:complexType>
</xs:element>
Fo

</xs:schema>

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 16 of 63


Advantages of XML Schemas over DTD 3-3

y
nl
O
Sample XML File with a reference to schema: mail.xml

se
rU
<?xml version="1.0"?>
<mail
xmlns="http://www.abc.com"

te
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

en
xsi:schemaLocation="http://www.abc.com mail.xsd">

<to>John</to>
<from>Jordan</from> C
h
<header>Scheduler</header>
ec

<body>3rd March Monday, 7:30 PM: board meeting!</body>


</mail>
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 17 of 63


Lesson 2 – Exploring XML Schemas

y
nl
O
In this second lesson, Exploring XML Schemas,

se
you will learn to:

rU
 List the data types supported by schemas.

 Explain the XML Schema vocabulary.

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 18 of 63


Data Types Supported by Schema 1-6

y
nl
O
 XML Schema describes a number of built-in data types, which can be
used to specify and validate the intended data type of the content.

se
It also allows a user to create a user-defined data type by extending

rU

the built-in data types using facets.

te
XML Schema recommendation defines two sorts of data types:

en

 Built-in data types



C
User-derived data types
h
Built-in data types
ec

 Available to all XML Schema authors, and should be implemented by a


conforming processor.
pt
rA

User-derived data types


Fo

 Defined in individual schema instances, and are particular to that


schema.
@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 19 of 63
Data Types Supported by Schema 2-6

y
nl
O
string  can contain characters, line feeds, carriage returns,

se
and tab characters

rU
boolean  legal values for boolean data type are true and
false

te
 true can be replaced by the numeric value 1 and

en
false can be replaced by the value 0
numeric represents a numerical value

C
includes numbers such as whole numbers, and real
h

numbers
ec

dateTime represents a particular time on a given date,


pt


written as a string
rA

binary  include graphic files, executable programs, or any


Fo

other string of binary data


anyURI  represents a file name or location of the file
@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 20 of 63
Data Types Supported by Schema 3-6

y
nl
O
Syntax: string

se
<xs:element name="element_name" type="xs:string"/>

rU
Syntax: boolean

te
<xs:attribute name="attribute_name" type="xs:boolean"/>

en
Syntax: numeric
C
<xs:element name="element_name" type="xs:numeric"/>
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 21 of 63


Data Types Supported by Schema 4-6

y
nl
O
Syntax: dateTime

se
rU
<xs:element name="element_name" type="xs:dateTime"/>

Syntax: binary

te
en
<xs:element name="image_name" type="xs:hexBinary"/>

Syntax: anyURI C
h
ec

<xs:attribute name="image_name" type="xs:anyURI"/>


pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 22 of 63


Data Types Supported by Schema 5-6

y
nl
O
Code Snippet: string

se
<xs:element name="Customer" type="xs:string"/>

rU
<Customer>John Smith</Customer>

te
Code Snippet: boolean

en
<xs:attribute name="Disabled" type="xs:boolean"/>

C
<Status Disabled="true">OFF</Status>
h
ec

Code Snippet: numeric


pt

<xs:element name="Price" type="xs:numeric"/>


rA

<Price>500</Price>
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 23 of 63


Data Types Supported by Schema 6-6

y
nl
O
Code Snippet: dateTime

se
<xs:element name="BeginAt" type="xs:dateTime"/>

rU
<start>2001-05-10T12:35:40</start>

te
Code Snippet: binary

en
<xs:element name="Logo" type="xs:hexBinary"/>

C
h
Code Snippet: anyURI
ec

<xs:attribute name="flower" type="xs:anyURI"/>


pt
rA

<image
flower="http://www.creativepictures.com/gallery/flower.gif
" />
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 24 of 63


Additional Data types 1-3

y
nl
Additional data types are derived from the basic built-in data types,

O

which are called base type data types.

se
 The generated or derived data types, supported by the XML schema
include:

rU
 integer

te
 decimal

en
 time

Integer
C
h
 Base type is numeric data type. Includes both positive and negative
ec

numbers. Used to specify a numeric value without a fractional


component.
pt

Decimal
rA

 Represent exact fractional parts such as 3.26. Base type is numeric


data type. Used to specify a numeric value.
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 25 of 63


Additional Data types 2-3

y
nl
O
Time
Base type is the dateTime data type. Default representation is

se

16:35:26. time data type is used to specify time. Specified as

rU
"hh:mm:ss“.

te
Syntax: integer

en
<xs:element name="element_name" type="xs:integer"/>

Syntax: decimal
C
h
ec

<xs:element name="element_name" type="xs:decimal"/>


pt
rA

Syntax: time
Fo

<xs:element name="element_name" type="xs:time"/>

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 26 of 63


Additional Data types 3-3

y
nl
O
Code Snippet: integer

se
rU
<xs:element name="Age" type="xs:integer"/>

<Age>999</Age>

te
en
Code Snippet: decimal

C
<xs:element name="Weight" type="xs:decimal"/>
h
<prize>+70.7860</prize>
ec
pt

Code Snippet: time


rA

<xs:element name="BeginAt" type="xs:time"/>


Fo

<BeginAt>09:30:10.5</BeginAt>

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 27 of 63


Schema Vocabulary 1-3

y
nl
Creating a schema using XML schema vocabulary is like creating

O

any other XML document using a specialized vocabulary.

se
Every XML Schema starts with the root element <schema>.

rU
The code indicates that that the elements and data types used in the schema are derived from the
“http://www.w3.org/2001/XMLSchema” namespace and prefixed with xs.

te
en
C It specifies that the elements
defined in an XML document that
refers this schema, come from
h
the “http://www.abc.com”
ec

namespace.
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 28 of 63


Schema Vocabulary 2-3

y
nl
O
This line of code points to
“http://www.abc.com” as
the default namespace.

se
rU
te
It indicates that elements used
by the XML instance document

en
which were declared in this
schema needs to be qualified by
the namespace.

C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 29 of 63


Schema Vocabulary 3-3

y
nl
O
It indicates the default namespace declaration. This
declaration informs the schema-validator that all the

se
elements used in this XML document are declared in
the default (“http://www.abc.com”) namespace.

rU
te
en
C
h
This is the instance
ec

namespace available
for the XML schema.
pt
rA

This schemaLocation attribute has two values. The first


Fo

value identifies the namespace. The second value is the


location of the XML schema where it is stored.
@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 30 of 63
Lesson 3 – Working with Complex Types

y
nl
O
In this third lesson, Working with Complex Types,

se
you will learn to:

rU
 Describe complex type elements.

 Describe minOccurs and maxOccurs.

te
en
 Explain element content and mixed content.

C
Describe how grouping can be done.
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 31 of 63


Complex Types 1-4

y
nl
O
 A schema assigns a type to each element and

se
attribute it declares.

rU
 Elements with complex type may contain nested
elements and have attributes.

te
Only elements can contain complex types. Complex

en

type elements have four variations. They are:
C
 Empty Elements
h
ec

 Only Elements
pt

 Only Text
rA

 Mixed
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 32 of 63


Complex Types 2-4

y
nl
Empty elements

O
 Empty elements optionally specify attributes types, but do not permit

se
content.

rU
<xs:element name="Books">
<xs:complexType>
<xs:attributename="BookCode"

te
type="xs:positiveInteger"/>

en
</xs:complexType>
</xs:element>
Only Elements C
h
 These elements can only contain elements and do not contain attributes.
ec

<xs:element name="Books">
<xs:complexType>
pt

<xs:sequence>
rA

<xs:element name="ISBN" type="xs:string"/>


<xs:element name="Price" type="xs:string"/>
Fo

</xs:sequence>
</xs:complexType>
</xs:element>
@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 33 of 63
Complex Types 3-4

y
nl
O
Only Text

se
 These elements can only contain text and optionally

rU
may or may not have attributes.

te
en
<xs:complexType name="Books">
<xs:simpleContent>

C
<xs:extension base="xs:string">
<xs:attribute name="BookCode" type="xs:
h
positiveInteger"/>
ec

</xs:extension>
</xs:simpleContent>
pt

</xs:complexType>
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 34 of 63


Complex Types 4-4

y
nl
Mixed

O
 These are elements that can contain text content as well as sub-

se
elements within the element. They may or may not have attributes.

rU
<xs:element name="Books">
<xs:complexType mixed="true">

te
<xs:sequence>
<xs:element name="BookName" type="xs:string"/>

en
<xs:element name="ISBN" type="xs:positiveInteger"/>
<xs:element name="Price" type="xs:string"/>
</xs:sequence>
</xs:complexType> C
h
</xs:element>
ec
pt

A complex element can be defined in two different ways:


rA

 By directly naming the element

 By using the name and type attribute of the complex type


Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 35 of 63


Defining Complex Types 1-2

y
nl
O
 By directly naming the element

se
rU
Code Snippet

te
<xs:element name=”Student”>

en
<xs:complexType>
<xs:sequence>

C
<xs:element name=”FirstName” type=”xs:string”/>
<xs:element name=”MiddleName” type=”xs:string”/>
h
<xs:element name=”LastName” type=”xs:string”/>
ec

</xs:sequence>
</xs:complexType>
pt

</xs:element>
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 36 of 63


Defining Complex Types 2-2

y
nl
O
 By using the name and type attribute of the

se
complex type

rU
Code Snippet

te
<xs:element name=”Student” type=”PersonInfo”/>

en
<xs:complexType name=”personinfo”>
<xs:sequence>

C
<xs:element name=”FirstName” type=”xs:string”/>
<xs:element name=”LastName” type=”xs:string”/>
h
</xs:sequence>
ec

</xs:complexType>
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 37 of 63


minOccurs and maxOccurs 1-3

y
nl
O
 minOccurs

se
 It specify the minimum number of occurrences

rU
of the element in an XML document.

te
 maxOccurs

en
It specify the maximum number of occurrences
C

of the element in an XML document.


h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 38 of 63


minOccurs and maxOccurs 2-3

y
nl
O
Code Snippet

se
rU

<xs:element name= “Books”>
<xs:complexType>

te
<xs:sequence>

en
<xs:element name=”ISBN” type=”xs:string”/>
<xs:element name=”Quantity” type=”xs:string”

C
maxOccurs=”100” minOccurs=”0”/>
</xs:sequence>
h
</xs:complexType>
ec

</xs:element>

pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 39 of 63


minOccurs and maxOccurs 3-3

y
nl
O
minOccur MaxOccur Number of times an element can occur

se
0 1

rU
0 or 1
1 1 1

te
0

en
* Infinite
1 *
C At least once
h
*
ec

>0 At least minOccurs times


pt

>0
rA

>maxOccurs 0
Fo

Any value <minOccurs 0

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 40 of 63


Element Content 1-2

y
nl
O
Code Snippet

se
// Books.xml

rU
<?xml version=”1.0”?>
<Books xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

te
xsi:noNamespaceSchemaLocation= “Books.xsd”>

en
<Title>A cup of tea</Title>
<Author>
<Name>Dennis Compton</Name>
</Author>
<Author> C
h
<Name>George Ford</Name>
ec

</Author>
<Publisher>
pt

<Name>Orange</Name>
</Publisher>
rA

</Books>
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 41 of 63


Element Content 2-2

y
nl
O
Code Snippet

se
// Books.xsd

rU
<?xml version=”1.0”?>
<xs:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>
<xs:element name= “Books” type= “BookType”>

te
<xs:complexType name= “AuthorType”>

en
<xs:sequence>
<xs:element name= “Name” type=”xs:string”/>
</xs:sequence>
</xs:complexType>
C
<xs:complexType name= “PublisherType”>
h
<xs:sequence>
ec
<xs:element name= “Name” type= “xs:string”/>
</xs:sequence>
</xs:complexType>
pt

<xs:complexType name= “BookType”>


rA

<xs:sequence>
<xs:element name= “Title” type= “xs:string”/>
<xs:element name= “Author” type=”ComposerType”
Fo

maxOccurs= “unbounded”/>
<xs:element name= “Publisher” type=”PublisherType”
minOccurs=”0” maxOccurs= “unbounded”/
@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 42 of 63
Mixed Content

y
nl
O
Code Snippet

se
// Book.xml

rU
<Books>
Apocalypse written by<Author>Mary Jane</Author>

te
is of Genre<Category>Fiction</Type>.
</Books>

en
// Book.xsd
C
<xs:element name=”Books”>
h
ec
<xs:complexType mixed=”true”>
<xs:sequence>
pt

<xs:element name=”Author” type=”xs:string”/>


<xs:element name=”Category” type=”xs:string”/>
rA

</xs:sequence>
</xs:complexType>
Fo

</xs:element>

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 43 of 63


Grouping Constructs 1-3

y
nl
O
xs:all

se
 This grouping construct requires that each element in

rU
the group must occur at most once

te
Code Snippet

en
<xs:element name= “Books”>
<xs:complexType>
<xs:all> C
h
<xs:element name=”Name” type=”xs:string” minOccurs= “1”
ec
maxOccurs= “1”/>
<xs:element name=”ISBN” type=”xs:string” minOccurs= “1”
pt

maxOccurs= “1”/>
<xs:element name=”items” type=”Items” minOccurs= “1” />
rA

</xs:all>
</xs:complexType>
</xs:element>
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 44 of 63


Grouping Constructs 2-3

y
nl
O
xs:sequence

se
 It specifies each member of the sequence to appear

rU
in the same order

te
Code Snippet

en
<xs:element name=”Books”>
<xs:complexType>
<xs:sequence> C
h
<xs:element name=”Name” type=”xs:string” />
ec
<xs:element name=”ISBN” type=” xs:string “ />
<xs:element name=”Price” type=” xs:string “ />
pt

</xs:sequence>
</xs:complexType>
rA

</xs:element>
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 45 of 63


Grouping Constructs 3-3

y
nl
O
xs:choice

se
 It will allow only one of the choices to appear instead

rU
of requiring all the elements to be present

te
Code Snippet

en
C
<xs:complexType name=”AdressInfo”>
<xs:group>
h
<xs:choice>
ec
<xs:element name=”Address” type=”USAddress” />
<xs:element name=”Address” type=”UKAddress” />
pt

<xs:element name=”Address” type=”FranceAddress” />


</xs:choice>
rA

</xs:group>
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 46 of 63


Lesson 4 – Working with Simple Types

y
nl
O
In this last lesson, Working with Simple Types,

se
you will learn to:

rU
 Describe simple types.

 List and describe the data types used with simple

te
types.

en
 Explain restrictions and facets.
C
 Identify the usage of attributes.
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 47 of 63


Defining a Simple Type Element

y
nl
O
 They are used to form the textual data and specify the type of
data allowed within attributes and elements.

se
Syntax

rU
<xs:element name=”XXXX” type=”YYYY”/>

te
en
Code Snippet

C
Book.xml: XML Elements
<Book_name>The Da vinci code</Book_name>
h
<TotalNoOfPages>360</TotalNoOfPages>
ec

<Author_name>Dan Brown</Author_name>
pt

Book.xsd: Corresponding simple element definitions


rA

<xs:element name=”Book_name” type=”xs:string” />


<xs:element name=”TotalNoOfPages”
type=”xs:integer”/>
Fo

<xs:element name=”Author_name” type=”xs:string”/>

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 48 of 63


Data types used with Simple types

y
nl
O
 Built-in simple type

se
 User-defined simple type

rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 49 of 63


Built-in Simple types 1-2

y
nl
O
 There are several built-in simple types, such as

se
integer, date, float and string.

rU
 It can contain a default value or a fixed value.

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 50 of 63


Built-in Simple types 2-2

y
nl
O
se
Syntax

rU
<xs:element name=”XXXX” type=”YYYY” default=”ZZZZ”/>

te
Code Snippet

en
<xs:element name=”AccountType” type=”xs:string”
fixed=”Savings”/>
C
<xs:element name=”BalanceAmount” type=”xs:integer”
h
default=”5000”/>
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 51 of 63


User-defined Simple types 1-2

y
nl
O
 Custom user defined datatype can be created using

se
the <simpleType> definition.

rU
te
Syntax

en
C
<xs:simpleType name=”name of the simpleType”>
<xs:restriction base=”built-in data type”>
h
<xs:constraint=”set constraint to limit the
ec

content”/>
</xsd:restriction>
pt

</xsd:simpleType>
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 52 of 63


User-defined Simple types 2-2

y
nl
O
Code Snippet 1

se
<xs:simpleType name=”AngleMeasure”>

rU
<xs:restriction base=”xs:integer”>
<xs:minInclusive value=”0”/>

te
<xs:maxInclusive value=”360”/>
</xs:restriction></xs:simpleType>

en
Code Snippet 2 C
h
ec

<xs:simpleType name=”triangle”>
<xs:restriction base=”xsd:string”>
pt

<xs:enumeration value=”isosceles”/>
rA

<xs:enumeration value=”right-angled”/>
<xs:enumeration value=”equilateral”/>
</xs:restriction>
Fo

</xs:simpleType>

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 53 of 63


Restrictions 1-2

y
nl
It can be specified for the simpleType elements.

O

They are declared using the <restriction>

se

declaration.

rU
Syntax

te
<restriction base=”name of the simpleType you are

en
deriving from”>

C
In this <restriction> declaration, the base data type can
be specified using the base attribute.
h
ec

Code Snippet
pt

<xs:simpleType name=”Age”>
<xs:restriction base=”xs:integer”>
rA

...
...
Fo

</xs:restriction>
</xs:simpleType>
@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 54 of 63
Restrictions 2-2

y
nl
O
Syntax

se
<xs:simpleType name= “name”>
<xs:restriction base= “xs:source”>

rU
<xs:facet value= “value”/>
<xs:facet value= “value”/>

te

</xs:restriction>

en
</xs:simpleType>

Code Snippet C
h
ec

<xs:simpleType name=”triangle”>
<xs:restriction base=”xs:string”>
pt

<xs:enumeration value=”isosceles”/>
<xs:enumeration value=”right-angled”/>
rA

<xs:enumeration value=”equilateral”/>
</xs:restriction>
Fo

</xs:simpleType>

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 55 of 63


Facets 1-2

y
nl
O
 They are used to restrict the set or range of values a datatype can contain.
There are 12 facet elements declared using a common syntax.

se
Facet Description

rU
minExclusive Specifies the minimum value for the type that excludes the value provided.
minInclusive Specifies the minimum value for the type that includes the value provided.

te
maxExclusive Specifies the maximum value for the type that excludes the value provided.

en
maxInclusive Specifies the maximum value for the type that includes the value provided.

C
totalDigits Specifies the total number of digits in a numeric type.
fractionDigits Specifies the number of fractional digits in a numeric type.
h
length Specifies the number of items in a list type or the number of characters in a
ec

string type.
minLength Specifies the minimum number of items in a list type or the minimum number
pt

of characters in a string type.


rA

maxLength Specifies the maximum number of items in a list type or the maximum
number of characters in a string type.
enumeration Specifies an allowable value in an enumerated list.
Fo

whiteSpace Specifies how whitespace should be treated within the type.


pattern Restricts string types.
@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 56 of 63
Facets 2-2

y
nl
O
Syntax

se
<xs:simpleType name= “name”>

rU
<xs:restriction base= “xs:source”>
<xs:facet value= “value”/>

te
<xs:facet value= “value”/>

en
</xs:restriction>
</xs:simpleType>
C
h
Code Snippet
ec

<xs:simpleType name=”triangle”>
pt

<xs:restriction base=”xs:string”>
<xs:enumeration value=”isosceles”/>
rA

<xs:enumeration value=”right-angled”/>
<xs:enumeration value=”equilateral”/>
Fo

</xs:restriction>
</xs:simpleType>
@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 57 of 63
Attributes 1-3

y
nl
O
 Default

se
 Fixed

rU
 Optional
 Prohibited

te
 Required

en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 58 of 63


Attributes 2-3

y
nl
O
Syntax

se
<xs:attribute name=”Attribute_name”
type=”Attribute_datatype”/>

rU
te
where,
Attribute_name is the name of the attribute.

en
Attribute_datatype specifies the data type of the
attribute. There are lot of built in data types in XML schema such
C
as string, decimal, integer, boolean, date, and time.
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 59 of 63


Attributes 3-3

y
nl
O
Code Snippet

se
....
<xs:complexType name= “SingerType”>

rU
<xs:sequence>
<xs:element name= “Name”>

te
<xs:complexType>
<xs:all>

en
<xs:element name= “FirstName” type=”xs:string”/>

C
<xs:element name= “LastName” type=”xs:string”/>
</xs:all>
h
</xs:complexType>
ec

</xs:element>
</xs:sequence>
pt

<xs:attribute name= ”age” type=”xs:positiveInteger”


use= “optional/>
rA

</xs:complexType>
....
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 60 of 63


Summary 1-3

y
nl
O
 XML Schema

se
 An XML Schema is an XML based alternative to DTDs, which

describes the structure of an XML document.

rU
 An XML Schema can define elements, attributes, child

te
elements and the possible values that can appear in a

en
document.
 Schemas overcome the limitations of DTDs and allow Web

C
applications to exchange XML data robustly, without relying
h
on adhoc validation tools.
ec

 Exploring XML Schemas


pt

 XML schema offers built-in and user defined data types.


rA

 It supports built-in data types like string, boolean, number,

dateTime, binary, and uri.


Fo

 It also supports integer, decimal, time, and user-defined

@ Aptech Limited
data types. Modern Markup for Data Interchange/ Module 4/ 61 of 63
Summary 2-3

y
nl
O
 Working with Complex Types

se
 Elements with complex type may contain nested elements

and have attributes.

rU
 A complex element can be defined by directly naming the

te
element and by using the name and the type attribute of the

en
complex type.
 minOccurs and maxOccurs specify the minimum and

C
maximum number of occurrences of the element in an XML
h
document respectively.
ec

 Element content in an XML document contains only XML


pt

elements and mixed content contains text mixed with


rA

elements.
 The grouping constructs in XML schema specify the order of
Fo

XML elements.

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 62 of 63


Summary 3-3

y
nl
O
 Working with Simple Types

se
 The elements of simple type describe the content and data

type of the element rather than its structure.

rU
 The simple type can have built-in and user defined data

te
types.

en
 Simple type definition takes one of the two values, default or

fixed as per the requirement.


 C
The user-defined data type can be derived from a built-in
h
type or an existing simple type.
ec

 Use of restrictions and facets restricts its content to user


pt

prescribed values.
rA

 Schema supports usage of attributes in elements, which is

declared with a simple type definition.


Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 4/ 63 of 63


Fo
rA
pt
ec
h
C
en
te
rU
Module 5

se
Style Sheets

O
nl
y
Module Overview

y
nl
O
In this module, you will learn about:

se
 Style Sheets

rU
 Selectors in CSS

te
 Properties and Values

en
 Inheritance and Cascades in CSS

C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 2 of 53


Lesson 1 – Style Sheets

y
nl
O
In this first lesson, Style Sheets, you will learn to:

se
 Define and describe style sheets.

rU
 Define and describe cascading style sheets (CSS).

te
 Explain how to implement styles with CSS.

en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 3 of 53


Need of Style Sheets

y
nl
O
 They are a set of rules that describe the appearance of data

se
in an XML document.

rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 4 of 53


Various Style Sheets

y
nl
O
Cascading Style Sheet (CSS)

se
It allows you to control the appearance of data in

rU

HTML and XML documents.

te
en
C
Extensible Style Sheet (XSL)
h
It is used to define the appearance of data contained
ec

only in XML documents.
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 5 of 53


Cascading Style Sheets

y
nl
O
 It comprises a set of rules for various elements in a document.

se
 Each rule defines how the data enclosed in the element should be
displayed.

rU
 The term cascading is derived from this ability to mix and match rules.

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 6 of 53


Benefits of CSS

y
nl
O
 Any style or presentation changes to data can be

se
achieved faster as changes are to be implemented

rU
in one place.
Device independence can be achieved by defining

te

different style sheets for different device.

en

C
Reduction in document code as the presentation
information is stored in a different file and can be
h
ec

reused.
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 7 of 53


Style Rules

y
nl
O
 They define how the content enclosed in an

se
element will be displayed.

rU
 These rules are applicable to all child elements

te
within an element.

en
 Selector

Property
C
h

ec

 Value
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 8 of 53


Ways of Writing Style rules

y
nl
O
 A single selector can have more than one property-value

se
pairs associated with it.

rU
 A collection of one or more property-value pairs can be
associated with more than one selector.

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 9 of 53


External Style Sheets

y
nl
O
 It should appear in the prolog section of an XML document, that is, it should
appear before any tag of an element.

se
 One XML document can have more than one style sheet processing
instructions, each linked to one .css file.

rU
Syntax

te
<?xml-stylesheet href=”headers.css” type=”text/css”?>

en
where,
C
xml-stylesheet is the processing instruction.
h
url is the URL of a .css file; the .css file can on a local system or
ec

anywhere on the Internet.


type=”text/css” is optional; however if a browser does not support
pt

CSS, it informs the browser that it does not have to download a .css file.
rA

Code Snippet
Fo

<?xml-stylesheet href=”url” [type=”text/css”]?>

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 10 of 53


Lesson 2 – Selectors in CSS

y
nl
O
In this second lesson, Selectors in CSS, you will

se
learn to:

rU
 Identify simple selectors in CSS.

 State the use of universal selector in CSS.

te
en
 Describe ID selectors.

C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 11 of 53


Simple Selectors

y
nl
O
 It comprises an element name followed by one or

se
more property declarations.

rU
 It match every occurrence of the element in a
document.

te
en
Code Snippet

C
/* Simple selector */
CD { color: black }
h
ec
/* Single element, multiple property declarations */
CD { color: white; background-color: blue }
pt

/* Multiple elements, multiple property declarations */


CD, Name, Title { color: white; background-color: blue }
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 12 of 53


Universal Selector

y
nl
O
 It comprises an asterisk followed by property

se
declarations.

rU
 It matches all the elements in a document.

te
Code Snippet

en
* { color : blue }
C
h
ec

Displays the content of all elements in a document in blue.


pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 13 of 53


ID Selector 1-2

y
nl
O
 It comprises a hash (#) symbol, immediately followed by an attribute’s
value followed by property declarations.

se
 It is used to define styles for unique elements in a document.

rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 14 of 53


ID Selector 2-2

y
nl
O
Syntax

se
rU
#attribute_value { property_declarations }

te
Code Snippet

en
#1001 { color : blue }
C
h
Displays the content of an element in blue if its id attribute’s value equals
ec

1001.
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 15 of 53


Lesson 3 – Properties and Values

y
nl
O
In this third lesson, Properties and Values, you will

se
learn to:

rU
 State and describe how to use color properties.

 Describe the font property.

te
en
 Describe the other properties such as margins,

borders, padding.
C
 Explain briefly about positioning and alignment.
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 16 of 53


Color Properties

y
nl
O
Color Names RGB Percentages RGB Values Hexadecimal Values

se
aqua rgb(0%,65%,65) rgb(0,160,160) #00a0a0
black rgb(0%,0%,0%) rgb(0,0,0) #000000

rU
blue rgb(0%,32%,100) rgb(0,80,255) #0050ff
gray rgb(65%,65%,65%) rgb(160,160,160) #a0a0a0

te
green rgb(0%,100%,0%) rgb(0,255,0) #00ff00

en
lime rgb(0%,65%,0%) rgb(0,160,0) #00a000
maroon rgb(70%,0%,32%) rgb(176,0,80) #b00050
navy C
rgb(0%,0%,65%) rgb(0,0,160) #0000a0
h
olive rgb(65%,65%,0%) rgb(160,160,0) #a0a000
ec

purple rgb(65%,0%,65%) rgb(160,0,160) #a000a0


pt

red rgb(100%,0%,32%) rgb(255,0,80) #ff0050


rA

silver rgb(90%,90%,90%) rgb(225,225,255) #d0d0d0


teal rgb(0%,65%,100%) rgb(0,160,255) #00a0ff
Fo

white rgb(100%,100%,100%) rgb(255,255,255) #ffffff


yellow rgb(100%,100%,0%) rgb(255,255,0) #ffff00

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 17 of 53


Setting Color Properties 1-3

y
nl
O
Syntax

se
rU
te
where,

en
color
Property to set the foreground color of text in an element.
colorValue
C
colorValue can take up any value from the CSS color table.
h
background-color
ec

Property to set the background color of text in an element.


pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 18 of 53


Setting Color Properties 2-3

y
nl
O
Code Snippet

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 19 of 53


Setting Color Properties 3-3

y
nl
O
Style Sheet

se
rU
where,

te
Cars { background-color:rgb(0%,32%,100%);color:

en
#ffffff }
Causes the text enclosed in Cars element to be displayed in white color

C
with a background color of blue.
Price{color:yellow;}
h
Causes the text enclosed in Price element to be displayed in yellow color.
ec
pt

Output
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 20 of 53


Font Properties

y
nl
O
se
Property Name Description

rU
font-family To specify the font family
font-size To specify the size of font

te
font-style To specify the style of font

en
font-weight To specify the weight of font

C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 21 of 53


Font-family Property 1-2

y
nl
O
Syntax

se
rU
where,
font-family

te
Property to specify the font-family to be used.
font-family name(s)

en
Comma separated list of font-family names such as serif, san-serif, monospace,
cursive, and fantasy. The list should start with the most specific font in which

C
you want to display the data and end with the most generic font.
h
Code Snippet
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 22 of 53


Font-family Property 2-2

y
nl
O
Style Sheet

se
rU
te
Output

en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 23 of 53


Font-size Property 1-3

y
nl
O
Syntax

se
rU
where,

te
font-size

en
Property to specify the size of font.
xx-small|x-small|small|medium large|x-large|xx-large

C
One of various values that can be assigned to the property font-size.
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 24 of 53


Font-size Property 2-3

y
nl
O
Code Snippet

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 25 of 53


Font-size Property 3-3

y
nl
O
Style Sheet

se
rU
te
where,

en
Cars{font-size:medium }
All the text enclosed in Cars element and its child elements will

C
be displayed with medium font size.
h
ec

Output
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 26 of 53


Font Style and Weight Properties 1-3

y
nl
O
Syntax

se
rU
te
where,

en
font-style
Property to specify the style of text in an element.
normal|oblique|italic
C
One of the values that can be assigned to font- style property.
h
font-weight
ec

Property to specify the weight style of the text in an element.


pt

light | normal | bold


One of the values that can be assigned to font- weight property.
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 27 of 53


Font Style and Weight Properties 2-3

y
nl
O
Code Snippet

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 28 of 53


Font Style and Weight Properties 3-3

y
nl
O
Style Sheet

se
rU
te
where,
Manufacturer { font-weight: bold }

en
The text enclosed in Manufacturer element will be displayed in bold.
Location {font-style: italic}
C
The text enclosed in Location element will be displayed in italics.
h
ec

Output
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 29 of 53


Margins in CSS 1-3

y
nl
O
Syntax

se
rU
te
where,

en
margin-left | margin-right | margin-top | margin- bottom
The various margin properties to set left, right, top and bottom margins.
marginValue
C
The value to be assigned to one of the margin properties.This value can be
h
a fixed value or a percentage.
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 30 of 53


Margins in CSS 2-3

y
nl
O
Code Snippet

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 31 of 53


Margins in CSS 3-3

y
nl
O
Style Sheet

se
rU
te
where,

en
Manufacturer {margin-left:20;margin-right:50;}
Inserts a space of 20 pixels to the left and a space of 50 pixels to the right
C
of text enclosed in element Manufacturer.
Manufacturer {background-color: aqua}
h
Sets the background of text enclosed in element Manufacturer to aqua.
ec

Vehicle {background-color: orange}


pt

Sets the background of text enclosed in element Vehicle to orange.


rA

Output
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 32 of 53


Border Properties in CSS 1-3

y
nl
O
Syntax
Output

se
rU
where,
border: Property to set the border of the box surrounding an

te
element’s data.

en
border_width: Specifies the thickness of the border.
Possible values are thin, medium and thick.
C
border_style: Specifies the style of the border. Possible
h
values are solid, dashed, dotted, groove, ridge, double,
ec

inset, outset.
border_color: Specifies the color of the border. All values
pt

that are applicable to CSS color property are allowed.


rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 33 of 53


Border Properties in CSS 2-3

y
nl
O
Code Snippet

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 34 of 53


Border Properties in CSS 3-3

y
nl
O
Style Sheet

se
rU
te
where,

en
Manufacturer {border: thick dashed magenta }
Displays a thick and dashed magenta border around the content of
Manufacturer element.
C
Model { border: thick solid olive }
h
Displays a thick and solid olive border around the content of Model element.
ec

Color {border: thick groove aqua}


pt

Displays a thick and groove aqua border around the content of Color element.
Price {border: thick inset gray}
rA

Displays a thick and inset gray border around the content of Price element.
Fo

Output
@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 35 of 53
Padding Properties in CSS 1-2

y
nl
O
Syntax

se
rU
Code Snippet

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 36 of 53


Padding Properties in CSS 2-2

y
nl
O
Style Sheet

se
rU
te
en
where,
Manufacturer { padding: 2 }

C
Inserts padding of 2 pixels between the four borders and text of Manufacturer element. The four
borders applicable are top, right, bottom, and left.
Model { padding: 2 5 }
h
Inserts padding between the borders and text of Model element. The value 2 is applied to top and
ec
bottom borders and 5 is applied to left and right borders.
Color { padding: 2 5 8 }
Inserts padding between the borders and text of Color element. The value 2 is applied to top border, 5
pt

to left and right borders, and value 8 to bottom border.


Price { padding: 2 5 8 10 }
rA

Inserts padding between the borders and text of Price element. The value 2 is applied to top
border, 5 to right border, 8 to bottom border, and 10 to left border.
Fo

Output
@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 37 of 53
CSS Units

y
nl
O
Unit Type Unit Designator

se
Relative em – defines the height of element’s font.

rU
ex – defines the x-height of element’s font.
px – defines the pixel relative to display device.

te
% - percentage.

en
in – inches
Absolute
C
cm – centimeters
mm – millimeters
h
ec

pt – 1/72 inch
pc – 12 pt
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 38 of 53


Position Properties

y
nl
O
se
Property Description Possible Values
position Property to place an element in a static, static, fixed,

rU
relative, absolute or fixed position relative or absolute
top Property specifying how far the top auto, integer or

te
edge of an element is above/below the floating point values

en
top edge of the parent element adhering to CSS length units
left Property specifying how far the left auto, integer or

C
edge of an element is to the right/left of floating point values
the left edge of the parent element adhering to CSS length
h
units
ec

bottom Property specifying how far the bottom auto, integer or


edge of an element is above/below the floating point values
pt

bottom edge of the parent element adhering to CSS length units


rA

right Property specifying how far the right auto, integer or


edge of an element is to the left/right of floating point values
Fo

the right edge of the parent element adhering to CSS length units

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 39 of 53


Position Properties Example 1-3

y
nl
O
Code Snippet

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 40 of 53


Position Properties Example 2-3

y
nl
O
Style Sheet

se
rU
te
where,
Location {position:relative;top:20;left:20}

en
Positions the text of Location element relative to the previous element.
However, inside the box, the content is placed at 20 pixels from top and 20
pixels from left.
C
h
Price {position:absolute;top:40;left:20}
Positions the text of Price element at an absolute location of 40 pixels from
ec

the top and 20 pixels from the left.


pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 41 of 53


Position Properties Example 3-3

y
nl
O
Output

se
rU
te
en
where,
40,000
C
Text displayed using absolute positioning
Liverpool
h
Text displayed using relative positioning
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 42 of 53


Display Property 1-3

y
nl
O
Syntax

se
rU
te
where,

en
display
Property to specify how the element is to be rendered.
none C
h
No rendering is applied to the element.
ec
inline
Displays the element text as in line.This is the default value if no value is
pt

specified.
Block
rA

Displays the element text on a new line in a block of its own.


Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 43 of 53


Display Property 2-3

y
nl
O
Code Snippet

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 44 of 53


Display Property 3-3

y
nl
O
Style Sheet

se
rU
te
where,
Price { display: block }

en
Inserts a new line before and after the text of element Price.

C
h
Output
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 45 of 53


Text Alignment and Indentation 1-3

y
nl
O
Syntax

se
rU
te
en
where,
text-align
C
Property to align the text in a block.
alignment_value
h
Can be one of the following: left(default), right, center and justify.
ec

text-indent
pt

Property to indent the text in a block.


value
rA

Floating point value followed by absolute units designators or relative units


designators; or an integer value followed by percentage (%) symbol.
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 46 of 53


Text Alignment and Indentation 2-3

y
nl
O
Code Snippet

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 47 of 53


Text Alignment and Indentation 3-3

y
nl
O
Style Sheet

se
rU
where,
Price {display: block}

te
Inserts a new line before and after the text of element Price and displays the
text in a separate block.

en
Price {text-align: left}
Left aligns the text of Price element.
Price {text-indent: 20}
C
Indents the text of Price element at 20 pixels.
h
ec

Output
pt
rA
Fo

where,
40,000
The text in element Price is display on a separate row and left indented at 20 pixels.
@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 48 of 53
Lesson 4 - Inheritance and Cascades in CSS

y
nl
O
In this last lesson, Inheritance and Cascades in CSS,

se
you will learn to:

rU
 Define the process of cascading.

 Explain inheritance.

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 49 of 53


Cascading in CSS

y
nl
O
 All declarations that apply to an element and

se
property are grouped.

rU
 Declarations are sorted by weight and origin.
Declarations are sorted by specificity of selector.

te

en
 Declarations are sorted by the order specified.
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 50 of 53


Inheritance in CSS 1-2

y
nl
O
 Inheritance is the ability of one entity to acquire the

se
characteristics of another entity.

rU
 In CSS, the child elements inherit the styles rules

te
defined for parent element.

en
 However, this is applicable only if the child has no
C
explicit style rule defined for it.
h
ec

 Otherwise, the style rule defined for child element


overrides the style rule defined for parent element.
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 51 of 53


Inheritance in CSS 2-2

y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 52 of 53


Summary

y
nl
O
 Style Sheets
Style Sheets are a set of rules that define the appearance of data.

se

 These rules are written in a file with the extension .css.

rU
 The .css file is associated with an XML document using the xml-
processing instruction.

te
 Selectors in CSS
Selectors define the elements to which the styles will be applied.

en

 The various types of selectors are simple, universal and ID selectors.

 Properties and Values


C
h
 CSS provides several properties to define the appearance of data.
ec

 Some of these properties are color, background-color, position, padding,


font, text- align to name a few.
pt

 Inheritance and Cascades in CSS


rA

 In CSS, a child element inherits the styles rules applied to its ancestor
element.
Fo

 Also there are several sources of style sheets, hence CSS follows W3C
defined cascading order when applying style rules to elements.
@ Aptech Limited Modern Markup for Data Interchange/ Module 5/ 53 of 53
Fo
rA
pt
ec
h
C
en
te
rU
Module 6

se
XSL and XSLT

O
nl
y
Module Overview

y
nl
O
In this module, you will learn about:

se
 Introduction to XSL

rU
 Working with XSL

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 2 of 53


Lesson 1 – Introduction to XSL

y
nl
O
In this first lesson, Introduction to XSL, you will

se
learn to:

rU
 Define Extensible Stylesheet Language (XSL),

Extensible Stylesheet Language Transformations

te
(XSLT) and their purpose.

en
 Explain the structure and syntax of XSL.
C
 Distinguish between Cascading Style Sheet (CSS)
h
ec

and XSL.
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 3 of 53


Stylesheets 1-2

y
nl
O
 It is a collection of commands that tells a

se
processor how to render the visual appearance of

rU
content in a web page.
Stylesheets typically contain instructions like:

te

 Number figures sequentially throughout the

en
document.
C
 Display hypertext links in blue.
h
ec

 Position text and images on a Web page.


pt

 Create a layout optimized for small displays for


rA

mobile phones.
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 4 of 53


Stylesheets 2-2

y
nl
O
 CSS is used for HTML content formatting.

se
 XSL is used to describe how the XML document

rU
should be displayed.

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 5 of 53


Extensible Stylesheet Language (XSL)

y
nl
O
 XSL Transformations (XSLT)

se
 An XML language for transforming XML documents.

rU
 XML Path Language (XPath)
A language for navigating the XML document.

te

XSL Formatting Objects (XSL-FO)

en

An XML language for formatting XML documents.


C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 6 of 53


XSL Transformations

y
nl
O
 The transformation component of the XSL stylesheet

se
technology is XSLT.

rU
 It describes the process of transforming an XML
document, using a transformation engine and XSL.

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 7 of 53


XSL Processing Model

y
nl
O
 It reads an XML document and processes it into a

se
hierarchical tree.

rU
 It starts with the root node in the tree and performs
pattern matching in the stylesheet.

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 8 of 53


XSLT Structure and Syntax

y
nl
O
 It uses a standard document introduction, matching

se
closing tags for any opening tags that contain

rU
content, and a proper syntax for empty elements.
The style rules are written in a file with the extension

te

.xsl.

en
Syntax
C
<xsl:stylesheet version=”1.0”
h
xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>
ec

………
………
pt

</xsl:styleheet>
rA

where,
<xsl:stylesheet>: Root element of the stylesheet.
Fo

xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”: refers
to the official W3C XSLT namespace. You must include the attribute
version=”1.0” if you use this namespace.
@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 9 of 53
Top Level XSLT Elements

y
nl
O
 An element occurring as a child of an xsl:stylesheet element is
called a top-level element.

se
 It can occur directly inside the xsl:stylesheet element.

rU
Element Name Description

te
xsl:attribute-set Adds a list of attributes to the output node tree

en
xsl:import Used to import contents of one stylesheet into another. The
importing stylesheet takes precedence over the imported

xsl:namespace-alias C
stylesheet
Replaces source document Namespace with a new
h
Namespace in the output tree node
ec

xsl:output Specifies the output for the result tree. It contains a list of
attributes. The most important one is the method attribute
pt

which dictates if the type of output is HTML, text, or XML


rA

xsl:template Used to define a template that can be applied to a node to


produce a desired output display
Fo

xsl:variable Defines a variable in a stylesheet or template, and to


assign it a value

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 10 of 53


CSS and XSL

y
nl
O
 They are two different style languages recommended by the World
Wide Web Consortium (W3C).

se
 XSL is more powerful and complex than CSS.

rU
CSS XSL

te
Stylesheet language to create a style Stylesheet language to create a style
for HTML and XML documents for XML documents

en
Determines the visual appearance of a Provides a means of transforming XML

the source document C


page, but does not alter the structure of documents
h
Does not support decision structures Supports decision structures and can
ec

and it cannot calculate quantities or calculate quantities or store values in


store values in variables variables
pt

Uses its own notation Uses an XML notation


rA

Highly effective and easy to learn for Designed to meet the needs of more
Fo

simple applications complex applications for richer style


sheets

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 11 of 53


Lesson 2 – Working with XSL

y
nl
O
In this last lesson, Working with XSL, you will

se
learn to:

rU
 Explain XSL templates.

 Describe the use of select attribute.

te
en
 State how to use xsl:value-of element.

 Describe how to use xsl:for-each element.


C
h
 Explain briefly how to use xsl:text element.
ec

 Describe how to use xsl:number element.


pt

 Describe how to use xsl:if element.


rA

 Describe how to use xsl:choose element.


Fo

 Explain how to perform sorting using XSL.


@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 12 of 53
XSL Templates

y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 13 of 53


The xsl:template Element 1-2

y
nl
O
 It is used to define a template that can be applied to a node
to produce desired output.

se
rU
Syntax

te
<xsl:template
match=”pattern”

en
mode=”mode
name=”name”

>
priority=”number”
C
h
</xsl:template>
ec

where,
match: Is a pattern that is used to define which nodes will have which
pt

template rules applied to them. If this attribute is omitted there must be a


rA

name attribute.
mode: Allows the same nodes to be processed more than once.
name: Specifies a name for the template. If this attribute is omitted there must
Fo

be a match attribute.
priority: Is a real number that sets the priority of importance for a
template. The higher the number, the higher the priority.
@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 14 of 53
The xsl:template Element 2-2

y
nl
O
Code Snippet

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 15 of 53


The xsl:apply-templates element 1-4

y
nl
O
 It defines a set of nodes to be processed.

se
rU
Syntax

te
en
C
h
ec

where,
pt

select
Used to process nodes selected by an expression.
rA

mode
Allows the same nodes to be processed more than once. Each
Fo

time the nodes are processed, they can be displayed in a


different manner.

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 16 of 53


The xsl:apply-templates element 2-4

y
nl
O
Code Snippet

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 17 of 53


The xsl:apply-templates element 3-4

y
nl
O
Style Sheet

se
rU
te
en
C
h
ec

where,
match=”GEMEmployees/Designer”
pt

Represents the Designer child element by specifying the GEMEmployees parent element.
xsl:apply-templates select=”Name”
rA

Applies the template on Name element.


xsl:apply-templates select=”DOJ”
Applies the template on DOJ element.
xsl:value-of
Fo

Used to extract the value of a selected node.


xsl:template match=”Name”
If the template is matched with Name element, the value of the Name element is displayed in green color with font size 22 pixels.
xsl:template match=”DOJ”
If the template is matched with DOJ element, the value of the DOJ element is displayed in blue color.

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 18 of 53


The xsl:apply-templates element 4-4

y
nl
O
Output

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 19 of 53


The select attribute 1-4

y
nl
O
 It can be used to process nodes selected by an expression

se
instead of processing all children.

rU
Syntax

te
en
C
h
where,
ec

select
Uses the same kind of patterns as the match attribute of the
pt

xsl:template element. If select attribute is not present,


rA

all child element, comment, text, and processing instruction


nodes are selected.
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 20 of 53


The select attribute 2-4

y
nl
O
Code Snippet

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 21 of 53


The select attribute 3-4

y
nl
O
Style Sheet

se
rU
te
en
where,
select=”Title”

C Applies the template on Title element.


select=”Author”
Applies the template on Author element.
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 22 of 53


The select attribute 4-4

y
nl
O
Output

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 23 of 53


The xsl:value-of element 1-4

y
nl
O
 It is used to write or display text string representing the

se
value of the element specified by the select attribute.

rU
Syntax

te
en
where,
C
h
select
ec

A mandatory attribute that assigns the node (by name) to the element.
disable-output-escaping
pt

Specifies how special characters should appear in the output string.


rA

yes
Indicates that special characters should be displayed as is (for example, a < or >).
no
Fo

Indicates that special characters should not be displayed as is (for example, a > is
displayed as &gt;).

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 24 of 53


The xsl:value-of element 2-4

y
nl
O
Code Snippet

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 25 of 53


The xsl:value-of element 3-4

y
nl
O
Style Sheet

se
rU
te
en
C
h
ec

where,
xsl:value-of select=”FirstName”
pt

Display the value of the element FirstName.


xsl:value-of select=”LastName”
rA

Display the value of the element LastName.


Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 26 of 53


The xsl:value-of element 4-4

y
nl
O
Output

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 27 of 53


The xsl:for-each element 1-4

y
nl
O
 It can be used to iterate through the XML elements of a

se
specified node set.

rU
Syntax

te
en
where,
C
h
ec
select=”expresion”
The expression is evaluated on the current context to determine
pt

the set of nodes to iterate over.


rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 28 of 53


The xsl:for-each element 2-4

y
nl
O
Code Snippet

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 29 of 53


The xsl:for-each element 3-4

y
nl
O
Style Sheet

se
rU
te
en
where,

C xsl:for-each select=”Employee”
Iterates through the Employee node and
h
applies a template.
xsl:value-of select=”Department”
ec

Displays the value of Department element.


xsl:value-of select=”Name”
pt

Displays the value of Name element.


xsl:value-of select=”Salary”
rA

Displays the value of Salary element.


xsl:value-of select=”Language”
Displays the value of Language element.
Fo

</xsl:for-each>
End of for-each loop.

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 30 of 53


The xsl:for-each element 4-4

y
nl
O
Output

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 31 of 53


The xsl:text element 1-4

y
nl
O
 It is used to add literal text to the output.

se
rU
Syntax

te
en
where, C
h
disable-output-escaping
ec

Turns on or off the ability to escape special characters.


yes
pt

If the value is yes, a &gt; will appear as a >.


rA

no
If the value is no, a &gt; will appear as a &gt; in the text.
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 32 of 53


The xsl:text element 2-4

y
nl
O
Code Snippet

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 33 of 53


The xsl:text element 3-4

y
nl
O
Style Sheet

se
rU
te
en
C
h
ec
pt

where,
rA

xsl:for-each select=”Orders/Item”
Iterates through the Item element.
xsl:value-of select=”Name”
Displays the value of Name element.
Fo

<xsl:text>,</xsl:text>
Inserts a comma (,) after each name value.
<xsl:text>!!!!</xsl:text>
Inserts four exclamation marks (!) at the end of the output.

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 34 of 53


The xsl:text element 4-4

y
nl
O
Output

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 35 of 53


The xsl:number element 1-4

y
nl
O
 It can be used to determine the sequence number for the

se
current node.

rU
Syntax

te
en
C
h
ec

where,
pt

count=”pattern”
Indicates what nodes are to be counted. Only nodes that match the pattern are counted.
rA

format=”{ string }”
Sequence of tokens that specifies the format to be used for each number in the list.
value=”expression”
Fo

Specifies the expression to be converted to a number and output to the result tree.

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 36 of 53


The xsl:number element 2-4

y
nl
O
Code Snippet

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 37 of 53


The xsl:number element 3-4

y
nl
O
Style Sheet

se
rU
te
en
C
h
ec

where,
pt

position()
rA

The current node’s position in the source document.


format=”1.”
User-provided number starts with 1.
Fo

format=”I.”
User-provided roman number starts with I.

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 38 of 53


The xsl:number element 4-4

y
nl
O
Output

se
rU
te
en
where,
C
h
1.Water Bottle, I.Water Bottle
ec

The first item is numbered with number 1 and roman number I.


4.Mobile Phone, IV. Mobile Phone
pt

The fourth item is numbered with number 4 and roman number IV.
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 39 of 53


The xsl:if element 1-4

y
nl
O
 Evaluates a conditional expression against the content of the

se
XML file.

rU
Syntax

te
en
C
h
where,
ec

test=expression
The condition in the source data to test with either a true or false.
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 40 of 53


The xsl:if element 2-4

y
nl
O
Code Snippet

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 41 of 53


The xsl:if element 3-4

y
nl
O
Style Sheet

se
rU
te
en
C
h
ec
pt
rA

where,
<xsl:for-each select=”Catalog/Book”>
Fo

Iterates through the Book node.


<xsl:if test=”Price &gt; 50”>
Checks if the price of the book is greater than 50. If true, Author, Title and Year are displayed.
@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 42 of 53
The xsl:if element 4-4

y
nl
O
Output

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 43 of 53


The xsl:choose element 1-5

y
nl
O
 It is used in conjunction with xsl:when and xsl:otherwise

se
to express multiple conditional tests.

rU
Syntax

te
en
C
h
ec

where,
pt

xsl:when test=”expression”
The xsl:when element is examined in the order of occurrence. If the test
rA

expression is true, the code contained in that element is executed.


xsl:otherwise
If all the test conditions in any xsl:when element are false, then the
Fo

xsl:otherwise element is automatically selected and the code associated with


that element is executed.

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 44 of 53


The xsl:choose element 2-5

y
nl
O
Code Snippet

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 45 of 53


The xsl:choose element 3-5

y
nl
Style Sheet

O
se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 46 of 53


The xsl:choose element 4-5

y
nl
Style Sheet

O
se
rU
te
en
C
h
ec
pt

where,
rA

xsl:when test=”Price > 100”


Checks whether the price of the book is greater than 100. If true, the details like Author, Price
and Year are displayed with magenta as the background color.
Fo

xsl:otherwise
If all the conditions are false, this block is executed. Here, all other book details are printed in normal
background color.

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 47 of 53


The xsl:choose element 5-5

y
nl
O
Output

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 48 of 53


Sorting in XSLT 1-4

y
nl
O
 It can be used to sort a group of similar elements.

se
Syntax

rU
te
en
C
h
where,
ec
case-order
Indicates whether the sort will have upper or lowercase letters listed first in the sort output. The
default option is to list uppercase first.
pt

data-type: Specifies the data type of the strings.


Number: Sort key is converted to a number.
rA

Qname: Sort is based upon a user-defined data type.


Text: Specifies that the sort keys should be sorted alphabetically.
Order: The sort order for the strings. The default value is “ascending”.
Fo

Select: Expression that defines the key upon which the sort will be based. The expression is
evaluated and converted to a string that is used as the sort key.

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 49 of 53


Sorting in XSLT 2-4

y
nl
O
Code Snippet

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 50 of 53


Sorting in XSLT 3-4

y
nl
O
Style Sheet

se
rU
te
en
C
h
ec
pt

where,
rA

select=”UnitPrice”
Sort is based on UnitPrice element.
order=”descending”
Fo

The sorting order for UnitPrice is descending in that the higher value will be
displayed first.

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 51 of 53


Sorting in XSLT 4-4

y
nl
O
Output

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 52 of 53


Summary

y
nl
O
 Introduction to XSL

se
 XML provides the ability to format document content.
XSL provides the ability to define how the formatted XML content is

rU

presented.
An XSL Transformation applies rules to a source tree read from an

te

XML document to transform it into an output tree written out as an

en
XML document.

 Working with XSL C


h
 An XSL template rule is represented as an xsl:template element.
ec

 You can process multiple elements in two ways: using the


xsl:apply-templates element and the xsl:for-each element.
pt

The xsl:stylesheet element allows you to include a stylesheet


rA

directly in the document it applies to.


The xsl:if element produces output only if its test attribute is
Fo

true.
@ Aptech Limited Modern Markup for Data Interchange/ Module 6/ 53 of 53
Fo
rA
pt
ec
h
C
en
te
rU
Module 7

se
More on XSLT

O
nl
y
Module Overview

y
nl
O
In this module, you will learn about:

se
 XPath
 XPath Expressions and Functions

rU
 Working with different Styles

te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 2 of 51


Lesson 1 - XPath

y
nl
O
In this first lesson, XPath, you will learn to:

se
 Define and describe XPath.
 Identify nodes according to XPath.

rU
 List operators used with XPath.

te
 Describe the types of matching

en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 3 of 51


XPath 1-2

y
nl
O
 XPath can be considered as a query language like SQL.

se
 Extracts information from an XML document.
 Language for retrieving information from a XML document.

rU
 Used to navigate through elements and attributes in an XML
document.

te
 Allows identifying parts of an XML document.

en
 Provides a common syntax for features shared by Extensible
Stylesheet Language Transformations (XSLT) and XQuery.
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 4 of 51


XPath 2-2

y
nl
O
 XSLT

se
 Language for transforming XML documents into XML, HTML or
text.

rU
 XQuery
Builds on XPath.

te

 Language for extracting information from XML documents.

en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 5 of 51


Benefits of XPath

y
nl
O
 Provides single syntax that you can use for:

se
 Queries
Addressing

rU

 Patterns
Concise, simple, and powerful.

te

Benefits:

en

 Syntax is simple for the simple and common cases.



C
Any path that can occur in an XML document.
Any set of conditions for the nodes in the path can be specified.
h

ec

 Any node in an XML document can be uniquely identified.


pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 6 of 51


XML Document in XPath 1-4

y
nl
O
 An XML document is viewed as a tree.

se
 Each part of the document is represented as a node.

rU
 Nodes

te
 Root

en
 A single root node that contains all other nodes in the tree.
Element
C

 Every element in a document has a corresponding element


h
node that appears in the tree under the root node.
ec

 Within an element node appear all of the other types of nodes


pt

that correspond to the element’s content.


Element nodes may have a unique identifier associated with
rA

them that is used to reference the node with XPath.


Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 7 of 51


XML Document in XPath 2-4

y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 8 of 51


XML Document in XPath 3-4

y
nl
O
 Attribute

se
 Each element node has an associated set of attribute nodes.
Element is the parent of each of these attribute nodes.

rU

 An attribute node is not a child of its parent element.


Text

te

Character data is grouped into text nodes.

en

 Characters inside comments, processing instructions and attribute


values do not produce text nodes.
 C
The text node has a parent node and it may be the child node.
h
Comment
ec

 There is a comment node for every comment, except within the
pt

document type declaration (DTD).


The comment node has a parent node and it may be the child
rA

node too.
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 9 of 51


XML Document in XPath 4-4

y
nl
O
 Processing instruction

se
 Processing instruction node exists for

rU
every processing instruction except for any
processing instruction within the

te
document type declaration.

en
 The processing instruction node has a parent
C
node and it may be the child node too.
h
 Namespace
ec

 Each element has an associated set of


pt

namespace nodes.
rA

 Provides descriptive information about their


parent node.
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 10 of 51


XPath Representation

y
nl
O
 An XPath query operates on a well-formed XML document after it
has been parsed into a tree structure.

se
rU
te
en
C
h
ec
pt
rA
Fo

Original XML Document XPath Representation


@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 11 of 51
Operators in XPath

y
nl
O
 An XPath expression returns a node set, a boolean, a string, or

se
a number.
XPath provides basic floating point arithmetic operators and

rU

some comparison and boolean operators.

te
en
Operator Description
/ Child operator; selects immediate children of the left-side collection.
//
C
Recursive descent; searches for the specified element at any depth
h
. Indicates the current context
ec

.. The parent of the current context node


pt

* Wildcard; selects all elements regardless of the element name


@ Attribute; prefix for an attribute name
rA

: Namespace separator; separates the namespace prefix from the


element or attribute name
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 12 of 51


Examples of XPath Operators

y
nl
O
se
Expression Refers to

rU
Author/FirstName All <FirstName> elements within an
<Author> element of the current context node.
BookStore//Title All <Title> elements one or more levels deep

te
in the <BookStore> element.

en
BookStore/*/Title All <Title> elements that are grandchildren of
<BookStore> elements.

C
BookStore//Book/Excerpt//Work All <Work> elements anywhere inside
<Excerpt> children of <Book> elements,
h
anywhere inside the <BookStore> element.
ec

.//Title All <Title> elements one or more levels deep


in the current context.
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 13 of 51


Types of Matching 1-3

y
nl
O
 XPath can create of the patterns.

se
 The match attribute of the xsl:template element

rU
supports a complex syntax.
Allows to express exactly which nodes to be

te

matched.

en
The select attribute of xsl:apply-templates,

C
xsl:value-of, xsl:for-each, xsl:copy-of,
h
and xsl:sort supports a superset of the syntax.
ec

Allows to express exactly which nodes to selected


pt


and which nodes not to be selected.
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 14 of 51


Types of Matching 2-3

y
nl
O
 Matching by name

se
 The source element is identified by its name, using the match attribute.
 The value given to the match attribute is called the pattern.

rU
Code Snippet
Matches all greeting elements in

te
<xsl:template match = “Greeting”>
the source document.
Matching by ancestry

en

 As in CSS, a match can be made using the element’s ancestry.

Code Snippet
C
h
<xsl:template match = “P//EM”> Matches any ‘EM’ element that
has ‘P’ as an ancestor.
ec

 Matching the element names


pt

 The most basic pattern contains a single element name which matches all
elements with that name.
rA

Code Snippet
Fo

<xsl:template match=”Product”> Matches Product elements and marks


<xsl:value-of select=”Product_ID”/> their Product_ID children bold.
</xsl:template>
@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 15 of 51
Types of Matching 3-3

y
nl
O
 Matching the root

se
 Enables all descendant nodes to inherit the properties on the root of document.
 Uses a single forward slash to represent the root.

rU
Code Snippet

te
<xsl:template match = “/”> Selects the root pattern.

en
 Matching by attribute

C
 As in CSS, a match can be made using the element’s ancestry.

Syntax
h
Uses square
ec

<xsl:template match = “ element name[‘attribute’ brackets to hold


(attribute-name)=attribute-value]”> the attribute name
and value.
pt

Code Snippet
rA

<xsl:template match=”Product”> Applies the templates to the


Fo

<xsl:apply-templates select=”@Units”/> non-existent Units attributes of


</xsl:template> Product elements.

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 16 of 51


Lesson 2 – XPath Expressions and Functions

y
nl
O
In this second lesson, XPath Expressions and

se
Functions, you will learn to:

rU
 State and explain the various XPath expressions

and functions.

te
 List the node set functions.

en
 List the boolean functions.
C
 State the numeric functions.
h
ec

 Describe the string functions.


pt

 Explain what result tree fragments are.


rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 17 of 51


XPath Expressions 1-2

y
nl
O
 Statements that can extract useful information from

se
the XPath tree.

rU
 Instead of just finding nodes, one can count them,
add up numeric values, compare strings, and more.

te
 Are like statements in a functional programming

en
language.

C
Evaluates to a single value.
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 18 of 51


XPath Expressions 2-2

y
nl
O
 Node-set

se
 An unordered group of nodes from the input document that match
an expression’s criteria.

rU
 Boolean
 XSLT allows any kind of data to be transformed into a boolean.

te
 Often done implicitly when a string or a number or a node-set
is used where a boolean is expected.

en
 Number

arithmetic. C
Numeric values useful for counting nodes and performing simple
h
 Numbers like 43 or –7000 that look like integers are stored as
ec

doubles.
 Non-number values, such as strings and booleans, are converted
pt

to numbers automatically as necessary.


rA

 String
 A sequence of zero or more Unicode characters.
Fo

 Other data types can be converted to strings using the string()


function.
@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 19 of 51
XPath Functions

y
nl
O
 XPath defines various functions required for XPath 2.0, XQuery

se
1.0 and XSLT 2.0.
The different functions are Accessor, AnyURI, Node, Error

rU

and Trace, Sequence, Context, Boolean,


Duration/Date/Time, String, QName and Numeric.

te
en
 Can be used to refine XPath queries and enhance the
programming power and flexibility of XPath.
 C
Each function in the function library is specified using a
h
function prototype that provides the return type, function
ec

name, and argument type.


pt

 If an argument type is followed by a question mark, the


rA

argument is optional; otherwise, the argument is required.


 Function names are case-sensitive.
Fo

 The default prefix for the function namespace is fn.


@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 20 of 51
Node-Set Functions 1-4

y
nl
O
 Take a node-set argument.
 Return a node-set or information about a particular node within a

se
node-set.
 Are name(), local-name(), namespace-uri() and root().

rU
Syntax

te
where,

en
name(): Returns name of current node or
the first node in specified node set.

C local-name(): Returns name of current


h
node or the first node in specified node set
ec
– without namespace prefix.

namespace-uri(): Returns namespace


pt

URI of current node or first node in


rA

specified node set.

root(): Returns root of tree to which the


Fo

current node or specified node belongs. This


will usually be a document node.

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 21 of 51


Node-Set Functions 2-4

y
nl
Code and Schema

O
se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 22 of 51


Node-Set Functions 3-4

y
nl
Stylesheet

O
se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 23 of 51


Node-Set Functions 4-4

y
nl
O
Formatted Output

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 24 of 51


Boolean Functions 1-5

y
nl
boolean(arg)

O

 Returns a boolean value for a number, string, or node-set.

se
Syntax

rU
fn:boolean(arg)

te
Code Snippet

en
<ul>
<li><b>boolean(0)</b> = <xsl:value-of select=”boolean(0)”/></li>

C
<li><b>boolean(1)</b> = <xsl:value-of select=”boolean(1)”/></li>
<li><b>boolean(-100)</b> = <xsl:value-of select=”boolean(-100)”/></li>
h
<li><b>boolean(‘hello’)</b> = <xsl:value-of select=”boolean(‘hello’)”/></li>
ec
<li><b>boolean(‘’)</b> = <xsl:value-of select=”boolean(‘’)”/></li>
<li><b>boolean(//book)</b> = <xsl:value-of select=”boolean(//book)”/></li>
</ul>
pt
rA

boolean(0) = false
boolean(1) = true
boolean(-100) = true
Fo

boolean(‘hello’) = true
boolean(‘’) = false
boolean(//book) = false
@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 25 of 51
Boolean Functions 2-5

y
nl
O
 not(arg)

se
 The sense of an operation can be reversed by using the
not() function.

rU
Syntax

te
en
fn:not(arg)

C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 26 of 51


Boolean Functions 3-5

y
nl
O
 not(arg)

se
 The sense of an operation can be reversed by using the
not() function.

rU
Code Snippet

te
<xsl:template match=”PRODUCT[not(position()=1)]”>

en
<xsl:value-of select=”.”/>
</xsl:template>

C
h
The template rule selects all product elements that are not the first child of their parents.
ec

Code Snippet
pt

<xsl:template match=”PRODUCT[position()!=1]”>
rA

<xsl:value-of select=”.”/>
</xsl:template>
Fo

The same template rule could be written using the not equal operator != instead.
@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 27 of 51
Boolean Functions 4-5

y
nl
O
 true()

se
 Returns the boolean value true.

rU
Syntax

te
fn:true()

en
Code Snippet
C
h
<xsl:value-of select=”true()”/>
ec
pt

true
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 28 of 51


Boolean Functions 5-5

y
nl
O
 false()

se
 Returns the boolean value true.

rU
Syntax

te
fn:false()

en
Code Snippet
C
<xsl:value-of select=”false() or false()”/>
h
<xsl:value-of select=”true() and false()”/>
ec

<xsl:value-of select=”false() and false()”/>


pt
rA

true
false
Fo

false

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 29 of 51


Numeric Functions 1-5

y
nl
O
 Return strings or numbers.
 Can be used with comparison operators in filter patterns such as:

se
 number(arg)

rU
 ceiling(num)
 floor(num)
round(num)

te

en
Syntax
where,
C number(arg): Returns numeric value of
argument. Argument could be a boolean, a string,
h
or a node-set.
ec

ceiling(num): Returns smallest integer greater


pt

than the number argument.


rA

floor(num): Returns largest integer that is not


greater than the number argument.
Fo

round(num): Rounds the number argument to


the nearest integer.
@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 30 of 51
Numeric Functions 2-5

y
nl
O
Stylesheet(1-3)

se
<?xml version=”1.0”?>
<?xml-stylesheet type=”text/xsl” href=”Number.xsl”?>

rU
<xsl:stylesheet version=”1.0” xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>
<xsl:output method=”html”/>
<xsl:template match=”/”>
<html>

te
<body>
<h3>Numeric Functions</h3>

en
<ul>
<li>
<b>number(‘1548’)</b> =

C
<xsl:value-of select=”number(‘1548’)”/>
</li>
h
<li>
<b>number(‘-1548’)</b> = <xsl:value-of select=”number(‘-1548’)”/>
ec

</li>
<li>
pt

<b>number(‘text’)</b> = <xsl:value-of select=”number(‘text’)”/>


</li>
rA

<li>
<b>number(‘226.38’ div ‘1’)</b> = <xsl:value-of select=”number(‘226.38’
div ‘1’)”/>
</li>
Fo

</ul>

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 31 of 51


Numeric Functions 3-5

y
nl
O
Stylesheet(2-3)

se
<ul>
<li>

rU
<b>ceiling(2.5)</b> = <xsl:value-of select=”ceiling(2.5)”/>
</li>
<li>
<b>ceiling(-2.3)</b> = <xsl:value-of select=”ceiling(-2.3)”/>

te
</li>
<li>

en
<b>ceiling(4)</b> = <xsl:value-of select=”ceiling(4)”/>
</li>
</ul>
<ul>
<li> C
h
<b>floor(2.5)</b> = <xsl:value-of select=”floor(2.5)”/>
</li>
ec

<li>
<b>floor(-2.3)</b> = <xsl:value-of select=”floor(-2.3)”/>
pt

</li>
<li>
rA

<b>floor(4)</b> = <xsl:value-of select=”floor(4)”/>


</li>
</ul>
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 32 of 51


Numeric Functions 4-5

y
nl
O
Stylesheet(3-3)

se
<ul>
<li>

rU
<b>round(3.6)</b> = <xsl:value-of select=”round(3.6)”/>
</li>
<li>
<b>round(3.4)</b> = <xsl:value-of select=”round(3.4)”/>

te
</li>
<li>

en
<b>round(3.5)</b> = <xsl:value-of select=”round(3.5)”/>
</li>
<li>

C
<b>round(-0.6)</b> = <xsl:value-of select=”round(-0.6)”/>
</li>
h
<li>
<b>round(-2.5)</b> = <xsl:value-of select=”round(-2.5)”/>
ec

</li>
</ul>
pt

</body>
</html>
rA

</xsl:template>
</xsl:stylesheet>
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 33 of 51


Numeric Functions 5-5

y
nl
O
Output

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 34 of 51


String Functions 1-5

y
nl
O
 String functions are used to:

se
 Evaluate

rU
 Format and manipulate string arguments
Convert an object to a string

te

en
 Different String functions are:
 string(arg) C
h
compare()
ec

 concat()
pt

 substring()
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 35 of 51


String Functions 2-5

y
nl
O
Syntax

se
where,

rU
string(arg): Returns string value of
the argument. The argument could be
a number, boolean, or node- set.

te
translate(): Returns first argument

en
string with occurrences of characters in
second argument string replaced by

C character at the corresponding position


in third argument string.
h
ec

concat(): Returns concatenation of


the strings.
pt

substring(): Returns substring from


rA

the start position to specified length.


Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 36 of 51


String Functions 3-5

y
nl
O
Code Snippet

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 37 of 51


String Functions 4-5

y
nl
O
Stylesheet

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 38 of 51


String Functions 5-5

y
nl
O
Output

se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 39 of 51


Result Tree Fragments

y
nl
O
 A portion of an XML document that is not a complete
node or set of nodes.

se
The only allowed operation in a result tree fragment

rU

is on a string.
The operation on the string may involve first

te

converting the string to a number or a boolean.

en
 Result tree fragment is an additional data type other
C
than four basic XPath data types, such as, string,
number, boolean, node-set.
h
ec

 A result tree fragment represents a fragment of the


result tree.
pt

It is not permitted to use the /, //, and [] XPath


rA


operators on Result tree fragments.
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 40 of 51


Lesson 3 – Working with different Styles

y
nl
O
In this last lesson, Working with different Styles,

se
you will learn to:

rU
 Explain how to switch between styles.

 Describe how to transform XML documents into

te
HTML using XSLT.

en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 41 of 51


Transformation of XML Document 1-2

y
nl
O
 Transformation is one of the most important and useful
techniques for working with XML.

se
XML can be transformed by changing its structure, its markup,

rU

and perhaps its content into another form.

te
 The most common reason to transform XML is to extend the
reach of a document into new areas by converting it into a

en
presentational format.
 Uses of transformation: C
h
 Formatting a document to create a high-quality presentational
ec

format.
Changing one XML vocabulary to another.
pt

Extracting specific pieces of information and formatting them in


rA

another way.
Fo

 Changing an instance of XML into text.


 Reformatting or generating content.
@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 42 of 51
Transformation of XML Document 2-2

y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 43 of 51


Transformation using XSLT Processor 1-2

y
nl
O
 Takes two inputs:

se
 An XSLT stylesheet to govern the transformation process.
An input document called the source tree.

rU

 The output is called the result tree.


The XSLT engine begins by reading in the XSLT stylesheet

te

and caching it as a look-up table.

en
 XPath locates the parts of XML document such as Element
C
nodes, Attribute nodes and Text nodes.
For each node the XSLT processes, it will look in the table for
h

the best matching rule to apply.
ec

 Starting from the root node, the XSLT engine finds rules,
pt

executes them, and continues until there are no more nodes


rA

in its context node set to work with.


 At that point, processing is complete and the XSLT engine
Fo

outputs the result document.

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 44 of 51


Transformation using XSLT Processor 2-2

y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 45 of 51


Transforming XML using XSLT 1-2

y
nl
Step 1

O

 Creates a normal XML document.

se
Code Snippet

rU
<?xml version=”1.0” encoding=”UTF-8”?>

 Step 2
Add the following lines.

te

Code Snippet

en
<?xml version=”1.0” encoding=”ISO-8859-1”?>

C
<xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” version=”1.0” >
...
</xsl:stylesheet>
h
Step 3
ec

 Set it up to produce HTML-compatible output.
 The <xsl:output> tag should be used with either “text” or “html” to output
pt

anything besides well-formed XML.


The default value is “xml”.
rA

Code Snippet
Fo

<xsl:stylesheet>
<xsl:output method=”html”/>
...
</xsl:stylesheet>
@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 46 of 51
Transforming XML using XSLT 2-2

y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 47 of 51


Transforming XML using XSLT Example 1-2

y
nl
O
 Example code of transforming XML documents into HTML using XSLT
processor

se
Code Snippet

rU
where,

te
Company: The root Company
element contains Product

en
child elements.

C Units: Attribute specifies the


h
units for products melting
point and boiling point.
ec
pt
rA
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 48 of 51


Transforming XML using XSLT Example 2-2

y
nl
O
Stylesheet

se
rU
te
en
C
h
ec
pt
rA

Output
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 49 of 51


Summary 1-2

y
nl
O
 XPath

se
 Notation for retrieving information from a document.

 Provides a common syntax for features shared by

rU
Extensible Stylesheet Language Transformations (XSLT)
and XQuery.

te
 Have seven types of node as Root, Element, Attribute,
Text, Comment, Processing instruction and Namespace.

en
 Used in the creation of the patterns.

C
XPath Expressions and Functions
h

ec

 The four types of expressions in XPath are

 Node-sets.
pt

 Booleans.
rA

 Numbers.

 Strings.
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 50 of 51


Summary 2-2

y
nl
O
 XPath Expressions and Functions (contd...)

se
 Functions defined for XPath are Accessor, AnyURI,
Node, Error and Trace, Sequence, Context,

rU
Boolean, Duration/Date/Time, String, QName and
Numeric.

te
 A Result tree fragment is a portion of an XML document
that is not a complete node or set of nodes.

en

C
Working with different styles
 Transformation is one of the most important and useful
h
techniques for working with XML.
ec

 To transform XML is to change its structure, its markup,


pt

and perhaps its content into another form.


 Transformation can be carried out using an XSLT
rA

processor also.
Fo

@ Aptech Limited Modern Markup for Data Interchange/ Module 7/ 51 of 51


y
nl
O
se
Session 8

rU
te
en
Introduction to JSON
C
h
ec
pt
rA
Fo
Objectives

y
nl
o Define and describe JavaScript Object

O
Notation (JSON)

se
o Explain the uses of JSON

rU
o List and describe the features of JSON
o Compare JSON with XML

te
o Describe the advantages of JSON

en
o Explain syntax and rules
o Explain literal notation in JavaScript and
conversion to JSON C
h
o Explain how to create a simple JSON file
ec
pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 8 2
Introduction: The Need of JSON
o XML parses using Document Object Model

y
nl
(DOM) with complex processes and no

O
cross-browser compatibility.

se
o JavaScript engine incorporates a markup

rU
language into an HTML Web page.

te
o The need of more natural format native to this

en
engine and overcome DOM limitations was felt.
C
h
ec
pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 8 3
What is JSON

y
nl
o A lenient text format derived from

O
JavaScript language and ECMAScript.

se
rU
o Platform-as well as language-independent.

te
o Implements conventions of the C-based

en
languages.

o More concise than XML. C


h
ec
pt

JSON is a lightweight, text-based, and open


rA

standard data-interchange format.


Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 8 4
Uses of JSON

y
nl
O
For serializing data to be sent over a network

se
For facilitating communication between incompatible

rU
platforms

te
For developing JavaScript based applications

en
C
For sending public data through Web services and
Application Programming Interfaces (APIs)
h
ec

For fetching data without hard coding in HTML


pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 8 5
History of JSON

y
nl
O
• Originally stated by Douglas Crockford
Coining the Term • Coined initially at the State Software
Company

se
rU
• To provide an abstraction layer for
The Co-founders’ Decision devising stateful Web applications without
plugins

te
en
• Implementing frames to send information
Participation by Other
Companies page
C
to the browsers without refreshing the
h
ec

• Implementing JavaScript as an
Crockford’s New object-based format for messaging
pt

Discovery • JSON.org in 2002


rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 8 6
Features of JSON 1-2

y
nl
o Standard structure

O
se
o Simplicity

rU
o Open-source

te
en
o Self-describing

o Lightweight C
h
ec

o Scalable/Reusable
pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 8 7
Features of JSON 2-2

y
nl
o Clean data

O
se
o Efficiency

rU
o Interoperability

te
en
o Extensibility

o Internationalization C
h
ec
pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 8 8
JSON versus XML 1-3

y
nl
Characteristic JSON XML

O
Data Types Offers scalar data types. Does not offer any idea of

se
data types.

rU
Array Support Provides native support. Expresses arrays by
conventions.

te
Object Support Provides native support. Expresses objects by

en
conventions.

Null Support C
Recognizes the value Mandates the use of
h
natively. xsi:nil on elements.
ec
pt

Comments Does not support. Provides native support


rA

(via APIs).
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 8 9
JSON versus XML 2-3

y
nl
Characteristic JSON XML

O
Namespaces Does not support Accepts namespaces to

se
namespaces. prevent name collisions.

rU
Formatting Is simple and offers more Is complex and needs
direct data mapping. more effort for mapping

te
application types to

en
elements.
Size Has very short syntax. Has lengthy documents.
Parsing in JavaScript
C
Needs no additional
application for parsing.
Implements XML DOM
and requires extra code
h
for mapping text to
ec

JavaScript objects.
pt

Parsing Has JSONPath. Has XPath specification.


Learning Curve Is not steep at all. Is steep.
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 8 10
JSON versus XML 3-3

y
nl
Characteristic JSON XML

O
Complexity Is complex. Is more complex.

se
Schema (Metadata) Has a schema but is not Uses many specifications
widely used. for defining a schema.

rU
Styling Has no special Has XSLT specification for
specification. styling an XML document.

te
Querying Has specifications such as Has XQuery specification

en
JSON Query Language that is widely used.
(JAQL) and JSONiq but
are not widely used.
Security C
Is less secure, as the Is more secure.
h
browser has no JSON
ec
parser.
pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 8 11
JSON versus XML Coding

y
nl
JSON XML

O
{ <students>
"students":

se
[ <student>
{"name":"Steve", <name>Steve</name>

rU
"age":"20",
"city":"Denver"}, <age>20</age>

te
<city>Denver</city>
{"name":"Bob",

en
"age":"21", </student>
"city":"Sacramento"}
C
<student>
]
<name>Bob</name>
h
}
ec

<age>21</age>
city>Sacramento</city>
pt
rA

</student>
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 8 12
Advantages of JSON

y
nl
o Quick serialization

O
se
o Efficient encoding

rU
o Faster parsing than XML and Yaml Ain't

te
Markup Language (YAML)

en
o Simpler to work with other languages
C
h
o Easy differentiation between data types
ec
pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 8 13
Limitations of JSON

y
nl
o Difficult in reading the format and precision of keeping

O
brackets in its place

se
o Following snippet illustrates complexity:

rU
{
"problems": [{
"Diabetes":[{

te
"medications":[{

en
"medicationsClasses":[{
"className":[{
"associatedDrug":[{
C
"name":"Asprin", "dose":"", "strength":"500 mg“
h
}]
ec

}]
}]
pt

}]
rA

}]
}]
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 8 14
JSON Data Structures

y
nl
o Support for two universally-supported data

O
structures:

se
rU
 Ordered list of values, which can be an array,
vector, list, or sequence.

te
 Unordered set of name/value pairs, which can be

en
a keyed list, object, record, struct, or a hash table.

C
h
ec
pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 8 15
JSON Arrays and Objects

y
nl
o Following figure represents an array

O
(ordered set) in JSON:

se
rU
te
en
o Following figure represents an object
(unordered set) in JSON: C
h
ec
pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 8 16
JSON Objects

y
o Following snippet represents a JSON object:

nl
O
{

se
rU
"Name":"Janet George",
"Street":"123 Ashley Street",

te
en
"City":"Chicago",
"Code":"60604"
C
h
}
ec
pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 8 17
JSON Syntax Rules

y
nl
O
se
Arrays • Always in square brackets

rU
te
en
C
• Always in curly braces
h
• Always in name value pairs
Objects
ec

separated by commas, with name


and value delimited by colon
pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 8 18
Literal Notation in JavaScript

y
nl
o Expresses fixed values literally.

O
se
o Is a floating-point number, integer, string,

rU
Boolean, array, or object.

te
o Contains zero or more expressions in a

en
sequence in an array literal.

C
o Contains a list of members in an object
h
literal.
ec
pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 8 19
JavaScript Array Literal Notation
1-2

y
nl
o Following snippet defines an array using

O
literal notation:

se
var fruits = ["Pineapple", "Orange", "Melon",

rU
"Kiwi", "Guava"];

te
alert (fruits[4] + " is one of the " +
fruits.length + " fruits.");

en
C
h
ec
pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 8 20
JavaScript Array Literal Notation
2-2

y
nl
o Following snippet defines an object using

O
literal notation:

se
var Address = {

rU
"Street": "123 New Mansion Street.",

te
"City": "Denver",

en
"PostalCode": 80123 };

C
alert ("The product will be sent to postal code
" + Address.PostalCode);
h
ec
pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 8 21
From JavaScript Literals to JSON

y
nl
o JSON has stricter rules:

O
▪ Object member name to be a valid JSON

se
string in quotation marks

rU
▪ A member value or an array element in
JSON to be confined to a restricted set

te
en
o JSON does not support Date/time literals.
C
h
ec
pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 8 22
Creating a JSON File

y
nl
o The JSON.stringify() function converts

O
a JavaScript value to a serialized JSON

se
string.

rU
o Following is the syntax:

te
en
JSON.stringify(value [, replacer] [,
space])
where, C
h
ec

value identifies a JavaScript value.


pt

replacer represents a function or array.


rA

space adds line break and white space.


Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 8 23
Summary 1-2

y
nl
o JSON is a text-based and open standard format
derived from JavaScript and ECMAScript.

O
se
o JSON is lighter, more scalable, less complex,

rU
quicker to learn, and faster to process than
XML.

te
en
o JSON works independently of any language or
platform.
C
h
o JSON is preferred for serializing and de-
ec

serializing data to be exchanged on the Web.


pt
rA

o Data in JSON format is represented as


name/value pairs.
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 8 24
Summary 2-2

y
nl
o JSON supports two data structures namely,
array as ordered list and objects as unordered

O
set of name/value pairs.

se
rU
o While syntax for JavaScript’s literal values is
flexible, JSON follows stricter rules for arrays

te
and objects.

en
o A JSON file is saved with .json extension.
C
h
o The JSON.stringify() function is used to
ec

transform a JavaScript object to a JSON string.


pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 8 25
y
nl
O
se
Session 9

rU
te
en
Work with JSON Data
C
h
ec
pt
rA
Fo
Objectives

y
nl
o Identify data types supported by JSON

O
se
o Explain how to represent complex data with

rU
JSON

te
o Explain how to execute serialization and de-

en
serialization of JSON with JavaScript

C
o Describe tools and editors that can be used
h
to work with JSON
ec
pt

o Describe the syntax and schema of a JSON


rA

document
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 9 2
JSON Data Types 1-4

y
nl
The two primary data types supported by JSON

O
are primitive and structure.

se
rU
te
en
C
h
ec
pt

In JSON, the type of a variable is recognized


rA

automatically during the parsing phase.


Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 9 3
JSON Data Types 1-4

y
nl
• It is a floating-point format (double

O
precision).
• Does not accept Octal, hexadecimal, NaN,

se
and Infinity values.
Number • The types allowed are integer, fraction, and

rU
exponent.
• Syntax {"string": number_value,
.......}

te
en
• A series of zero or other Unicode

C
characters within double quotes and
backslash escapes.
h
• Delimited with double-quotation marks.
String
ec

• A character denotes a single character


string.
pt

• Syntax {"string":"string value",


rA

.......}
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 9 4
JSON Data Types 2-4

y
nl
• Has only two values: true and false.

O
• Using quotes for Boolean values treats
Boolean them as String values.

se
• Syntax {string: true/false,
.......}

rU
te
en
Null • Is an empty type.

C
h
ec
pt

• A white space can appear between the


White Space characters in string values to make code
rA

more comprehensible.
• Syntax {string:" ",....}
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 9 5
JSON Data Types 3-4
Arrays allow storing various values of the same type in one

y
nl
variable.

O
Syntax [value, .......]

se
rU
The characteristics of an array in JSON are:

te
It is a sequential collection of values, not necessarily of

en
the same type.

C
Indexing begins at 0 or 1.
h
ec
pt

It is enclosed in square brackets [.].


rA
Fo

Each value is set apart by comma (,).


<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 9 6
JSON Data Types 4-4
An object is an independent data type, having its own

y
attributes.

nl
O
Syntax {string : value, .......}

se
The characteristics of an object in JSON are:

rU
It is a non-sequential (having no order) set of key/value pairs.

te
en
It is enclosed in curly braces {.}.

C
h
Each key/value pairs are set apart by comma (,) and every key
is proceeded by colon (:).
ec
pt

The keys are only strings, each differing from the other.
rA

It is used when the key names are random strings.


Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 9 7
JSON Value

y
o In JSON, value can be of any primitive and

nl
structure data type.

O
se
o A JSON value can be a number, string, Boolean

rU
such as true or false, null, object, or an array.

te
o The following Code Snippet shows some examples

en
of JSON values:
C
h
var emp-no = 1234;
ec

var name = "Sherill";


var experience = null;
pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 9 8
Storing Different Values in Arrays

y
nl
JSON arrays can store elements of different types.

O
Following is a sample code:

se
rU
[ 456, "Dog", 123, "Frog", true ]

te
o Elements 0 and 2 in the array are of the type

en
Number.
o Elements 1 and 3 are of String type. C
h
o Element 4 is a Boolean type.
ec
pt

In JSON, arrays can also contain other arrays.


rA

This is called nesting of arrays.


Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 9 9
Data Structures Supported by JSON

y
nl
In JSON, the built-in data structures can be

O
used to develop other data structures.

se
The two data structures supported by JSON

rU
are:

te
Collection of

en
Ordered List of Values
Name/Value Pairs
C
h
ec

Supported by different Includes arrays, lists, and


programming languages, in vectors.
pt

the form of objects.


rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 9 10
Collections: Maps, Sets, and Lists

y
nl
• The elements in a list follow
a defined order and copies

O
List or duplicates are allowed.
• The elements are positioned

se
in a specific location.

rU
• Collection without duplicate

te
elements.
• In JSON, it is not possible to
Set

en
Collections avoid replicas, however, the
parser removes the
C duplicates while serializing
the data.
h
ec

Map • A type of data structure that


pt

mainly aids in quick


searching of data.
rA

• Accepts data in the form of


key and value pairs and each
Fo

key is distinct.
<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 9 11
JSON Schema
o JSON schema specifies the rules that defines the

y
nl
structure of a JSON document.

O
o The two main aspects that define why a JSON schema

se
language is required are:

rU
To specify JSON To validate JSON

te
data structures data structures

en
Handy when the
JSON-based Web
C Handy when
h
validating JSON
services need to be
ec

documents from
made accessible to
other applications
a large audience
pt
rA

Helps to avoid
parsing an invalid
Fo

data structure in
valid JSON
<?xml> documents
© Aptech Ltd. Modern Markup for Data Interchange/Session 9 12
Schema Overview
o Application/schema+json is the media type described

y
by JSON schema and prescribes the design of JSON documents.

nl
O
o It offers provision for defining the structure of the documents

se
with respect to the permitted values, descriptions, and decoding
connections to other resources.

rU
te
o The JSON schema format is arranged in the following individual
definitions:

en
Core Schema C
• Explains a JSON structure and states
h
Specification valid elements in it.
ec
pt

Hyper Schema • Explains the elements in a JSON


rA

structure that can be considered as


Specification hyperlinks.
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 9 13
JSON Comments

y
o JSON does not have any provision for documentation or

nl
comments.

O
se
o However, comments are still supported by a few JSON
parsers and must be provided within /* … */.

rU
{

te
"id":2,

en
"title":"The fallen Hero", /* This books
is about Harvey Dent */
"noOfCopies":14,
C
h
"tags":[
ec

"BatMan",
pt

"Gowtam"
rA

],
}
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 9 14
Creating and Parsing JSON Messages
with JavaScript

y
nl
O
The JSON.stringify() method allows converting a
JavaScript object into a JSON String.

se
rU
The JSON.parse() method allows parsing a JSON
object using JavaScript.

te
en
C
It converts a JSON String to an object in JavaScript.
h
ec

Start by defining a string in JSON format, using the


pt

method for conversion, and then looping through the


rA

attributes for printing its values.


Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 9 15
JSON with Developer Tools on
Browser

y
nl
o There are many plugins or extensions that help in validating and
formatting a JSON document or a JSON HTTP response.

O
se
o One such extension in Firefox is JSONView, which allows viewing a

rU
JSON document.

te
o With JSONView the JSON document is displayed in the browser.

en
o JSONView displays the raw text even though the JSON document
has errors. C
h
ec

o Chrome also offers JSONView for validating a JSON document.


pt
rA

o For modifying the JSON values during runtime, it is recommended


converting the JSON document to a JavaScript object before using
Fo

the built-in browser developer tools.


<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 9 16
Online Tools and Editors

y
o JSONLint is an open source project that helps in validating

nl
JSON data.

O
o Using JSONLint or any other online tool is easy, as it only

se
requires copying the JSON data to its online editor.

rU
o In case of an error, the output similar to the following is displayed:

te
en
C
h
ec

o Output shows the line number and the type of error that occurred.
pt
rA

o If everything is fine, the following output is displayed:


Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 9 17
Online JSON Viewers 1-2

y
nl
o While working with JSON, it is often required to use a JSON
viewer to see how the document will look in the browser.

O
se
o One of the most widely used is jsonviewer.stack.hu and JSON

rU
text can be directly copied to this online viewer.

te
o The following screenshot shows the jsonviewer.stack.hu:

en
C
h
ec
pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 9 18
Online JSON Viewers 2-2

y
nl
o Another way of supplying data to the viewer is by clicking
Load JSON data.

O
se
rU
te
en
C
h
o The viewer takes data from the URL you provide and loads
ec

data from that source.


pt
rA

o Once the JSON code is visible in the viewer, clicking Format


formats the code.
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 9 19
Summary

y
nl
o The two primary data types supported by JSON are
primitive and structure.

O
o The primitive types are String, Number, Null, and

se
Boolean. The structure types are Array and Object.
o Arrays allow storing various values of the same type in

rU
one variable.
o An object is an independent data type, having its own

te
attributes.

en
o In JSON, arrays can also contain other arrays. This is
called nesting of arrays.
C
o The two data structures supported by JSON are Collection
of Name/Value Pairs and Ordered List of Values.
h
o JSON schema specifies the rules that defines the structure
ec

of a JSON document.
pt

o JSON does not have any provision for documentation or


comments.
rA

o JSONLint is an open source project that helps in


validating JSON data.
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 9 20
y
nl
O
se
Session 10

rU
te
en
JSON in Real World
C
h
ec
pt
rA
Fo
Objectives

y
nl
o Describe the support of different browser

O
and programming languages for JSON

se
o Describe JSON content types

rU
o Explain how to use JSON for Web and Data
Storage

te
o Compare JSON with relational databases

en
o Identify security and data portability issues
with respect to JSON
C
h
ec
pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 10 2
Support for JSON

y
nl
o Each major programming language can

O
incorporate JSON, natively or through

se
libraries.

rU
o This is possible by incorporating two

te
functionalities namely:

en
▪ Parsing
▪ Formatting
C
h
ec
pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 10 3
JSON with C# and Java

y
nl
o These languages support statically typed

O
classes and not objects of HashMap or

se
Dictionary type.

rU
o The solution is to use a library along with a

te
custom code for converting these structures

en
into static type instances.

C
o The Gson library from Google is one such
h
library that resolves the issue.
ec
pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 10 4
What is Gson

y
nl
O
Is an open-source Java library for transforming a Java
object to JSON data and vice-versa.

se
Offers easy mechanisms such as constructor (factory

rU
method) and toString().

te
Functions well with arbitrary Java objects, involving the

en
pre-existing ones.

C
Serializes and deserializes huge data without any issues.
h
ec

Is convenient to learn and use by only using toJson()


pt

and fromJson().
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 10 5
JSON with PHP

y
nl
o From 5.2.0 version, the extension for JSON

O
is packaged into PHP.

se
rU
o Following table shows the functions for
encoding and decoding JSON structures:

te
Function Description

en
Serializes the stated array or object
json_encode
C
and returns it in the JSON format if
successful or FALSE.
h
ec

Deserializes JSON data and returns


json_decode
the suitable PHP type.
pt

Returns the error that happened


json_last_error
rA

last.
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 10 6
MIME Type of JSON

y
nl
o Is also called media type or content type.

O
se
o Is a two-part identifier composed of a type

rU
and subtype isolated by a slash.

te
o Aids in identifying the type of formatted

en
content being sent over the Web.

o Is ‘application/json’ for JSON. C


h
ec

o Is unofficially ‘text/json’ or ‘text/Javascript’.


pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 10 7
Applications of JSON

y
nl
• For exchanging data to and from APIs
APIs • Popular in social networking sites implementing API

O
se
• For storing data easily in NoSQL databases, as JSON is

rU
NoSQL easily convertible into JavaScript

te
• For replacing XML when new data is fetched by a

en
Asynchronous
JavaScript and JSON loaded Web page in a browser
(AJAJ)

C
h
• For replacing Simple Object Access Protocol (SOAP) or
JSON-RPC (Remote
ec
XML-RPC and for sending several calls and
Procedure Call ) notifications to a server
pt
rA

• For storing metadata


Package Management
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 10 8
JSON HTTP and Files

y
nl
o JSON displays the data fetched from a Web

O
server efficiently through the

se
XMLHttpRequest object.

rU
o The object exchanges data in the

te
background, which prevents reloading the

en
full page for updates.

C
o Following is the syntax of initializing the
h
object:
ec
pt

variable = new XMLHttpRequest();


rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 10 9
JSON for Data Storage

y
nl
O
JSON helps in storing data fetched from services and

se
applications.

rU
• Reasons: Simplicity and ‘just adequate structure’

te
en
RDBMS and Structured Query Language (SQL) are now
C
replaced by NoSQL databases for developers who prefer
JSON.
h
ec

• Trends: More implementation of JSON-centric


document databases, JSON in traditional RDBMS’
pt

• Reasons: Developer-friendly and agility


rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 10 10
Document versus Relational
Databases

y
nl
O
Document-oriented Relational Databases
Databases

se
Coupling Tighter due to the need Loose
to navigate the

rU
document while
querying

te
Portability and No, due to new query Yes, due to well-defined
Standardization language setup for each SQL query core

en
document store
Optimization
C
Restricted, due to no
abstraction between the
Fully optimized, due to
abstraction ensured by
h
logical data structure highly queryable stored data
and its physical storage
ec
definition
Consistency and No, due to no support Yes
pt

Normalization for constraints


rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 10 11
Benefits of Using JSON in RDBMS

y
nl
O
se
rU
te
en
Decision Power for: JSON Query as Native
• The data portions to SQL:
be abstracted
• Areas where flexibility C • Allows querying
randomly across JSON
h
is essential records in tables
ec

• Locations where strict • Allows expanding


schema and constraints systems to use JSON
pt

are essential
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 10 12
JSON versus RDBMS Data Models

y
nl
O
JSON RDBMS

se
Storage Structure Is array or object. Is a table.

rU
Metadata Can be in a schema, but Is stored in a schema
is not pre-created. generated while creating a
table.

te
Data Retrieval Uses evolving languages Uses SQL.

en
namely, JSON Query
Language (JAQL) and

Sorting
JSONiq
C
Is only for arrays. Is for tables.
h
ec
Learning Curve Is smoother. Is time consuming.
pt

Application Is used in several Is in the form of many


rA

programming commercial and open-


languages. source databases.
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 10 13
Security Issues in JSON

y
nl
o JSON, as a flexible subset of JavaScript, has

O
some security issues.

se
rU
o In the following figure the root cause
increasing the risk of malicious script, a

te
major
var issue:
data = eval('(' + JSONresponse + ')');

en
C
h
ec
pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 10 14
Issues with Eval()

y
nl
o Most JSON text is syntactically JavaScript.

O
se
o JavaScript interpreter converts JSON into a

rU
JavaScript object without validation.

te
o This increases the risk of authentication

en
forgery, identity and data theft, and misuse
of resources.
C
h
o Solution: JSON.parse() and
ec

JSON.stringify() functions processing


pt

text only in JSON format


rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 10 15
XSS and CSRF Issues

y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 10 16
XSS and CSRF Solution

y
nl
O
se
rU
te
By using the jsonify() function

en
C
h
ec
pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 10 17
Implementation Issues

y
nl
O
se
Denial of • Renders a resource or a machine
Service (DoS) on a network unavailable to its

rU
Attack targeted users

te
en
Mass C
• Maltreats the functioning pattern
h
of record in a Web application for
Assignment
ec

illegitimately changing
Vulnerability confidential data items
pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 10 18
Portability Issues

y
nl
O
Unicode Line Null UTF JSON Unsupported
Terminators Character Encoding Numbers Data Types

se
• Are allowed in • Is allowed in • Involves • Involve • Involve Error,
JSON without JSON string if having a few numbers and Date,

rU
being escaped escaped as escaped floating Undefined,
and that they “\u0000”, characters integers, Function, and
need to be which creates using UTF-16 which are Regular

te
backslash issues with C surrogate distinguished Expression,
escaped for strings. pairs, which a by some which JSON

en
portability. few JSON languages does not
parsers do not only, not by accept.

C
h recognize. all.

• Have no
specifications
ec

for rounding,
overflow, and
pt

precision loss.
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 10 19
Handling JSON Securely

y
nl
o Avoiding eval() by using a JavaScript

O
library available at www.json.org, such as

se
JSON sans eval()

rU
o Ensuring data integrity via XMLHttpRequests

te
en
C
h
ec
pt
rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 10 20
Summary 1-2

y
nl
o Modern programming languages incorporate
JSON via libraries or native parsing support.

O
se
o Gson is an open-source Java library for

rU
converting a Java object into JSON and vice-
versa.

te
en
o Formal MIME type for JSON text is
‘application/json’.
C
h
o JSON is used in several applications, such as in
ec

APIs, NoSQL databases, RDBMS’, Web


pt

development, and application packages.


rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 10 21
Summary 2-2

y
nl
o Unlike relational databases, JSON does not
have tables, pre-created metadata, and

O
support for SQL.

se
rU
o Using eval() for parsing JSON data can lead
to security attacks such as XSS and CSRF.

te
en
o JSON is prone to DoS attack and vulnerability
of mass assignment.
C
h
o Developers can secure JSON structures by
ec

replacing eval() with a JavaScript library


pt

and/or using XMLHttpRequests.


rA
Fo

<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 10 22

You might also like