Use Only: Introduction To XML
Use Only: Introduction To XML
Use Only: Introduction To XML
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
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
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
y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo
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
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
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
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
y
nl
O
XML markup defines the physical and logical layout of
se
the document.
rU
XML markup divides a document into separate
te
en
C
A document consists of one outermost element,
h
ec
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
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
y
nl
O
In this second lesson, Exploring XML, you will learn
se
to:
rU
Describe the structure of an XML document.
te
en
State the functions of editors for XML and list the
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
of the alias.
Fo
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
document.
rA
y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo
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
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
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
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
y
nl
O
Main Functions:
Add opening and closing tags to the code
se
rU
Verify XML against a DTD/Schema
Perform series of transforms over a document
te
en
Display the line numbers
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
se
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
Validating parser
rA
Fo
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
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
y
nl
O
In this third lesson, Working with XML, you will learn
se
to:
rU
Explain the steps towards building an XML document.
te
en
C
h
ec
pt
rA
Fo
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
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
y
nl
O
se
rU
te
Characters are encoded using
various encoding formats. The
en
character encoding is declared in
encoding declaration.
XML document.
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.
y
nl
O
Structure
se
Semantic
rU
Style
te
en
C
h
ec
pt
rA
Fo
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
y
nl
O
Code Snippet
se
rU
<Book>
<Name>Good XML</Name>
te
<Cost>$20</Cost>
</Book>
en
C
h
ec
pt
rA
Fo
y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo
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
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
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
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
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
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
processing instruction
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
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
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
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
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
y
nl
O
Predefined Description Output
se
Entity
rU
< produces the left angle <
bracket
te
> produces the right angle >
en
bracket
& C
produces the ampersand &
h
' produces a single quote ‘
ec
character
pt
character
Fo
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>"Tom Smith"</To>
C
<Address>&address;</Address>
<City>&city;</City>
h
<Body>
ec
</Body>
rA
<From>ARNOLD</From>
</Letter>
Fo
y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo
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
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
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
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>
Fo
</Program>
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
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
<Player Sex=”male”>
rA
<FirstName>Tom</FirstName>
<LastName>Federer</LastName>
Fo
</Player>
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
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
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
y
nl
O
In this first lesson, XML Namespaces, you will
se
learn to:
rU
Identify the need for a namespace.
te
en
C
h
ec
pt
rA
Fo
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
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
y
nl
O
Elements are distinguished by using namespaces.
se
A namespace is a collection of names.
rU
te
en
Combine documents from different sources
C
Identify the source of elements or attributes
h
ec
pt
rA
Fo
y
nl
O
In this last lesson, Working with Namespaces
se
syntax, you will learn to:
rU
Explain the syntax for XML namespaces.
te
en
Discuss how to use default namespaces.
C
h
ec
pt
rA
Fo
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
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
y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo
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
URI
rA
Resource
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
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
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
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
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>
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
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
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
y
nl
O
XML Namespaces
se
Namespaces distinguish between elements and attributes
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
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
y
nl
O
In this first lesson, Document Type Definition, you
se
will learn to:
rU
Define what is meant by a DTD.
te
en
C
h
ec
pt
rA
Fo
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
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
en
C
h
ec
pt
rA
Fo
y
nl
O
In this second lesson, Working with DTDs, you will
se
learn to:
rU
Describe the structure of a DTD.
te
en
Describe what is meant by document type declarations.
C
h
ec
pt
rA
Fo
y
nl
O
Element Declarations
se
Attribute Declarations
rU
Entity Declarations
te
en
C
h
ec
pt
rA
Fo
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
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
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
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
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
y
nl
O
DTDs can be classified as Internal or External.
se
Internal DTDs
rU
te
en
C
h
ec
pt
rA
Fo
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
y
nl
O
In this third lesson, Valid XML Documents, you will
se
learn to:
rU
Define document validity.
te
en
C
h
ec
pt
rA
Fo
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
y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo
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
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
<Cc> </Cc>
<Bcc> </Bcc>
rA
</Message>
<Signature> </Signature>
</Mail>
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
y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo
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
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
Mixed Content
Fo
y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo
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
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
ENTITY An entity
rA
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
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
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
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
y
nl
Code Snippet
O
se
rU
te
en
C
h
ec
pt
rA
Fo
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
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
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
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
hp.txt
Head Phones
ch.txt
Fo
Charger
sk.txt
Starters Kit
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.
pt
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
and a value.
Fo
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
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
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
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
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
y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo
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
y
nl
O
Code Snippet
se
rU
XML File: message.xml
<?xml version= "1.0"?>
te
<Message>
Hello World!
en
</Message>
">
<xsd:element name= "MESSAGE " type= "xsd:string "/>
pt
</xsd:schema>
rA
Fo
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
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
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
Fo
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
y
nl
O
Drawbacks of using DTDs are:
se
DTDs are written in a non-XML syntax
rU
te
en
C
DTDS offer limited data typing
h
ec
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
string.
rA
</code>
</program>
Fo
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
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:sequence>
</xs:complexType>
</xs:element>
Fo
</xs:schema>
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
y
nl
O
In this second lesson, Exploring XML Schemas,
se
you will learn to:
rU
List the data types supported by schemas.
te
en
C
h
ec
pt
rA
Fo
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
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
written as a string
rA
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
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
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
<Price>500</Price>
Fo
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
<image
flower="http://www.creativepictures.com/gallery/flower.gif
" />
Fo
y
nl
Additional data types are derived from the basic built-in data types,
O
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
Decimal
rA
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
Syntax: time
Fo
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
<BeginAt>09:30:10.5</BeginAt>
y
nl
Creating a schema using XML schema vocabulary is like creating
O
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
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
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
y
nl
O
In this third lesson, Working with Complex Types,
se
you will learn to:
rU
Describe complex type elements.
te
en
Explain element content and mixed content.
C
Describe how grouping can be done.
h
ec
pt
rA
Fo
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
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: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
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
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
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
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
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
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
rA
>maxOccurs 0
Fo
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
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: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:sequence>
</xs:complexType>
Fo
</xs:element>
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
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
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:group>
Fo
y
nl
O
In this last lesson, Working with Simple Types,
se
you will learn to:
rU
Describe simple types.
te
types.
en
Explain restrictions and facets.
C
Identify the usage of attributes.
h
ec
pt
rA
Fo
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
y
nl
O
Built-in simple type
se
User-defined simple type
rU
te
en
C
h
ec
pt
rA
Fo
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
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
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
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>
y
nl
It can be specified for the simpleType elements.
O
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>
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
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
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
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
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:complexType>
....
Fo
y
nl
O
XML Schema
se
An XML Schema is an XML based alternative to DTDs, which
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
@ 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
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
elements.
The grouping constructs in XML schema specify the order of
Fo
XML elements.
y
nl
O
Working with Simple Types
se
The elements of simple type describe the content and data
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
prescribed values.
rA
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
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
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
y
nl
O
Cascading Style Sheet (CSS)
se
It allows you to control the appearance of data in
rU
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
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
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
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
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
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
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
CSS, it informs the browser that it does not have to download a .css file.
rA
Code Snippet
Fo
y
nl
O
In this second lesson, Selectors in CSS, you will
se
learn to:
rU
Identify simple selectors in CSS.
te
en
Describe ID selectors.
C
h
ec
pt
rA
Fo
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
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
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
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
y
nl
O
In this third lesson, Properties and Values, you will
se
learn to:
rU
State and describe how to use color properties.
te
en
Describe the other properties such as margins,
borders, padding.
C
Explain briefly about positioning and alignment.
h
ec
pt
rA
Fo
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
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
y
nl
O
Code Snippet
se
rU
te
en
C
h
ec
pt
rA
Fo
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
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
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
y
nl
O
Style Sheet
se
rU
te
Output
en
C
h
ec
pt
rA
Fo
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
y
nl
O
Code Snippet
se
rU
te
en
C
h
ec
pt
rA
Fo
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
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
y
nl
O
Code Snippet
se
rU
te
en
C
h
ec
pt
rA
Fo
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
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
y
nl
O
Code Snippet
se
rU
te
en
C
h
ec
pt
rA
Fo
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
Output
Fo
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
y
nl
O
Code Snippet
se
rU
te
en
C
h
ec
pt
rA
Fo
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
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
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
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
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
the right edge of the parent element adhering to CSS length units
y
nl
O
Code Snippet
se
rU
te
en
C
h
ec
pt
rA
Fo
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
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
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
y
nl
O
Code Snippet
se
rU
te
en
C
h
ec
pt
rA
Fo
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
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
y
nl
O
Code Snippet
se
rU
te
en
C
h
ec
pt
rA
Fo
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
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
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
y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo
y
nl
O
Style Sheets
Style Sheets are a set of rules that define the appearance of data.
se
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
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
y
nl
O
In this first lesson, Introduction to XSL, you will
se
learn to:
rU
Define Extensible Stylesheet Language (XSL),
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
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
en
document.
C
Display hypertext links in blue.
h
ec
mobile phones.
Fo
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
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
en
C
h
ec
pt
rA
Fo
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
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
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
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
Highly effective and easy to learn for Designed to meet the needs of more
Fo
y
nl
O
In this last lesson, Working with XSL, you will
se
learn to:
rU
Explain XSL templates.
te
en
State how to use xsl:value-of element.
y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo
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
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
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
y
nl
O
Code Snippet
se
rU
te
en
C
h
ec
pt
rA
Fo
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
y
nl
O
Output
se
rU
te
en
C
h
ec
pt
rA
Fo
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
y
nl
O
Code Snippet
se
rU
te
en
C
h
ec
pt
rA
Fo
y
nl
O
Style Sheet
se
rU
te
en
where,
select=”Title”
y
nl
O
Output
se
rU
te
en
C
h
ec
pt
rA
Fo
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
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 >).
y
nl
O
Code Snippet
se
rU
te
en
C
h
ec
pt
rA
Fo
y
nl
O
Style Sheet
se
rU
te
en
C
h
ec
where,
xsl:value-of select=”FirstName”
pt
y
nl
O
Output
se
rU
te
en
C
h
ec
pt
rA
Fo
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
y
nl
O
Code Snippet
se
rU
te
en
C
h
ec
pt
rA
Fo
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
</xsl:for-each>
End of for-each loop.
y
nl
O
Output
se
rU
te
en
C
h
ec
pt
rA
Fo
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
no
If the value is no, a > will appear as a > in the text.
Fo
y
nl
O
Code Snippet
se
rU
te
en
C
h
ec
pt
rA
Fo
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.
y
nl
O
Output
se
rU
te
en
C
h
ec
pt
rA
Fo
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.
y
nl
O
Code Snippet
se
rU
te
en
C
h
ec
pt
rA
Fo
y
nl
O
Style Sheet
se
rU
te
en
C
h
ec
where,
pt
position()
rA
format=”I.”
User-provided roman number starts with I.
y
nl
O
Output
se
rU
te
en
where,
C
h
1.Water Bottle, I.Water Bottle
ec
The fourth item is numbered with number 4 and roman number IV.
rA
Fo
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
y
nl
O
Code Snippet
se
rU
te
en
C
h
ec
pt
rA
Fo
y
nl
O
Style Sheet
se
rU
te
en
C
h
ec
pt
rA
where,
<xsl:for-each select=”Catalog/Book”>
Fo
y
nl
O
Output
se
rU
te
en
C
h
ec
pt
rA
Fo
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
y
nl
O
Code Snippet
se
rU
te
en
C
h
ec
pt
rA
Fo
y
nl
Style Sheet
O
se
rU
te
en
C
h
ec
pt
rA
Fo
y
nl
Style Sheet
O
se
rU
te
en
C
h
ec
pt
where,
rA
xsl:otherwise
If all the conditions are false, this block is executed. Here, all other book details are printed in normal
background color.
y
nl
O
Output
se
rU
te
en
C
h
ec
pt
rA
Fo
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
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.
y
nl
O
Code Snippet
se
rU
te
en
C
h
ec
pt
rA
Fo
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.
y
nl
O
Output
se
rU
te
en
C
h
ec
pt
rA
Fo
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
en
XML document.
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
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
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
y
nl
O
XSLT
se
Language for transforming XML documents into XML, HTML or
text.
rU
XQuery
Builds on XPath.
te
en
C
h
ec
pt
rA
Fo
y
nl
O
Provides single syntax that you can use for:
se
Queries
Addressing
rU
Patterns
Concise, simple, and powerful.
te
Benefits:
en
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
y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo
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
te
en
node too.
Fo
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
namespace nodes.
rA
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
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
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
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
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
and which nodes not to be selected.
rA
Fo
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
Code Snippet
C
h
<xsl:template match = “P//EM”> Matches any ‘EM’ element that
has ‘P’ as an ancestor.
ec
The most basic pattern contains a single element name which matches all
elements with that name.
rA
Code Snippet
Fo
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
Code Snippet
rA
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
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
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
String
A sequence of zero or more Unicode characters.
Fo
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
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
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.
y
nl
Code and Schema
O
se
rU
te
en
C
h
ec
pt
rA
Fo
y
nl
Stylesheet
O
se
rU
te
en
C
h
ec
pt
rA
Fo
y
nl
O
Formatted Output
se
rU
te
en
C
h
ec
pt
rA
Fo
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
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
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
true
false
Fo
false
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
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
<li>
<b>number(‘226.38’ div ‘1’)</b> = <xsl:value-of select=”number(‘226.38’
div ‘1’)”/>
</li>
Fo
</ul>
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
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
y
nl
O
Output
se
rU
te
en
C
h
ec
pt
rA
Fo
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
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
y
nl
O
Code Snippet
se
rU
te
en
C
h
ec
pt
rA
Fo
y
nl
O
Stylesheet
se
rU
te
en
C
h
ec
pt
rA
Fo
y
nl
O
Output
se
rU
te
en
C
h
ec
pt
rA
Fo
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
operators on Result tree fragments.
Fo
y
nl
O
In this last lesson, Working with different Styles,
se
you will learn to:
rU
Explain how to switch between styles.
te
HTML using XSLT.
en
C
h
ec
pt
rA
Fo
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
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
another way.
Fo
y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo
y
nl
O
Takes two inputs:
se
An XSLT stylesheet to govern the transformation process.
An input document called the source tree.
rU
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
y
nl
O
se
rU
te
en
C
h
ec
pt
rA
Fo
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
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
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.
y
nl
O
Stylesheet
se
rU
te
en
C
h
ec
pt
rA
Output
Fo
y
nl
O
XPath
se
Notation for retrieving information from a document.
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
Node-sets.
pt
Booleans.
rA
Numbers.
Strings.
Fo
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
processor also.
Fo
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.
<?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
<?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
<?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
(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
<?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
<?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
<?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
<?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
<?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
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
<?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
.......}
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
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
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
<?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
<?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
<?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
<?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
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
<?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
<?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
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
<?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
<?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
<?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
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
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.
<?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
<?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
<?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
<?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
<?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
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
<?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
<?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
<?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
<?xml>
© Aptech Ltd. Modern Markup for Data Interchange/Session 10 22