Professional Documents
Culture Documents
Write a well formed xml document containing the information about the Employee in a file
employee.xml
Employee
Ename
Empid
Designation
Email
Address
Employee
Solution:
<?xml version="1.0" encoding="UTF-8"?>
<employees>
<employee>
<ename>Abhinav</ename>
<empid>1</empid>
<salary>12000</salary>
<designation>DBA</designation>
<email>reddevil@gmail.com</email>
<address>Devendra Nagar</address>
</employee>
<employee>
<ename>Abhishek</ename>
<empid>2</empid>
<salary>20000</salary>
<designation>DBA</designation>
<email>akakmisra@gmail.com</email>
<address>Pandri</address>
</employee>
<employee>
<ename>Amit</ename>
<empid>3</empid>
<salary>10000</salary>
<designation>Network Admin</designation>
<email>jain2111@yahoo.co.in</email>
<address>Dhamtari</address>
</employee>
<employee>
<ename>Irshad</ename>
<empid>4</empid>
<salary>10000</salary>
<designation>DBA</designation>
<email>monto911@gmail.com</email>
<address>Devendra Nagar</address>
</employee>
<employee>
<ename>Nikunj</ename>
<empid>21</empid>
<salary>30000</salary>
<designation>Developer</designation>
<email>nikunjrathod77@gmail.com</email>
<address>Shri Nagar</address>
</employee>
<employee>
<ename>Ritesh</ename>
<empid>12</empid>
<salary>12000</salary>
<designation>Network Admin</designation>
<email>riteshu@gmail.com</email>
<address>Ashram Road</address>
</employee>
<employee>
<ename>Mandeep</ename>
<empid>19</empid>
<salary>35000</salary>
<designation>MD</designation>
<email>mandeep263@gmail.com</email>
<address>Shr Ram Nagar</address>
</employee>
<employee>
<ename>Bhavdeep</ename>
<empid>7</empid>
<salary>12000</salary>
<designation>Network Administrator</designation>
<email>bhavdeepdua@gmail.com</email>
<address>Shyam Nagar</address>
</employee>
<employee>
<ename>Shivani</ename>
<empid>14</empid>
<salary>12000</salary>
<designation>Networking</designation>
<email>mandavi@gmail.com</email>
<address>Khamtrai</address>
</employee>
<employee>
<ename>Sandeep</ename>
<empid>22</empid>
<salary>12000</salary>
<designation>Hacker</designation>
<email>sandeepkhatik7@gmail.com</email>
<address>Shyam Nagar</address>
</employee>
</employees>
Output :-
Q2. Create a DTD file as Employee.dtd and check for the validity or the employee.xml file.
Solution:
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT employees (employee*)>
<!ELEMENT employee (ename,empid,salary,designation,email,address)>
<!ELEMENT ename (#PCDATA)>
<!ELEMENT empid (#PCDATA)>
<!ELEMENT salary (#PCDATA)>
<!ELEMENT designation (#PCDATA)>
<!ELEMENT email (#PCDATA)>
<!ELEMENT address (#PCDATA)>
OutPut _-
Q3. Create a schema file as Employee.xsd and perform validations as mentioned:
Operations :
1.Ename must contain only alphabets and must not exceed 20 characters.
2. Empid must start with the initials emp.
3. salary must not contain alphabets should not be negative and should be on 5 digit figure only.
4.Email must contain @ and only.com suffix.
Solution:
The xsd file for the validation against above xml file
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="first">
<xs:restriction base="xs:string">
<xs:minLength value="4"/>
<xs:pattern value="([e][m][p])([0-9])*"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="second">
<xs:restriction base="xs:string">
<xs:maxLength value="20"/>
<xs:pattern value="([a-z])*"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="third">
<xs:restriction base="xs:integer">
<xs:totalDigits value="5"/>
<xs:pattern value="([0-9])*"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="four">
<xs:restriction base="xs:string">
<xs:pattern value="([a-z])*([@])([a-z])*([.][c][o][m])"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="Employee">
<xs:complexType>
<xs:sequence>
<xs:element name="empid" type="first"/>
<xs:element name="name" type="second"/>
<xs:element name="salary" type="third"/>
<xs:element name="email" type="four"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Output :-
Q4. Create a file as food.xml having the contents as mentioned below :-
<?xml version="1.0"?>
<food_list>
<food_item type="vegetable">
<name>Agar</name>
<carbs_per_serving>81</carbs_per_serving>
<fiber_per_serving>8</fiber_per_serving>
<fat_per_serving>0.5</fat_per_serving>
<kj_per_serving>1280</kj_per_serving>
</food_item>
<food_item type="vegetable">
<name>Asparagus</name>
<carbs_per_serving>1</carbs_per_serving>
<fiber_per_serving>1</fiber_per_serving>
<fat_per_serving>0</fat_per_serving>
<kj_per_serving>40</kj_per_serving>
</food_item>
<food_item type="vegetable">
<name>Cabbage</name>
<carbs_per_serving>0</carbs_per_serving>
<fiber_per_serving>1</fiber_per_serving>
<fat_per_serving>0</fat_per_serving>
<kj_per_serving>14</kj_per_serving>
</food_item>
<food_item type="vegetable">
<name>Potato</name>
<carbs_per_serving>21.5</carbs_per_serving>
<fiber_per_serving>2</fiber_per_serving>
<fat_per_serving>1</fat_per_serving>
<kj_per_serving>460</kj_per_serving>
</food_item>
<food_item type="vegetable">
<name>Pumpkin</name>
<carbs_per_serving>6</carbs_per_serving>
<fiber_per_serving>1</fiber_per_serving>
<fat_per_serving>0.5</fat_per_serving>
<kj_per_serving>150</kj_per_serving>
</food_item>
<food_item type="vegetable">
<name>Yam</name>
<carbs_per_serving>30.5</carbs_per_serving>
<fiber_per_serving>2</fiber_per_serving>
<fat_per_serving>0.5</fat_per_serving>
<kj_per_serving>550</kj_per_serving>
</food_item>
<food_item type="vegetable">
<name>Zucchini</name>
<carbs_per_serving>1.5</carbs_per_serving>
<fiber_per_serving>1.5</fiber_per_serving>
<fat_per_serving>0.5</fat_per_serving>
<kj_per_serving>55</kj_per_serving>
</food_item>
<food_item type="seafood">
<name>Abalone</name>
<carbs_per_serving>0</carbs_per_serving>
<fiber_per_serving>0</fiber_per_serving>
<fat_per_serving>1</fat_per_serving>
<kj_per_serving>400</kj_per_serving>
</food_item>
<food_item type="seafood">
<name>Barramundi</name>
<carbs_per_serving>0</carbs_per_serving>
<fiber_per_serving>0</fiber_per_serving>
<fat_per_serving>2</fat_per_serving>
<kj_per_serving>390</kj_per_serving>
</food_item>
<food_item type="fruit">
<name>Apple</name>
<carbs_per_serving>15</carbs_per_serving>
<fiber_per_serving>2.5</fiber_per_serving>
<fat_per_serving>0</fat_per_serving>
<kj_per_serving>250</kj_per_serving>
</food_item>
<food_item type="fruit">
<name>Kiwi Fruit</name>
<carbs_per_serving>7.5</carbs_per_serving>
<fiber_per_serving>2.5</fiber_per_serving>
<fat_per_serving>0</fat_per_serving>
<kj_per_serving>150</kj_per_serving>
</food_item>
<food_item type="grain">
<name>Oatbran</name>
<carbs_per_serving>62</carbs_per_serving>
<fiber_per_serving>14</fiber_per_serving>
<fat_per_serving>7</fat_per_serving>
<kj_per_serving>1400</kj_per_serving>
</food_item>
<food_item type="grain">
<name>Wheatgerm</name>
<carbs_per_serving>1.5</carbs_per_serving>
<fiber_per_serving>1</fiber_per_serving>
<fat_per_serving>0.5</fat_per_serving>
<kj_per_serving>70</kj_per_serving>
</food_item>
</food_list>
Operations:
1. create a file as food.xslt and retrieve the information according to the food item type mentioned.
2. retrieve the kj_per_serving from food type seafood in ascending order in tabular format.
3. Retrieve the name from food type grain in descending order or as specified in tabular format.
4. Retrieve only the name, carbs_per_serving and kj_per_serving for all food types as grain.
Solution:
4.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="food_list">
<table border="2" bgcolor="navyblue" align="center">
<tr>
<th>Name</th>
<th>Kj Per Seving</th>
<th>carbs Per Seving</th>
</tr>
<xsl:for-each select="food_item">
<xsl:if test="@type='grain'">
<tr>
<td>
<xsl:value-of select="name"/>
</td>
<td align="center">
<xsl:value-of select="kj_per_serving"/>
</td>
<td align="center">
<xsl:value-of select="carbs_per_serving"/>
</td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
Output :-
Q5. Create an xml file books.sml as mentioned below:-
Books
Book bk_id
Name
Subject
Publication
Price {200,300,500,400,700,1100,2300,450,1500}
Books
Operations :
1. Make entry for at least 10 books.
2. Retrieve the name price and subject information either in ascending or descending order of id.
3. Retrieve the name and price of only those books whose price lies between 500 to 1000.
4. Retrieve the complete information about the book based on the maximum and minimum price
5. Perform all the validation for the data wherever requied
Solution:
1.
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2011 sp1 (http://www.altova.com) by end user (free.org) -->
<?xml:stylesheet type="text/xsl" href="Fifth.xslt"?>
<Books>
<Book type="Java">
<Book_Id>101</Book_Id>
<Name>Jaishwal</Name>
<Subject>Java</Subject>
<Publication>2000</Publication>
<Price>600</Price>
</Book>
<Book type="Java">
<Book_Id>102</Book_Id>
<Name>Goyal</Name>
<Subject>XML</Subject>
<Publication>2002</Publication>
<Publication>2002</Publication>
<Price>1100</Price>
</Book>
<Book type="Java">
<Book_Id>103</Book_Id>
<Name>P.K. Gupta</Name>
<Subject>O.R.</Subject>
<Publication>1990</Publication>
<Price>5500</Price>
</Book>
<Book type="Java">
<Book_Id>104</Book_Id>
<Name>Peterson</Name>
<Subject>A.I.</Subject>
<Publication>1995</Publication>
<Price>300</Price>
</Book>
<Book type="Java">
<Book_Id>105</Book_Id>
<Name>Rozer Precmen</Name>
<Subject>S.E.</Subject>
<Publication>1999</Publication>
<Price>950</Price>
</Book>
<Book type="Java">
<Book_Id>106</Book_Id>
<Name>Jaiveher Khan</Name>
<Subject>D.W.H.</Subject>
<Publication>1997</Publication>
<Price>750</Price>
</Book>
<Book type="Java">
<Book_Id>107</Book_Id>
<Name>Bala Guruswami</Name>
<Subject>C++</Subject>
<Publication>2001</Publication>
<Price>400</Price>
</Book>
<Book type="Java">
<Book_Id>108</Book_Id>
<Name>Pilipsh</Name>
<Subject>P.O.M.</Subject>
<Publication>2003</Publication>
<Price>360</Price>
</Book>
<Book type="Java">
<Book_Id>109</Book_Id>
<Name>Mukhrjee</Name>
<Subject>P.M.</Subject>
<Publication>2006</Publication>
<Price>800</Price>
</Book>
<Book type="Java">
<Book_Id>110</Book_Id>
<Name>Gupta</Name>
<Subject>HTML</Subject>
<Publication>2007</Publication>
<Price>990</Price>
</Book>
</Books>
<tr>
<td>
<xsl:value-of select="Book_Id"/>
</td>
<td>
<xsl:value-of select="Name"/>
</td>
<td>
<xsl:value-of select="Subject"/>
</td>
<td>
<xsl:value-of select="Publication"/>
</td>
<td>
<xsl:value-of select="Price"/>
</td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
<h2><center>Maximum Price</center></h2>
<table border="2" bgcolor="navyblue" align="center">
<tr>
<td>Book_Id</td>
<th>Name</th>
<th>Subject</th>
<th>Publication</th>
<th>Price</th>
</tr>
<xsl:for-each select="Books/Book">
<xsl:sort select="Price" data-type="number" order="descending"/>
<xsl:if test="position()=1">
<tr>
<td>
<xsl:value-of select="Book_Id"/>
</td>
<td>
<xsl:value-of select="Name"/>
</td>
<td>
<xsl:value-of select="Subject"/>
</td>
<td>
<xsl:value-of select="Publication"/>
</td>
<td>
<xsl:value-of select="Price"/>
</td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
5.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Books">
<xs:complexType>
<xs:sequence>
<xs:element name="Book_Id" type="xs:integer"/>
<xs:element name="Book_Name" type="xs:string"/>
<xs:element name="Subject" type="xs:string"/>
<xs:element name="Publication" type="xs:string"/>
<xs:element name="Price" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Output :-
Q6. Create a XSLT file for the XML structure mentioned with following specification:
Products
Productname
Price
Company
Description
Quantity
Products
XSLT Operation :-
Productname
{
Font-family:Arial
Font-size:20 points
Font-weight:bold
Color:red
Display:block
Padding-top:6pt
Padding-bottom:6pt
}
Price, Company, Description, Quantity
{
Font-family: Times Roman
Font-size: 10 pints
Font-weight:bold
Color: blue
Display:block
Padding-top:3pt
Padding-bottom:3pt
}
Solution:
Output:-
Q7. The Owner at a Shop needs to display the product wise order report. The product and order
data is stored in an XML document. This data includes the product detail such as productID, name,
and price per unit for Each Product the details about all the orders placed against that product are
also stored in the document. The order detail includes the orderNo, shipping Address, total
quantity ordered and the order value. The total sales value for each product also needs to be
displayed.
<Summary>
<product Id=”” Name=”” price=””>
<Order Id=””>
<ship_add></ship_add>
<quantity></quantity>
</Order>
</Summary>
Operations:
As per the problem statement the data to be displayed:-
Solution:
<order id="101">
<ship_add>Bihar</ship_add>
<quantity>33</quantity>
</order>
<order id="102">
<ship_add>UP</ship_add>
<quantity>12</quantity>
</order>
<order id="103">
<ship_add>gujrat</ship_add>
<quantity>77</quantity>
</order>
<order id="106">
<ship_add>maharastra</ship_add>
<quantity>88</quantity>
</order>
<order id="109">
<ship_add>hariyana</ship_add>
<quantity>76</quantity>
</order>
<order id="111">
<ship_add>Delhi</ship_add>
<quantity>92</quantity>
</order>
<order id="114">
<ship_add>Mumbai</ship_add>
<quantity>65</quantity>
</order>
<order id="115">
<ship_add>goa</ship_add>
<quantity>85</quantity>
</order>
<order id="117">
<ship_add>bangal</ship_add>
<quantity>100</quantity>
</order>
<order id="130">
<ship_add>Chennai</ship_add>
<quantity>55</quantity>
</order>
</summary>
<xsl:template match="/">
<html><head><title>Products Sample</title>
</head>
<body>
<xsl:apply-templates/>
<xsl:apply-templates select="order">
<xsl:sort select="id" order="ascending"/>
</xsl:apply-templates>
</body></html>
</xsl:template>
<xsl:template match="summary">
<xsl:text disable-output-escaping="yes">
<li>
</xsl:text>
<table border="2">
<caption align="bottom">Product Table</caption>
<tbody>
<tr bgcolor="green">
<th>Product Id</th>
<th>Product Name</th>
<th>Product Price</th>
</tr>
<xsl:for-each select="product">
<tr bgcolor="blue">
<font color="green">
<td > <b><xsl:value-of select="@id"/></b></td>
<td> <b><xsl:value-of select="@name"/></b></td>
<td><b><xsl:value-of select="@price"/></b></td>
</font>
</tr>
</xsl:for-each>
</tbody>
</table>
<table border="1">
<caption align="bottom">Order Details</caption>
<tbody>
<tr>
<th>Order Id</th>
<th>Shipping Address</th>
<th>Quantity</th>
</tr>
<xsl:for-each select="order">
<tr bgcolor="#ECF1EF">
<th><b><xsl:value-of select="@id"/></b><br/></th>
<th><b><xsl:value-of select="ship_add"/></b><br/></th>
<th><b><xsl:value-of select="quantity"/></b><br/></th>
</tr>
</xsl:for-each>
</tbody>
</table><br/>
<pre><b>The total shiping value is<xsl:value-of select="sum(//@price)"></xsl:value-of></b></pre>
</xsl:template>
</xsl:stylesheet>
Output:-
Q8. Have a look at the following script:-
<Employeedata>
<Employee>
<EmpName></EmpName>
<Address>
<Residence></Residence>
</Address>
<Salary></Salary>
</Employee>
</Employeedata>
Operation:
1. Create an html file using the DOM model that navigats through all the records of Employee.xml
and prints the data for an individual employee using ActiveXObject.
2. Insert atleast 5 Records.
3. Provide validation for the data wherever required.
Solution:
<html>
<head>
<script langugage="JavaScript">
function one()
xml_doc.async = false;
var i,j;
xml_doc.load("Eight.xml");
var comment1=xml_doc.documentElement.childNodes.item(1).childNodes.item(2).text;
var num1=comment1.length;
for(i=0;i<=num1-1;i++)
for(j=0;j<=2;j++)
comment = xml_doc.documentElement.childNodes.item(i).childNodes.item(j).text;
document.write(comment+'\n');
</script>
</head>
<body onload="one()">
</body>
</html>
Output :-
Q9. Have a look at the following script:
<StudentInfo>
<Student>
<Name></Name>
<City><City>
</Student>
</StudentInfo>
Operation :
1. Create an xml file containing the appropriate data for the elements defined above.
2. Write an html file that uses XML property of DomDocument Object and adds a new element
node called DOB and Email to first Student element, it also appends a new node to Student fully.
3. Run the .html file to see the updated XML script in out put..
Solution:
Output :-
Q10. Have a look at the following script:
Operations:
1. Write an XPath script that iterate through each title element and prints them in separate line.
Solution:
<html>
<head>
<script type="text/javascript" src="loadxmldoc.js"></script>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml");
for(i=0;i<=4;i++)
{
x=xmlDoc.getElementsByTagName("title")[i]
y=x.childNodes[0];
document.write("\n"+y.nodeValue);
document.write(" \n ");
}
</script>
</body>
</html>
Output :-