0% found this document useful (0 votes)
6 views8 pages

Exp 5 and 6 WT

Must read

Uploaded by

dushyantgupta214
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

Exp 5 and 6 WT

Must read

Uploaded by

dushyantgupta214
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

PROGRAM 5

Objective: Writing program in XML for creation of DTD, which specifies set of rules. Create
a style sheet in CSS/ XSL & display the document in internet explorer .

Theory:
XML Document Type Declaration, commonly known as DTD, is a way to describe precisely the
XML language. DTDs check the validity of structure and vocabulary of an XML document against
the grammatical rules of the appropriate XML language.
An XML document can be defined as:
• Well-formed: If the XML document adheres to all the general XML rules such as tags
must be properly nested, opening and closing tags must be balanced, and empty tags
must end with '/>', then it is called as well-formed.
OR
• Valid: An XML document said to be valid when it is not only well-formed, but it also
conforms to available DTD that specifies which tags it uses, what attributes those tags
can contain, and which tags can occur inside other tags, among other properties.
Types:
DTD can be classified on its declaration basis in the XML document, such as:
• Internal DTD
External DTD

When a DTD is declared within the file it is called Internal DTD and if it is declared in a
separate file it is called External DTD.
We will learn more about these in the chapter DTD Syntax

Syntax
Basic syntax of a DTD is as follows:
<!DOCTYPE element DTD identifier
[
declaration1
declaration2
........
]>

In the above syntax


• DTD starts with <!DOCTYPE> delimiter.
• An element tells the parser to parse the document from the specified root element.
• DTD identifier is an identifier for the document type definition, which may be the
path to a file on the system or URL to a file on the internet. If the DTD is pointing to
external path, it is called external subset.
• The square brackets [ ] enclose an optional list of entity declarations called internal
subset.
Internal DTD
A DTD is referred to as an internal DTD if elements are declared within the XML files. To reference
it as internal DTD, standalone attribute in XML declaration must be set to yes. This means the
declaration works independent of external source.

Syntax
The syntax of internal DTD is as shown:

<!DOCTYPE root-element [element-declarations]>

where root-element is the name of root element and element-declarations is where you declare the
elements.

Example
Following is a simple example of internal DTD:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>


<!DOCTYPE address [
<!ELEMENT address (name,company,phone )>
<!ELEMENT name (#PCDATA )>
<!ELEMENT company (#PCDATA )>
<!ELEMENT phone (#PCDATA ) >
]>
<address >
<name>Tanmay Patil</name >
<company>TutorialsPoint</company >
<phone>(011) 123 -4567</phone >
</address >

External DTD
In external DTD elements are declared outside the XML file. They are accessed by specifying the
system attributes which may be either the legal .dtd file or a valid URL. To reference it as external
DTD, standalone attribute in the XML declaration must be set as no. This means, declaration
includes information from the external source.
Syntax
Following is the syntax for external DTD:

<!DOCTYPE root-element SYSTEM "file-name">

where file-namen is the file with .dtd extension.

Example
The following example shows external DTD usage:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>


<!DOCTYPE address SYSTEM "address.dtd">
<address >
<name>Tanmay Patil</name >
<company>TutorialsPoint</company >
<phone>(011) 123 -4567</phone >
</address>

The content of the DTD file address.dtd are as shown:


<!ELEMENT address (name,company,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT company (#PCDATA)>
<!ELEMENT phone (#PCDATA)>

XSLT
EXtensible Stylesheet Language Transformation commonly known as XSLT is a way to transform the XML document into other formats such as
XHTML.

<xsl:template> defines a way to reuse templates in order to generate the desired output
for nodes of a particular type/context.various template are used with the template like
name, match,mode ,priority.
<xsl:value-of> tag puts the value of the selected node as per XPath expression, as text.

Declaration:
Following is the syntax declaration of <xsl:value-of> element.
<xsl:value-of
select = Expression
disable-output-escaping = "yes" | "no" >
</xsl:value-of>

<xsl:for-each> tag applies a template repeatedly for each node.

Declaration
Following is the syntax declaration of <xsl:for-each> element
<xsl:for-each
select = Expression >
</xsl:for-each>

<xsl:sort> tag specifies a sort criteria on the nodes.

Declaration
Following is the syntax declaration of <xsl:sort> element.
<xsl:sort
select = string-expression
lang = { nmtoken }
data-type = { "text" | "number" | QName }
order = { "ascending" | "descending" } case-
order = { "upper-first" | "lower-first" } >
</xsl:sort>

<xsl:if> tag specifies a conditional test against the content of nodes.

Declaration
Following is the syntax declaration of <xsl:if> element.
<xsl:if
test = boolean-expression >
</xsl:if>

<xsl:choose> tag specifies a multiple conditional tests against the content of nodes in
conjunction with the <xsl:otherwise> and <xsl:when> elements.

Declaration
Following is the syntax declaration of <xsl:choose> element.
<xsl:choose >
</xsl:choose>

Output:

In Internet Explorer
Title Artist
Empire Burlesque Bob Dylan
Hide your heart Bonnie Tyler
2.XML

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE note SYSTEM "Note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
DTD:
<!DOCTYPE note
[
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)><!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
OUTPUT:
PROGRAM 6

Objective: Program to illustrate JDBC connectivity. Program for maintaining database by


sending queries. Design and implement a simple servlet book query with the help of JDBC
& SQL. Create MS Access Database, create on ODBC link, Compile & execute JAVA JDVC
Socket.

Theory:

Creating JDBC Application

There are following six steps involved in building a JDBC application −

• Import the packages: Requires that you include the packages containing the JDBC
classes needed for database programming. Most often, using import java.sql.* will suffice.
• Register the JDBC driver: Requires that you initialize a driver so you can open a
communication channel with the database.
• Open a connection: Requires using the DriverManager.getConnection() method to create
a Connection object, which represents a physical connection with the database.
• Execute a query: Requires using an object of type Statement for building and submitting
an SQL statement to the database.
• Extract data from result set: Requires that you use the appropriate ResultSet.getXXX()
method to retrieve the data from the result set.
• Clean up the environment: Requires explicitly closing all database resources versus
relying on the JVM's garbage collection.

STEPS:

//STEP 1. Import required packages


import java.sql.*;

public class FirstExample {


// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/EMP";

// Database credentials static final


String USER = "username";
static final String PASS = "password";

public static void main(String[] args) {


Connection conn = null;
Statement stmt = null;
try{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");

//STEP 3: Open a connection


System.out.println("Connecting to database..."); conn =
DriverManager.getConnection(DB_URL,USER,PASS);
//STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement(); String sql;
sql = "SELECT id, first, last, age FROM Employees";
ResultSet rs = stmt.executeQuery(sql);

//STEP 5: Extract data from result set


while(rs.next()){ //Retrieve by
column name int id =
rs.getInt("id"); int age =
rs.getInt("age"); String first =
rs.getString("first");
String last = rs.getString("last");

//Display values
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
//STEP 6: Clean-up environment
rs.close();
stmt.close();
conn.close();
}catch(SQLException
se){ //Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{ if(stmt!=null)
stmt.close();
}catch(SQLException
se2){ }// nothing we can do
try{ if(conn!=null)
conn.close();
}catch(SQLException
se){ se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}//end main
}//end FirstExample

You might also like