0% found this document useful (0 votes)
19 views

XML and XSLT Program

program

Uploaded by

jananisb2207
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

XML and XSLT Program

program

Uploaded by

jananisb2207
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Create and save an XML document at the server, which contains 5 books information.

Write a
Program, which takes book title as an input and returns the book details by taking the book
information from the XML document.
Solution:
XML File: Book.xml
<?xml version="1.0" encoding="UTF-8"?>
<library>
<book>
<title>Book One</title>
<author>Author One</author>
<genre>Fiction</genre>
<price>200</price>
<isbn>111-2223334445</isbn>
</book>
<book>
<title>Book Two</title>
<author>Author Two</author>
<genre>Non-Fiction</genre>
<price>300</price>
<isbn>222-3334445556</isbn>
</book>
<book>
<title>Book Three</title>
<author>Author Three</author>
<genre>Mystery</genre>
<price>250</price>
<isbn>333-4445556667</isbn>
</book>
<book>
<title>Book Four</title>
<author>Author Four</author>
<genre>Science Fiction</genre>
<price>400</price>
<isbn>444-5556667778</isbn>
</book>
<book>
<title>Book Five</title>
<author>Author Five</author>
<genre>Biography</genre>
<price>350</price>
<isbn>555-6667778889</isbn>
</book>
</library>
Node.js Program
Server.js
const fs = require("fs");
const xml2js = require("xml2js");
const express = require("express");
const app = express();
const PORT = 3000;
// Middleware to parse JSON request bodies
app.use(express.json());
// Route to get book details by title
app.get("/book/:title", (req, res) => {
const title = req.params.title;
// Read the XML file
fs.readFile("books.xml", (err, data) => {
if (err) {
return res.status(500).send("Error reading the XML file.");
}
// Parse the XML file
xml2js.parseString(data, (parseErr, result) => {
if (parseErr) {
return res.status(500).send("Error parsing the XML file.");
}
// Search for the book by title
const books = result.library.book;
const book = books.find(b => b.title[0].toLowerCase() === title.toLowerCase());
if (book) {
res.json({
title: book.title[0],
author: book.author[0],
genre: book.genre[0],
price: book.price[0],
isbn: book.isbn[0],
});
} else {
res.status(404).send("Book not found.");
}
});
});
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
3. Run the Program
1. Install the required dependencies:
npm install xml2js express
2. Start the server:
node Server.js
3. Test the Program
http://localhost:3000/book/Book%20One

Creating the XML file that contains 10 student’s information and displaying the XML file
using XSLT.
Solution:
XML File: students.xml
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student>
<name>Arun Kumar</name>
<rollNumber>101</rollNumber>
<class>10</class>
<grade>A</grade>
<contact>1234567890</contact>
</student>
<student>
<name>Lakshmi Priya</name>
<rollNumber>102</rollNumber>
<class>10</class>
<grade>B</grade>
<contact>1234567891</contact>
</student>
<student>
<name>Sandhya Ramesh</name>
<rollNumber>103</rollNumber>
<class>11</class>
<grade>A</grade>
<contact>1234567892</contact>
</student>
<student>
<name>Kumaran Raj</name>
<rollNumber>104</rollNumber>
<class>12</class>
<grade>C</grade>
<contact>1234567893</contact>
</student>
<student>
<name>Meena Shankar</name>
<rollNumber>105</rollNumber>
<class>10</class>
<grade>A</grade>
<contact>1234567894</contact>
</student>
<student>
<name>Divya Subramani</name>
<rollNumber>106</rollNumber>
<class>11</class>
<grade>B</grade>
<contact>1234567895</contact>
</student>
<student>
<name>Ramya Narayan</name>
<rollNumber>107</rollNumber>
<class>12</class>
<grade>A</grade>
<contact>1234567896</contact>
</student>
<student>
<name>Suresh Ravi</name>
<rollNumber>108</rollNumber>
<class>10</class>
<grade>B</grade>
<contact>1234567897</contact>
</student>
<student>
<name>Priya Kannan</name>
<rollNumber>109</rollNumber>
<class>11</class>
<grade>C</grade>
<contact>1234567898</contact>
</student>
<student>
<name>Vikram Srinivas</name>
<rollNumber>110</rollNumber>
<class>12</class>
<grade>A</grade>
<contact>1234567899</contact>
</student>
</students>
XSLT File: students.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<head>
<title>Student Information</title>
<style>
table {
width: 80%;
border-collapse: collapse;
margin: 20px auto;
}
th, td {
border: 1px solid black;
padding: 10px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<h1 style="text-align: center;">Student Information</h1>
<table>
<tr>
<th>Name</th>
<th>Roll Number</th>
<th>Class</th>
<th>Grade</th>
<th>Contact</th>
</tr>
<xsl:for-each select="students/student">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="rollNumber"/></td>
<td><xsl:value-of select="class"/></td>
<td><xsl:value-of select="grade"/></td>
<td><xsl:value-of select="contact"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Run The Program:
Steps to Create and Run the Program:
1. Create the XML File
Open Notepad.
Copy the XML content and paste it into Notepad.
Save the file as students.xml:
In the "Save As" dialog, choose Save as type: All Files.
Name the file students.xml.
Ensure the encoding is set to UTF-8 before saving.
2. Create the XSLT File
Open a new Notepad window.
Copy the XSLT content and paste it into Notepad.
Save the file as students.xsl:
In the "Save As" dialog, choose Save as type: All Files.
Name the file students.xsl.
Ensure the encoding is set to UTF-8 before saving.
3. Link the XSLT File to the XML File
Ensure the XML file contains the following processing instruction at the top:
<?xml-stylesheet type="text/xsl" href="students.xsl"?>
4. Run the Program
Place both students.xml and students.xsl in the same folder.
Open the students.xml file in a browser that supports XSLT processing (like Google
Chrome, Firefox, or Microsoft Edge).
The browser will apply the XSLT to the XML file and display the formatted HTML table.
Develop an inventory management system which stores sales product information in the
form of XML document at the server. Write a Program, which takes product Id as an input
and returns the product details by taking the information from the XML document.
XML File: inventory.xml
<?xml version="1.0" encoding="UTF-8"?>
<inventory>
<product>
<id>P001</id>
<name>Smartphone</name>
<category>Electronics</category>
<price>15000</price>
<stock>50</stock>
</product>
<product>
<id>P002</id>
<name>Laptop</name>
<category>Electronics</category>
<price>45000</price>
<stock>30</stock>
</product>
<product>
<id>P003</id>
<name>Desk Chair</name>
<category>Furniture</category>
<price>3500</price>
<stock>100</stock>
</product>
<product>
<id>P004</id>
<name>Air Conditioner</name>
<category>Appliances</category>
<price>25000</price>
<stock>20</stock>
</product>
<product>
<id>P005</id>
<name>Washing Machine</name>
<category>Appliances</category>
<price>18000</price>
<stock>25</stock>
</product>
</inventory>
JavaScript Program: inventory.js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Inventory Management</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
label, input, button {
margin: 5px;
}
.result {
margin-top: 20px;
}
</style>
</head>
<body>
<h1>Inventory Management System</h1>
<label for="productId">Enter Product ID:</label>
<input type="text" id="productId" placeholder="e.g., P001">
<button onclick="getProductDetails()">Get Product Details</button>
<div class="result" id="result"></div>
<script>
function getProductDetails() {
const productId = document.getElementById("productId").value.trim();
const resultDiv = document.getElementById("result");

// Load the XML file


fetch("inventory.xml")
.then(response => {
if (!response.ok) {
throw new Error("Failed to load XML file");
}
return response.text();
})
.then(xmlText => {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlText, "text/xml");
const products = xmlDoc.getElementsByTagName("product");
let productFound = false;

for (let product of products) {


const id = product.getElementsByTagName("id")[0].textContent;
if (id === productId) {
const name = product.getElementsByTagName("name")[0].textContent;
const category = product.getElementsByTagName("category")[0].textContent;
const price = product.getElementsByTagName("price")[0].textContent;
const stock = product.getElementsByTagName("stock")[0].textContent;
resultDiv.innerHTML = `
<h2>Product Details</h2>
<p><strong>ID:</strong> ${id}</p>
<p><strong>Name:</strong> ${name}</p>
<p><strong>Category:</strong> ${category}</p>
<p><strong>Price:</strong> ₹${price}</p>
<p><strong>Stock Available:</strong> ${stock}</p>
`;
productFound = true;
break;
}
}
if (!productFound) {
resultDiv.innerHTML = `<p style="color: red;">Product with ID "${productId}"
not found.</p>`;
}
})
.catch(error => {
resultDiv.innerHTML = `<p style="color: red;">Error: ${error.message}</p>`;
});
}
</script>
</body>
</html>

You might also like