Skill Development Lab-1
SDL-1 Lab
Follow below assignments for write-up.
Assignment No.3
XML Document:
1. XML stands for eXtensible Markup Language.
2. XML is a markup language much like HTML.
3. XML was designed to store and transport data.
4. XML was designed to carry data - with focus on what data is.
5. HTML was designed to display data - with focus on how data looks.
6. XML tags are not predefined like HTML tags are.
7. Extension of XML file is .xml
Example: Save file name as 'book.xml'
<?xml version="1.0" encoding="UTF-8"?>
<Book ISBN="123456789">
<Title>Sample Book</Title>
<Author>John Doe</Author>
<PublicationYear>2022</PublicationYear>
</Book>
XML schema:
1. XML schema came into the picture because of some disadvantages in DTD(Document Type Definition)
2. DTD does not support complex type of lines of code.
3. DTD does not support more data types.
4. An XML Schema describes the structure of an XML document.
5. The XML Schema language is also called XML Schema Definition (XSD).
6. Extension of XML schema is .xsd
7. XML supports complex types as well as data types.
8. XML schema uses XML syntax.
9. XML Schemas Secure Data Communication.
Example: Save file name as 'book_schema.xsd'
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- Define the complex type for the Book element -->
<xs:element name="Book" type="BookType"/>
<!-- Define the complex type for the Book element's structure -->
<xs:complexType name="BookType">
<xs:sequence>
<xs:element name="Title" type="xs:string"/>
<xs:element name="Author" type="xs:string"/>
<xs:element name="PublicationYear" type="xs:gYear"/>
</xs:sequence>
<xs:attribute name="ISBN" type="xs:string" use="required"/>
</xs:complexType>
</xs:schema>
Steps in Python code:
i. To execute XML schema we required library called lxml library.
ii. To install lxml library, type command "pip install lxml" in command prompt(CMD).
iii. Open python editor and follow below steps to validate XML data.
1. Import the necessary library (lxml).
2. Load the XML Schema and create an XML schema validator.
3. Load the XML document and parse it.
4. Validate the XML document against the XML Schema.
5. Print the validation result or errors.
Example: Save file name as 'program_name.py'
from lxml import etree
# Load the XML Schema
schema_file = 'F:/PCET/SDL_LAB/book_schema.xsd'
schema = etree.XMLSchema(etree.parse(schema_file))
# Load the XML document
xml_file = 'F:/PCET/SDL_LAB/book.xml'
xml_doc = etree.parse(xml_file)
# Validate the XML document against the schema
if schema.validate(xml_doc):
print("Validation successful.")
else:
print("Validation failed. Errors:")
for error in schema.error_log:
print(error)
Copy the Python code snippet into a Python script or a Jupyter notebook.
Replace 'path/to/your/book_schema.xsd' with the actual path to your XSD file (book_schema.xsd).
Replace 'path/to/your/xml/book.xml' with the actual path to your XML file (book.xml).
Run the script or cell.
Check Validation Result (Python Only):
If the validation is successful, you will see the message "Validation successful."
If there are validation errors, it will print the errors.
******************* The End Thank You *******************
Assignment No.8
Install Ruby Environment Setup and Write a Ruby program which accept the user's first and last name and print them in reverse order with a space between them.
Install Ruby Environment
Linux: You can install Ruby using your package manager. For example, on Ubuntu, you can use:
sudo apt update
sudo apt install ruby-full
macOS: You can use Homebrew to install Ruby:
brew install ruby
Windows: You can download RubyInstaller from the official Ruby website and follow the installation instructions.
https://rubyinstaller.org/downloads/
With devkit click on first link shown after opening above ruby official site.
Or Directly install by clicking on given below link.
We can embed Ruby code within an HTML file using ERB (Embedded Ruby). ERB allows you to mix Ruby code with HTML markup. Here's how you can embed the Ruby program within an HTML file:
In this HTML file, the Ruby code is enclosed within <% %> tags. The output of the Ruby code is then embedded into the HTML using <%= %> tags. When you run this HTML file, it will prompt you to enter your first and last name, and then it will display your name in reverse order within the paragraph element.
To run the code on a browser or server, you'll need a web server that can interpret Ruby code, such as Ruby on Rails or Sinatra. Here's a simplified example using Sinatra, a lightweight web application framework for Ruby:
1. First, make sure you have Sinatra and rackup installed. You can install it via the terminal/command prompt using the following command:
gem install Sinatra
gem install rackup
2. Create a new file named app.rb (or any other name you prefer) and paste the following code into it:
require 'sinatra'
get '/' do
erb :index
end
post '/reverse' do
@first_name = params[:first_name]
@last_name = params[:last_name]
@reversed_name = "#{@last_name} #{@first_name}"
erb :reverse
end
3. Now create two view files in the same directory: views/index.erb and views/reverse.erb. In index.erb, put the HTML code with the form, and in reverse.erb, put the HTML code to display the reversed name.
views/index.erb:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Name Reversal</title>
</head>
<body>
<h2>Name Reversal</h2>
<form action="/reverse" method="post">
<label for="first_name">First Name:</label>
<input type="text" id="first_name" name="first_name" required>
<br>
<label for="last_name">Last Name:</label>
<input type="text" id="last_name" name="last_name" required>
<br>
<input type="submit" value="Reverse Name">
</form>
</body>
</html>
views/reverse.erb:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Reversed Name</title>
</head>
<body>
<h2>Reversed Name</h2>
<p>Your name in reverse order: <%= @reversed_name %></p>
</body>
</html>
4. Now, in your terminal or command prompt, navigate to the directory where you saved app.rb and run the following command:
ruby app.rb
5. Sinatra will start a web server on your local machine. You should see some output indicating that the server is running.
6. Open your web browser and go to http://localhost:4567. You should see the form where you can enter your first and last name. Upon submitting the form, it will display your name in reverse order on the same page.
Congratulations! You've set up a simple web application using Sinatra to run the Ruby code on a server.
******************* The End Thank You *******************
Assignment No.9
Write a Ruby Script to send an Email to a specific User.
Steps for sending email to specific user
Just click on the user email id
2. go to manage your Google account
3. just click on security set 2 step verification
4. after that search for the app password in the search bar.
5. for creating app password
Just click on Select app
2. Select use other location from drop-down box
3. give the name as per your requirement.
4 .generate password
5. copy the generated Password.
6. open the text editor and paste the code and paste the app password as we copied earlier also give same sender and receiver email id.
7. after all of these steps just go to replit online editor
1. first we need to login into Editor.
2 to create replit and Select ruby and give the title Name as you wish and Click on create replit.
3. paste the same code in the replit.
4. install the package like "gem install mail" in replit shell itself.
5. run the program through a command like "ruby main.rb" in replit shell itself.
8. Now you can check your mail.
Below is the code and the file name is: "main.rb"
require 'mail'
# Set up SMTP settings
Mail.defaults do
delivery_method :smtp, {
:address => 'smtp.gmail.com',
:port => 587,
:user_name => example@gmail.com
:password => '16characterpassword',
:authentication => :login,
:enable_starttls_auto => true
}
end
# Define email message
message = Mail.new do
from 'example@gmail.com'
to 'example@gmail.com'
subject 'Hello from Ruby!'
body 'This is a test email sent from Ruby.'
end
# Send email
message.deliver!
Congratulations! You've got the mail in your Gmail.
******************* The End Thank You *******************
Updating soon....