UNIT –I
INTRODUCTION
XML is Extensible mark up Language that defines a set of rules for encoding documents. Although the design focuses on documents the language is widely used for representation of random structures.
Features
Designed to store and transport data
Designed to be self-descriptive.
You define your own tags
It is platform independent and language independent.
Consider the following example:
<!DOCTYPE html> <html> <h1>Note</h1> <body> <p>To:SIMON <br> From:PETER </p> <h1>Reminder</h1> <p>Meeting at 8am</p> </body> </html>
|
XML <pre> <note> <To> SIMON</to> <From>PETER</from> <heading>Reminder</heading> <body>Meeting at 8am</body> </note> </pre> |
Output
Note To : SIMON From : PETER
Reminder Meeting at 8 am |
Note :Here the output for both html and xml is same the difference is that in html we have p,h1 tags but in XML we have self-defined tags like From, To and so on.
Advantages
Makes web development user friendly
Simplifies data sharing.
Simplifies data storing
Increases data availability
- DTD defines structure, legal elements and attributes of XML document.
- With the help of DTD group of people can agree on standard DTD in term of interchanging data.
- Used to verify that XML data is valid.
Internal DTD Declaration
It is wrapped inside the <!DOCTYPE> definition:
XML with Internal DTD
<?xml version="1.0"?> | <note>
|
In the XML file select “view source” to view the DTD.
DTD is interpreted in the following manner:
- !DOCTYPE note defines that the root element of this document is note
- !ELEMENT note defines that the note element must contain four elements: "to,from,heading,body"
- !ELEMENT to defines the to element to be of type "#PCDATA"
- !ELEMENT from defines the from element to be of type "#PCDATA"
- !ELEMENT heading defines the heading element to be of type "#PCDATA"
- !ELEMENT body defines the body element to be of type "#PCDATA"
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
</note>
The first line is the XML declaration.
The next line describes the root element of the document (like saying: "this document is a note"):
<note>
The next 4 lines describe 4 child elements of the root (to, from, heading, and body).
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
And finally the last line defines the end of the root element.
</note>
External DTD Declaration
If DTD is declared in external file <!DOCTYPE> definition must contain reference to the DTD file .
XML document with reference to external DTD
<?xml version="1.0"?> |
|
Here the file is note.dtd, which contains:
<! ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
CSS is used to add style and display information to an XML document. It can format the whole XML document.
To link XML files with CSS use the following syntax:
<?xml-stylesheet type="text/css" href="cssemployee.css"?>
See the CSS file
Employee
{
Background-color: pink;
}
Firstname, lastname, email
{
Font-size:12pt;
Display: block;
Color: blue;
Margin-left: 50px;
}
Create the DTD file :
<!ELEMENT employee (firstname,lastname,email)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT lastname (#PCDATA)>
<!ELEMENT email (#PCDATA)>
Employee.xml file
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="cssemployee.css"?>
<!DOCTYPE employee SYSTEM "employee.dtd">
<employee>
<firstname>vimal</firstname>
<lastname>jaiswal</lastname>
<email>vimal@javatpoint.com</email>
</employee>
XML Namespace is used to avoid element name conflict in XML document.
Namespace Declaration
XML namespace is declared by using the reserved XML attribute. This attribute name must be started with "xmlns".
Syntax:
<element xmlns:name = "URL">
namespace starts with keyword "xmlns".
name is a namespace prefix.
URL is namespace identifier.
Example:
<?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>
Here the Namespace Prefix: cont
Namespace Identifier: http://sssit.org/contact-us
Specifies that element name and attribute names with cont prefix belongs to http://sssit.org/contact-us name space.
Elements name are defined by the developer, hence there is a chance for conflict in name of the elements.
To avoid conflicts we use XML Namespaces. They provide a method to avoid element name conflict.
Example
Let us consider two tables:
Table 1:
<table>
<tr>
<td>Aries</td>
<td>Bingo</td>
</tr>
</table
Table 2:
<table>
<name>Computer table</name>
<width>80</width>
<length>120</length>
</table>
Here conflict arises because both have table , though they have different name and meaning.
There are three ways of getting rid of conflict :
Using 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>
Xmlns Attribute
<element xmlns:name = "URL">
Example:
<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>
URI
Uniform Resource Identifier is used to identify the internet resource. It is a string of characters. The most common URI is URL (Uniform Resource Locator) which identifies an internet domain address.
Describes the structure of an XML document.
Files can carry a description of its own format.
An XML document validated against an XML Schema is valid.
XML Schema is an XML-based alternative to DTD:
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
The Schema above is interpreted like this:
- <xs:element name="note"> defines the element called "note"
- <xs:complexType> the "note" element is a complex type
- <xs:sequence> the complex type is a sequence of elements
- <xs:element name="to" type="xs:string"> the element "to" is of type string (text)
- <xs:element name="from" type="xs:string"> the element "from" is of type string
- <xs:element name="heading" type="xs:string"> the element "heading" is of type string
- <xs:element name="body" type="xs:string"> the element "body" is of type string
XSD is a kind of quality control (QC) language.
With XSD, information in an XML file is described well .
In some way to be validated.
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://www.w3schools.com"
xmlns="https://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Here element is a complex type because it contains other elements.
To, from, heading, body are simple types
This XML document has a reference to a DTD:
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM
"https://www.w3schools.com/xml/note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
This XML document has a reference to XML Schema
<?xml version="1.0"?>
<note
xmlns="https://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.w3schools.com/xml note.xsd">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
- Powerful language for applying styles to XML documents.
- XSL has two parts — a formatting language and a transformation language.
The formatting language allows you to apply styles but browser support is limited
XSLT allows you to transform your XML document into another form.
For example, you could use XSLT to dynamically output some (or all) of the contents of your XML file into an HTML document containing other content.
Declare XSL
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
Or:
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
- To get access to the XSLT elements, attributes and features we must declare XSLT namespace at the top of the document.
- The xmlns:xsl="http://www.w3.org/1999/XSL/Transform" points to the official W3C XSLT namespace. If you use this namespace, you must also include the attribute version="1.0".
To transform XML into HTML
- XML document
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
.
.
</catalog>
Open the XML file (click on the link below) - The XML document will be displayed with color-coded root and child elements (except in Safari).
There is a plus (+) or minus sign (-) to the left of the elements that can be clicked to expand or collapse the element structure.
To view the raw XML source, right-click in XML file and select "View Source"!
XSL Style sheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Link the XSL Style Sheet to the XML Document
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
.
.
</catalog>
Here XSL stylesheet is added to XML document.