Unit VI_3
Unit VI_3
<html>
<head><title>Example</title></head>.
<body>
<h1>This is an example of a page.</h1>
<h2>Some information goes here.</h2>
</body>
</html>
Example of an XML Document
<?xml version=“1.0”/>
<address>
<name>Alice Lee</name>
<email>[email protected]</email>
<phone>212-346-1234</phone>
<birthday>1985-03-22</birthday>
</address>
XML Syntax Rules
All XML Elements Must Have a Closing Tag
In HTML, some elements do not have a closing tag:
<p>This is a paragraph.
<br>
In XML, it is illegal to omit the closing tag.
All elements must have a closing tag:
<p>This is a paragraph.</p>
<br />
INCORRECT:
<note date=12/11/2007>
<to>Tove</to>
<from>Jani</from>
</note>
CORRECT:
<note date="12/11/2007">
<to>Tove</to>
<from>Jani</from>
</note>
The error in the first document is that the date attribute in the
note element is not quoted.
Entity References
Some characters have a special meaning in XML.
If you place a character like "<" inside an XML element, it will
generate an error because the parser interprets it as the start of a
new element.
This will generate an XML error:
<message>if salary < 1000 then</message>
To avoid this error, replace the "<" character with an entity
reference:
<message>if salary < 1000 then</message>
There are 5 pre-defined entity references in XML:
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
<footer>&writer; ©right;</footer>
</note>
Why Use a DTD?
/bookstore/book[1] Selects the first book element that is the child of the
bookstore element
/bookstore/book[last()] Selects the last book element that is the child of the
bookstore element
/bookstore/book[last()-1] Selects the last but one book element that is the child
of the bookstore element
/bookstore/book[position()<3] Selects the first two book elements that are children
of the bookstore element
//title[@lang] Selects all the title elements that have an attribute
named lang
//title[@lang='en'] Selects all the title elements that have a "lang"
attribute with a value of "en"
/bookstore/book[price>35.00] Selects all the book elements of the bookstore
element that have a price element with a value
greater than 35.00
/bookstore/book[price>35.00]/title Selects all the title elements of the book elements of
the bookstore element that have a price element with
a value greater than 35.00
XQuery
XQuery is to XML what SQL is to database tables.
XQuery is designed to query XML data - not just XML
files, but anything that can appear as XML, including
databases.
XQuery Example
for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title
What is Xquery
XQuery is the language for querying XML data
XQuery is built on XPath expressions
XQuery is supported by all major databases
XQuery is a W3C Recommendation
XQuery is a language for finding and extracting elements
and attributes from XML documents.
Here is an example of a question that XQuery could solve:
"Select all CD records with a price less than $10 from the
CD collection stored in the XML document called
cd_catalog.xml“
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
XQuery FLWOR Expressions
How to Select Nodes From "books.xml" With FLWOR
Look at the following path expression:
doc("books.xml")/bookstore/book[price>30]/title
The expression above will select all the title elements
under the book elements that are under the bookstore
element that have a price element with a value that is
higher than 30.
The following FLWOR expression will select exactly the
same as the path expression above:
for $x in doc("books.xml")/bookstore/book
where $x/price>30
return $x/title
The result will be:
<title lang="en">XQuery Kick Start</title>
<title lang="en">Learning XML</title>
With FLWOR you can sort the result:
for $x in doc("books.xml")/bookstore/book
where $x/price>30
order by $x/title
return $x/title
FLWOR is an acronym for "For, Let, Where, Order by, Return".
The for clause selects all book elements under the bookstore element
into a variable called $x.
The where clause selects only book elements with a price element
with a value greater than 30.
The order by clause defines the sort-order. Will be sort by the title
element.
The return clause specifies what should be returned. Here it returns
the title elements.
The result of the XQuery expression above will be:
<title lang="en">Learning XML</title>
<title lang="en">XQuery Kick Start</title>
JSON: Overview
JSON or JavaScript Object Notation is a lightweight text-
based open standard designed for human-readable data
interchange.
Conventions used by JSON are known to programmers
which include C, C++, Java, Python, Perl etc.
JSON stands for JavaScript Object Notation.
This format was specified by Douglas Crockford.
This was designed for human-readable data interchange
It has been extended from the JavaScript scripting
language.
The filename extension is .json
Uses of JSON
Characteristics of JSON
Easy to read and write JSON.
Lightweight text based interchange format
Language independent.
Simple Example in JSON
Example shows Books information stored using JSON considering
language of books and their editions:
{ "book": [
{ "id":"01",
"language": "Java",
"edition": "third",
"author": "Herbert Schildt" },
{ "id":"07",
"language": "C++",
"edition": "second"
"author": "E.Balagurusamy"
}
]
}
After understanding the above program we will try another example,
let's save the below code as json.htm:
<html>
<head>
<title>JSON example</title>
<script language="javascript" >
var object1 = { "language" : "Java", "author" : "herbert schildt" };
document.write("<h1>JSON with JavaScript example</h1>");
document.write("<br>");
document.write("<h3>Language = " + object1.language+"</h3>");
document.write("<h3>Author = " + object1.author+"</h3>");
var object2 = { "language" : "C++", "author" : "E-Balagurusamy" };
document.write("<br>");
document.write("<h3>Language = " + object2.language+"</h3>");
document.write("<h3>Author = " + object2.author+"</h3>");
document.write("<hr />");
document.write(object2.language + " programming language can be studied "
+ "from book written by " + object2.author); document.write("<hr />");
</script>
</head><body></body>
</html>
Now let's try to open json.htm using IE or any other javascript enabled
browser, this produces the following result
JSON - Syntax
Let's have a quick look on JSON basic syntax. JSON syntax is
basically considered as subset of JavaScript syntax, it includes the
following:
Data is represented in name/value pairs
Curly braces hold objects and each name is followed by ':'(colon), the
name/value pairs are separated by , (comma).
Square brackets hold arrays and values are separated by ,(comma).
Below is a simple example:
{ "book": [
{ "id":"01",
"language": "Java",
"edition": "third",
"author": "Herbert Schildt“
},
{ "id":"07",
"language": "C++",
"edition": "second"
"author": "E.Balagurusamy"
}
]
}
JSON supports following two data structures:
Collection of name/value pairs: This Data Structure is supported by
different programming language.
Ordered list of values: It includes array, list, vector or sequence etc.
JSON - DataTypes
There are following datatypes supported by JSON format:
Type Description
double- precision floating-point
Number
format in JavaScript
double-quoted Unicode with
String
backslash escaping
Boolean true or false
Array an ordered sequence of values
it can be a string, a number, true
Value
or false, null etc
an unordered collection of
Object
key:value pairs
can be used between any pair of
Whitespace
tokens
null empty
Number
It is a double precision floating-point format in JavaScript and it
depends on implementation.
Octal and hexadecimal formats are not used.
The following table shows number types:
Type Description
Integer Digits 1-9, 0 and positive or negative
Fraction Fractions like .3, .9
Exponent Exponent like e, e+, e-,E, E+, E-
Syntax:
var json-object-name = { string : number_value, .......}
Example:
Example showing Number Datatype, value should not be quoted:
var obj = {marks: 97}
String
It is a sequence of zero or more double quoted Unicode characters
with backslash escaping.
Character is a single character string i.e. a string with length 1.
The table shows string types:
Type Description
" double quotation
\ reverse solidus
/ solidus
b backspace
f form feed
n new line
r carriage return
t horizontal tab
u four hexadecimal digits
Syntax:
var json-object-name = { string : "string value", .......}
Example:
Example showing String Datatype:
var obj = {name: 'Amit'}
Boolean
Array
Example:
Syntax:
{ string : value, .......}
Example:
Example showing Object:
Example:
var i =1;
var j = "sachin";
var k = null;
JSON - Objects
Creating Simple Objects
JSON Schema:
Describes your existing data format.
Clear, human- and machine-readable documentation.
Complete structural validation, useful for automated
testing.
Complete structural validation, validating client-submitted
data.
JSON Schema Example
Following is a basic JSON schema which covers a classical product
catalog description:
{ "$schema": "http://json-schema.org/draft-04/schema#",
"title": "Product",
"description": "A product from Acme's catalog",
"type": "object",
"properties": {
"id": { "description": "The unique identifier for a product",
"type": "integer"
},
"name": {
"description": "Name of the product",
"type": "string"
},
"price": {
"type": "number",
"minimum": 0,
"exclusiveMinimum": true
}
}, "required": ["id", "name", "price"]
}
Keywords Description
The $schema keyword states that this schema is written according to the draft v4
$schema
specification.
title You will use this to give a title to your schema
description A little description of the schema
The type keyword defines the first constraint on our JSON data: it has to be a JSON
type
Object.
Defines various keys and their value types, minimum and maximum values to be used in
properties
JSON file.
required This keeps a list of required properties.
minimum This is the constraint to be put on the value and represents minimum acceptable value.
If "exclusiveMinimum" is present and has boolean value true, the instance is valid if it is
exclusiveMinimum
strictly greater than the value of "minimum".
maximum This is the constraint to be put on the value and represents maximum acceptable value.
If "exclusiveMaximum" is present and has boolean value true, the instance is valid if it is
exclusiveMaximum
strictly lower than the value of "maximum".
A numeric instance is valid against "multipleOf" if the result of the division of the instance
multipleOf
by this keyword's value is an integer.
maxLength The length of a string instance is defined as the maximum number of its characters.
minLength The length of a string instance is defined as the minimum number of its characters.
A string instance is considered valid if the regular expression matches the instance
pattern
successfully.
Above schema can be used to test the validity of the below
given JSON code:
[
{ "id": 2,
"name": "An ice sculpture",
"price": 12.50,
},
{ "id": 3,
"name": "A blue mouse",
"price": 25.50,
}
]
JSON - Comparison with XML
JSON and XML are human readable formats and are language
independent.
They both have support for creation, reading and decoding in real
world situations.
We can compare JSON with XML based on the following factors:
Verbose
XML is more verbose (Expressed in more words than needed) than
JSON, so it's faster to write JSON for humans.
Arrays Usage
XML is used to describe structured data which doesn't include arrays
whereas JSON include arrays.
Parsing
JavaScript's eval method parses JSON. When applied to JSON, eval
returns the described object.
Example
This shows individual examples of XML and JSON:
JSON
XML
<car>
<company>Volkswagen</company>
<name>Vento</name>
<price>800000</price>
</car>