You are on page 1of 35

Q1.

Write a well formed xml document containing the information about the Employee in a file
employee.xml

Employee
Ename
Empid
Designation
Email
Address
Employee

Note :- make entry for at least 10 employees.

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 xml file for the above validation


<?xml version="1.0" encoding="UTF-8"?>
<Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:///C:/Documents%20and
%20Settings/SysAdmin/Desktop/Final/Second(2).xsd">
<empid>emp1</empid>
<name>nikunj</name>
<salary>50000</salary>
<email>nikunjrathod@yahoo.com</email>
</Employee>

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:

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


<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:template match="food_list">
<table border="2" bgcolor="skyblue" align="center">
<tr align="center">
<th>Food Type</th>
<th>Name</th>
<th>Carbs Per Serving</th>
<th>Fiber Per Serving</th>
<th>Fiber Per Serving</th>
<th>KG Per Serving</th>
</tr>
<xsl:for-each select="food_item">
<tr align="center">
<td>
<xsl:value-of select="@type"/>
</td>
<td>
<xsl:value-of select="name"/>
</td>
<td>
<xsl:value-of select="carbs_per_serving"/>
</td>
<td>
<xsl:value-of select="fiber_per_serving"/>
</td>
<td>
<xsl:value-of select="fat_per_serving"/>
</td>
<td>
<xsl:value-of select="kj_per_serving"/>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>

2. <?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">
<center><h2>Sorting in assending order When Food Type is Seafood</h2></center>
<table border="2" bgcolor="navyblue" align="center">
<tr>
<th>Name</th>
<th>Kj Per Seving</th>
</tr>
<xsl:for-each select="food_item">
<xsl:sort select="kj_per_serving" order="ascending"/>
<xsl:if test="@type='seafood'">
<tr>
<td>
<xsl:value-of select="name"/>
</td>
<td align="center">
<xsl:value-of select="kj_per_serving"/>
</td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
<center><h2>Sorting in desending order When Food Type is Grain</h2></center>

3. <table border="2" bgcolor="navyblue" align="center">


<tr>
<th>Name</th>
<th>Kj Per Seving</th>
</tr>
<xsl:for-each select="food_item">
<xsl:sort select="name" order="descending"/>
<xsl:if test="@type='grain'">
<tr>
<td>
<xsl:value-of select="name"/>
</td>
<td align="center">
<xsl:value-of select="kj_per_serving"/>
</td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>

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>

<?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="/">
<html>
<body>
<font face="Arial" size="-3">
<center>
2. <h1>Asecnding Order</h1>
</center>
<table border="2" bgcolor="navyblue" align="center">
<tr>
<th>Book_ID</th>
<th>Name</th>
<th>Subject</th>
<th>Price</th>
</tr>
<xsl:for-each select="Books/Book">
<xsl:sort select="@Book_Id" order="ascending"/>
<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="Price"/>
</td>
</tr>
</xsl:for-each>
</table>
</font>
<font face="Arial" size="-3">
<center>
<h1>Desending Order</h1>
</center>
<table border="2" bgcolor="navyblue" align="center">
<tr>
<th>Book_ID</th>
<th>Name</th>
<th>Subject</th>
<th>Price</th>
</tr>
<xsl:for-each select="Books/Book">
<xsl:sort select="@Book_Id" order="descending"/>
<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="Price"/>
</td>
</tr>
</xsl:for-each>
</table>
</font>
3. <h2>Book Price Lessthen 500 And Greterthen 1000</h2>
<table border="2" bgcolor="navyblue" align="center">
<tr>
<th>Name</th>
<th>Price</th>
</tr>
<xsl:for-each select="Books/Book">

<xsl:if test="Price&gt;500 and Price&lt;1000">


<tr>
<td>
<xsl:value-of select="Name"/>
</td>
<td>
<xsl:value-of select="Price"/>
</td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
<h2><center>Book Price Lessthen 500 And Greterthen 1000</center></h2>
<h2><center>Minimum 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>
4. <xsl:for-each select="Books/Book">
<xsl:sort select="Price" data-type="number" order="ascending"/>
<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>

<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:

The xml file for the above problem

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


<?xml:stylesheet type="text/xsl" href="product.xslt"?>
<Products>
<Productname>CD</Productname>
<Price>500</Price>
<Company>sony</Company>
<Description>write_a_data</Description>
<Quantity>50</Quantity>
</Products>

The xsl file for the above css

<?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="Products">
<div style="Font-family:Arial;Font-size:20 points;Font-
weight:bold;Color:red;Display:block;Padding-top:6pt;Padding-bottom:6pt">
<xsl:value-of select="Productname"/>
</div>
<div style="Font-family: Times Roman; Font-size: 10 pints;Font-weight:bold; Color: blue;
Display:block; Padding-top:3pt; Padding-bottom:3pt">
<br>Price : <xsl:value-of select="Price"/></br>
<br>Company : <xsl:value-of select="Company"/></br>
<br>Description : <xsl:value-of select="Description"/></br>
<br>Quantity : <xsl:value-of select="Quantity"/></br>
</div>
</xsl:template>
</xsl:stylesheet>

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:-

1. productId, Product Name, Price per Unit


2. Details about orders placed against the product are Order Number, Shipping Address, Total
Quantity, OrderValue, total sales for the product.
3. Use XPath to perform the operations.
4. Create XSLT to display the required data in a tabular format with different font color of the
column and data.
5. Insert atleast 10 Records.

Solution:

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


<?xml-stylesheet type="text/xsl" href="shop.xsl"?>
<summary>
<product id="1" name="monitor" price="4400"/>
<product id="2" name="mouse" price="400"/>
<product id="5" name="cabinet" price="1500"/>
<product id="8" name="hard disk" price="3400"/>
<product id="9" name="RAM" price="1200"/>
<product id="3" name="SMPS" price="600"/>
<product id="7" name="DVD-Drive" price="4400"/>
<product id="4" name="floppy drive" price="600"/>
<product id="6" name="USB " price="900"/>
<product id="10" name="external harddisk" price="3200"/>

<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>

The equivalent xsl file for the data retrieval

<?xml version="1.0" ?>


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<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">
&lt;li&gt;

</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:

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


<Employeedata>
<Employee type="It">
<EmpName>Nikunj Rathod</EmpName>
<Address>
<Residence>Shri Nagar</Residence>
</Address>
<Salary>30000</Salary>
</Employee>
<Employee type="mba">
<EmpName>Devendra Jangde</EmpName>
<Address>
<Residence>Ravan Bhata</Residence>
</Address>
<Salary>30000</Salary>
</Employee>
<Employee type="Networking">
<EmpName>Sandeep Khatik</EmpName>
<Address>
<Residence>Telibandha</Residence>
</Address>
<Salary>25000</Salary>
</Employee>
<Employee type="DBA">
<EmpName>Abhishek Kumar</EmpName>
<Address>
<Residence>Pandari</Residence>
</Address>
<Salary>30000</Salary>
</Employee>
<Employee type="Desiner">
<EmpName>Ritesh Shrivastav</EmpName>
<Address>
<Residence>Ashram Road</Residence>
</Address>
<Salary>30000</Salary>
</Employee>
</Employeedata>

The HTML file to fetch the records using ActiveX Object

<html>

<head>

<script langugage="JavaScript">

function one()

var xml_doc = new ActiveXObject("Msxml2.DOMDocument");

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;

var num = comment.length;

document.write(comment+'\n');

</script>

</head>
<body onload="one()">

</body>

</html>

The xsd file for the xml file


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="ENTER_NAME_OF_ROOT_ELEMENT_HERE">
<xs:complexType>
<xs:sequence>
<xs:element name="EmpName" type="xs:string"/>
<xs:element name="Residence" type="xs:string"/>
<xs:element name="Salary" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

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:

The xml file


<?xml version="1.0" encoding="UTF-8"?>
<StudentInfo>
<Student>
<Name>Nikunj</Name>
<City>Raipur</City>
</Student>
</StudentInfo>
The html file
<html>
<head>
<script language="javascript">
function one()
{
var obj=new ActiveXObject("Msxml2.DOMDocument");
obj.load("Nine.xml");
var root=obj.documentElement;
var elem=obj.createElement("DOB");
elem.text="10.02.1987";
root.childNodes.item(0).insertBefore(elem,root.childNodes.item(0).lastChild);
var elem=obj.createElement("Email");
elem.text="nikunjrockstar@yahoo.com";
root.childNodes.item(0).insertBefore(elem,root.childNodes.item(0).lastChild);
alert(obj.xml);
}
</script>
</head>
<body onload="one()">
</body>
</html>

Output :-
Q10. Have a look at the following script:

<?xml version=”1.0” encoding=”ISO-8859-1”?>


<bookstore>
<book category=”COOKING”>
<title lang=”en”>Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
</book>
<book category=”CHILDERN”>
<title lang=”en”>Harry Potter</title>
<author>J K Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category =”WEB”>
</book>
<book category=”WEB”>
<title lang=”en”>XQueary Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurl Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book> category=”WEB”>
<title lang=”en”>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>

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 :-

You might also like