You are on page 1of 7

Web Technologies Lab

Week:- Date:-

Objective:-

Write an XML file which will display the Book information which includes the following:
1) Title of the book
2) Author number
3) ISBN number
4) Publisher name
5) Edition
6) Price
Write a Document Type Definition (DTD) to validate the above xml file.Display the XML
file as follows.The contents should be displayed in a table. The header of the table should
be in color GREY. And the Author names column should be displayed in one color and
should be capitalized and in bold. Use your own colors for remaining columns. Use XML
schemas XSL and CSS for the above purpose.

DTD FILE: books.dtd


< ! ELEMENT data (inner)>
< ! ELEMENT inner (title, author, isbn, pname, edition, price)>
<! ELEMENT title (#PCDATA)>
<! ELEMENT author (#PCDATA)>
<! ELEMENT isbn (#PCDATA)>
<! ELEMENT pname (#PCDATA)>
<! ELEMENT edition (#PCDATA)>
<! ELEMENT price (#PCDATA)>
XML FILE: books.xml
<?xml version="1.0" ?>
<?xml:stylesheet type="text/xsl" href="1.xsl" ?>
<data>
<inner>
<title>Java server pages</title>
<author>HANS BERGSTEN</author>
Web Technologies Lab

<isbn>5466</isbn>
<pname>pearson</pname>
<edition>2nd</edition>
<price>455</price>
</inner>
<inner>
<title>Computer Networks</title>
<author>ANDREW S TANENBUM</author>
<isbn>7899</isbn>
<pname>Person Education</pname>
<edition>4 th</edition>
<price>500</price>
</inner>
<inner>
<title>Java Programing with CORBA</title>
<author>G.BROSE</author>
<isbn>1230</isbn>
<pname>Wiley Dreamtech</pname>
<edition>3 rd </edition>
<price>400</price>
</inner>
<inner>
<title>Data Mining-Concepts and designs</title>
<author>HAN AND KAMBER</author>
<isbn>9720</isbn>
<pname>Pearson</pname>
<edition>4 th</edition>
Web Technologies Lab

<price>600</price>
</inner>
</data>
DOMValidateDTD.java:
import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.validation.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
public class DOMValidateDTD
{
public static void main(String args[])
{
try{
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
factory.setValidating(true);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setErrorHandler(new org.xml.sax.ErrorHandler()
{
//Ignore the fatal errors
public void fatalError(SAXParseException exception)throws
SAXException { }
//Validation errors
Web Technologies Lab

public void error(SAXParseException e)throws


SAXParseException
{
System.out.println("Error at " +e.getLineNumber() + "
line.");
System.out.println(e.getMessage());
System.exit(0);
}
//Show warnings
public void warning(SAXParseException err)throws
SAXParseException
{
System.out.println(err.getMessage());
System.exit(0);
}
} //line-19
);
Document xmlDocument = builder.parse(new
FileInputStream("books.xml"));
DOMSource source = new DOMSource(xmlDocument);
StreamResult result = new StreamResult(System.out);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
"books.dtd");
transformer.transform(source, result);
}
catch (Exception e) {
System.out.println(e.getMessage());
Web Technologies Lab

}
}
}
Output:-
C:\>CSE>javac DOMValidateDTD.java
C:\>CSE>java DOMValidateDTD
<?xml version="1.0" ?>
<?xml:stylesheet type="text/xsl" href="1.xsl" ?>
<data>
<inner>
<title>Java server pages</title>
<author>HANS BERGSTEN</author>
<isbn>5466</isbn>
<pname>pearson</pname>
<edition>2nd</edition>
<price>455</price>
</inner>
<inner>
<title>Computer Networks</title>
<author>ANDREW S TANENBUM</author>
<isbn>7899</isbn>
<pname>Person Education</pname>
<edition>4 th</edition>
<price>500</price>
</inner>
<inner>
<title>Java Programing with CORBA</title>
Web Technologies Lab

<author>G.BROSE</author>
<isbn>1230</isbn>
<pname>Wiley Dreamtech</pname>
<edition>3 rd </edition>
<price>400</price>
</inner>
<inner>
<title>Data Mining-Concepts and designs</title>
<author>HAN AND KAMBER</author>
<isbn>9720</isbn>
<pname>Pearson</pname>
<edition>4 th</edition>
<price>600</price>
</inner>
</data>
XSL FILE:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<head>
<title>naga</title>
</head>
<body>
<table border="1" width="100%">
<tr >
<th style="color:grey"> Title of the book</th>
<th style="color:grey">Author Name</th>
<th style="color:grey">ISBN Number</th>
<th style="color:grey">Publisher name</th>
<th style="color:grey">Edition</th>
<th style="color:grey">Price</th>
</tr>
<xsl:for-each select="data/inner">
<tr >
Web Technologies Lab

<td style="color:purple;text-align:center"><xsl:value-of select="title" /></td>


<td style="color:brown;text-align:center"><b><xsl:value-of select="author" /></b></td>
<td style="color:red ;text-align:center"><xsl:value-of select="isbn" /></td>
<td style="color:pink;text-align:center"><xsl:value-of select="pname"/></td>
<td style="color:orange;text-align:center"><xsl:value-of select="edition"/></td>
<td style="color:magenta;text-align:center"><xsl:value-of select="price"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Output:-

You might also like