Unit 3
Unit 3
XML, JSON
By
Dr. Chilukala Mahender Reddy
Content:
• XML: Syntax of XML,
• document structure,
• document type definition,
• namespaces,
• XML schemas,
• document object model,
• presenting XML using CSS, XSLT, XPath, XQuery, FLOWR.
• JSON: Features, JSON vs. XML,
• JSON Data Types,
• JSON Objects,
• JSON Arrays,
• JSON HTML.
What is XML?:
• Attributes cannot contain multiple values but child elements can have multiple
values.
• Attributes cannot contain tree structure but child elements can.
• Attributes are not easily expandable. If you want to change an attribute’s values in
the future, it may be complicated.
• Attributes cannot describe structure but child elements can.
• Attributes are more difficult to be manipulated by program code.
• Attributes values are not easy to test against a DTD, which is used to define the
legal elements of an XML document.
XML Comments:
• XML comments are just like HTML comments. We know that the comments are
used to make codes more understandable to other developers.
• XML Comments add notes or lines for understanding the purpose of an XML
code. Although XML is known as self-describing data sometimes XML comments
are necessary.
• Syntax
• An XML comment should be written as:
• <!-- Write your comment-->
• Rules for adding XML comments
• Don't use a comment before an XML declaration.
• You can use a comment anywhere in XML document except within the
attribute value.
• Don't nest a comment inside the other comment.
XML Tree Structure:
<?xml version="1.0"?>
<college>
<student>
<firstname>Mahender</firstname>
<lastname>Reddy</lastname>
<contact>09990449935</contact>
<email>[email protected]</email>
<address>
<city>Ghaziabad</city>
<state>Uttar Pradesh</state>
<pin>201007</pin>
</address>
</student>
</college>
Rules for well formed XML:
• It must begin with the XML declaration.
• It must have one unique root element.
• All start tags of XML documents must match end tags.
• XML tags are case-sensitive.
• All elements must be closed.
• All elements must be properly nested.
• All attribute values must be quoted.
• XML entities must be used for special characters.
Document Type Definitions(DTD):
What is DTD
• DTD stands for Document Type Definition. It defines the legal building blocks of
an XML document. It is used to define document structure with a list of legal
elements and attributes.
Purpose of DTD
• Its main purpose is to define the structure of an XML document. It contains a list
of legal elements and defines the structure with the help of them.
Checking Validation
• Before proceeding with XML DTD, you must check the validation. An XML
document is called "well-formed" if it contains the correct syntax.
• A well-formed and valid XML document has been validated against DTD.
Valid and well-formed XML document with DTD
employee.xml
<?xml version="1.0"?>
<!DOCTYPE employee SYSTEM "employee.dtd">
<employee>
<firstname>vimal</firstname>
<lastname>jaiswal</lastname>
<email>[email protected]</email>
</employee>
• In the above example, the DOCTYPE declaration refers to an external DTD file.
The content of the file is shown in below paragraph.
employee.dtd
<!ELEMENT employee (firstname,lastname,email)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT lastname (#PCDATA)>
<!ELEMENT email (#PCDATA)>
Description of DTD:
• <!DOCTYPE employee: It defines that the root element of the document is an employee.
• <!ELEMENT employee: It defines that the employee element contains 3 elements "firstname,
lastname and email".
• <!ELEMENT firstname: It defines that the firstname element is #PCDATA typed. (parse-able data
type).
• <!ELEMENT lastname: It defines that the lastname element is #PCDATA typed. (parse-able data
type).
• <!ELEMENT email: It defines that the email element is #PCDATA typed. (parse-able data type).
Note: PCDATA
• PCDATA means parsed character data.
• Think of character data as the text found between the start tag and the end tag of an XML element.
• PCDATA is text that WILL be parsed by a parser. The text will be examined by the parser for
entities and markup.
• Tags inside the text will be treated as markup and entities will be expanded.
• However, parsed character data should not contain any &, <, or > characters; these need to be
represented by the & < and > entities, respectively.
Declaring Entities
• A doctype declaration can also define special strings that can be used in the XML
file.
• An entity has three parts:
1. An ampersand (&)
2. An entity name
3. A semicolon (;)
• Syntax to declare entity:
• <!ENTITY entity-name "entity-value">
• Let's see a code to define the ENTITY in doctype declaration.
• author.xml
<?xml version="1.0" standalone="yes" ?>
<!DOCTYPE author [
<!ELEMENT author (#PCDATA)>
<!ENTITY sj "Sonoo Jaiswal"> ]>
<author>&sj;</author>
Internal and External DTDs
• A document type declaration can either contain declarations directly or refer to
another file
• Internal
• <!DOCTYPE root-element [
declarations
]>
• External file
• <!DOCTYPE root-name SYSTEM “file-name”>
• A public identifier can also be specified, that would be mapped to a system
identifier by the processing system
Namespaces
• XML Namespace is used to avoid element name conflict in XML document.
• An XML namespace is declared using the reserved XML attribute. This attribute
name must be started with "xmlns".
• Let's see the XML namespace syntax:
• <element xmlns:name = "URL">
• Here, the namespace starts with the keyword "xmlns". The word name is a
namespace prefix. The URL is a namespace identifier.
<?xml version="1.0" encoding="UTF-8"?>
<cont:contact xmlns:cont="http://sssit.org/contact-us">
<cont:name>Vimal Jaiswal</cont:name>
<cont:company>SSSIT.org</cont:company>
<cont:phone>(0120) 425-6464</cont:phone>
</cont:contact>
• Namespace Prefix: cont
• Namespace Identifier: http://sssit.org/contact-us
• It specifies that the element name and attribute names with cont prefix belong to
http://sssit.org/contact-us namespace.
• In XML, element names are defined by the developer so there is a chance to
conflict in the name of the elements. To avoid these types of conflict we use XML
Namespaces. We can say that XML Namespaces provides a method to avoid
element name conflict.
• Generally these conflict occurs when we try to mix XML documents from
different XML applications.
<table>
<tr>
<td>Aries</td>
<td>Bingo</td>
</tr>
</table>
<table>
<name>Computer table</name>
<width>80</width>
<length>120</length>
</table>
If you add both XML fragments together, there would be a name conflict because
both have <table> elements. Although they have different names and meanings.
You can easily avoid the XML namespace by using a name prefix.
<h:table>
<h:tr>
<h:td>Aries</h:td>
<h:td>Bingo</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>Computer table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
• You can use xmlns attribute to define a namespace with the following syntax:
<root>
<h:table xmlns:h="http://www.abc.com/TR/html4/">
<h:tr>
<h:td>Aries</h:td>
<h:td>Bingo</h:td>
</h:tr>
</h:table>
<f:table xmlns:f="http://www.xyz.com/furniture">
<f:name>Computer table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
In the above example, the <table> element defines a namespace and when a namespace is defined for an element, the
child elements with the same prefixes are associated with the same namespace.
<root xmlns:h="http://www.abc.com/TR/html4/"
xmlns:f="http://www.xyz.com/furniture">
<h:table>
<h:tr>
<h:td>Aries</h:td>
<h:td>Bingo</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>Computer table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
</root>
XML Schemas
What is XML schema?
XML schema is a language that is used for expressing constraints about XML
documents. There are so many schema languages that are used nowadays for
example Relax- NG and XSD (XML schema definition).
An XML schema is used to define the structure of an XML document. It is like DTD
but provides more control over XML structure.
Checking Validation
An XML document is called "well-formed" if it contains the correct syntax. A well-
formed and valid XML document has been validated against Schema.
employee.xsd example.xml
<?xml version="1.0"?> <?xml version="1.0"?>
<xs:schema <employee
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.javatpoint.com"
targetNamespace="http://www.javatpoint.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-
xmlns="http://www.javatpoint.com" instance"
elementFormDefault="qualified"> xsi:schemaLocation=" http://www.javatpoint.com
<xs:element name="employee"> employee.xsd">
<xs:complexType>
<xs:sequence> <firstname>vimal</firstname>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Description of XML Schema:
• FLWOR is an acronym that stands for "For, Let, Where, Order by, Return".
• For - It is used to select a sequence of nodes.
• Let - It is used to bind a sequence to a variable.
• Where - It is used to filter the nodes.
• Order by - It is used to sort the nodes.
• Return - It is used to specify what to return (gets evaluated once for every node).
<?xml version="1.0" encoding="UTF-8"?> <course category="C">
<courses> <title lang="en">Learn C in 2 Months.</title>
<course category="JAVA"> <trainer>Ramesh Kumar</trainer>
<title lang="en">Learn Java in 3 Months.</title> <year>2014</year>
<trainer>Sonoo Jaiswal</trainer> <fees>3000.00</fees>
<year>2008</year> </course>
<fees>10000.00</fees> <course category="XML">
</course> <title lang="en">Learn XML in 2 Months.</title>
<course category="Dot Net"> <trainer>Ajeet Kumar</trainer>
<title lang="en">Learn Dot Net in 3 Months.</title> <year>2015</year>
<fees>4000.00</fees>
<trainer>Vicky Kaushal</trainer>
</course>
<year>2008</year> </courses>
<fees>10000.00</fees>
</course>
let $courses := (doc("courses.xml")/courses/course)
return <results>
{
for $x in $courses
where $x/fees>2000
order by $x/fees
return $x/title
}
</results>
JSON:
What is JSON?
• JSON stands for JavaScript Object Notation
• JSON is a lightweight data interchange format
• JSON is a plain text written in JavaScript object notation
• JSON is used to send data between computers
• JSON is language independent *
Why Use JSON?
• The JSON format is syntactically similar to the code for creating JavaScript objects.
Because of this, a JavaScript program can easily convert JSON data into JavaScript objects.
• Since the format is text only, JSON data can easily be sent between computers, and used by
any programming language.
• JavaScript has a built-in function for converting JSON strings into JavaScript objects:
JSON.parse()
• JavaScript also has a built-in function for converting an object into a JSON string:
JSON.stringify()
JSON Example
• This example is a JSON string:
'{"name":"John", "age":30, "car":null}'
• It defines an object with 3 properties:
• name
• age
• car
• Each property has a value.
• If you parse the JSON string with a JavaScript program, you can access the data as
an object:
let personName = obj.name;
let personAge = obj.age;
JSON vs XML
JSON is Like XML Because
• Both JSON and XML are "self-describing" (human readable)
• Both JSON and XML are hierarchical (values within values)
• Both JSON and XML can be parsed and used by lots of programming languages
• Both JSON and XML can be fetched with an XMLHttpRequest
JSON is Unlike XML Because
• JSON doesn't use end tag
• JSON is shorter
• JSON is quicker to read and write
• JSON can use arrays
• The biggest difference is:
• XML has to be parsed with an XML parser. JSON can be parsed by a standard JavaScript
function.
{"employees":[
{ "firstName":"John", "lastName":"Doe" },
{ "firstName":"Anna", "lastName":"Smith" },
{ "firstName":"Peter", "lastName":"Jones" }
]}
<employees>
<employee>
<firstName>John</firstName> <lastName>Doe</lastName>
</employee>
<employee>
<firstName>Anna</firstName> <lastName>Smith</lastName>
</employee>
<employee>
<firstName>Peter</firstName> <lastName>Jones</lastName>
</employee>
</employees>
JSON Data Types:
In JSON, values must be one of the following data types:
• a string
• a number
• an object (JSON object)
• an array
• a boolean
• null
Note: JSON values cannot be one of the following data types:
• a function, a date, undefined
JSON Strings
• Strings in JSON must be written in double quotes.
• Example: {"name":"John"}
JSON Numbers
• Numbers in JSON must be an integer or a floating point.
• Example: {"age":30}
JSON Objects
• Values in JSON can be objects.
• Example: {
"employee":{"name":"John", "age":30, "city":"New York"}
}
JSON Arrays
• Values in JSON can be arrays.
• Example: {
"employees":["John", "Anna", "Peter"]
}
JSON Booleans
• Values in JSON can be true/false.
• Example: {"sale":true}
JSON null
• Values in JSON can be null.
• Example: {"middlename":null}
JSON Object Literals:
• This is a JSON string:
'{"name":"John", "age":30, "car":null}'
• Inside the JSON string there is a JSON object literal:
{"name":"John", "age":30, "car":null}
• JSON object literals are surrounded by curly braces {}.
• JSON object literals contains key/value pairs.
• Keys and values are separated by a colon.
• Keys must be strings, and values must be a valid JSON data type:
• string
• number
• object
• array
• boolean
• null
• Each key/value pair is separated by a comma.
JavaScript Objects
Ref:
• https://www.geeksforgeeks.org/javascript-json-html/
• https://www.w3schools.com/js/js_json_html.asp