[go: up one dir, main page]

0% found this document useful (0 votes)
21 views5 pages

SDL 3

The document contains a PHP script that validates an XML file against an XSD schema upon form submission. It checks for the existence of the XML and XSD files, loads the XML, and validates it, providing feedback on the validation status. Additionally, it includes HTML for the user interface and CSS for styling the page.

Uploaded by

Soham Mahajan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views5 pages

SDL 3

The document contains a PHP script that validates an XML file against an XSD schema upon form submission. It checks for the existence of the XML and XSD files, loads the XML, and validates it, providing feedback on the validation status. Additionally, it includes HTML for the user interface and CSS for styling the page.

Uploaded by

Soham Mahajan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment 3

Index.php:
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {

$xmlFile = "sample.xml";
$xsdFile = "schema.xsd";

// Check if the XML and XSD files exist before loading them
if (file_exists($xmlFile)) {

echo "<p style='color: green;'>XML file found.</p>";


} else {
echo "<p style='color: red;'>XML file not found.</p>";

if (file_exists($xsdFile)) {
echo "<p style='color: green;'>XSD file found.</p>";
} else {
echo "<p style='color: red;'>XSD file not found.</p>";
}

if (file_exists($xmlFile) && file_exists($xsdFile)) {

$xml = new DOMDocument();


if (@$xml->load($xmlFile)) { // Use @ to suppress errors during load

if ($xml->schemaValidate($xsdFile)) {
echo "<p style='color: green;'>The XML file is valid against the schema.</p>";
} else {
echo "<p style='color: red;'>The XML file is not valid. Check the schema or the
XML file.</p>";
}
} else {
echo "<p style='color: red;'>Error loading XML file.</p>";
}
}
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>XML Schema Validation</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">

<h1>XML Schema Validation</h1>


<form method="post">
<button type="submit">Validate XML</button>
</form>
</div>
</body>
</html>

Style.css:
body {
font-family: Arial, sans-serif;
margin: 20px;
line-height: 1.6;
}
h1 {
color: #333;
}
p{
font-size: 16px;
}
button {
padding: 10px 15px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}

Sample.xml:
<?xml version="1.0" encoding="UTF-8"?>
<inventory>

<product status="active">
<id>101</id>
<name>Laptop</name>
<price>75000.50</price>
<quantity>10</quantity>
<category>Electronics</category>

</product>
<product status="inactive">

<id>102</id>
<name>Smartphone</name>
<price>25000.00</price>
<quantity>5</quantity>
<category>Electronics</category>

</product>
</inventory>

Schema.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="inventory">

<xs:complexType>
<xs:sequence>

<xs:element name="product" maxOccurs="unbounded" minOccurs="1">


<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:int" />
<xs:element name="name" type="xs:string" />
<xs:element name="price" type="xs:decimal" />
<xs:element name="quantity" type="xs:int" />
<xs:element name="category" type="xs:string" />

</xs:sequence>
<xs:attribute name="status" type="xs:string" use="optional" />

</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Output:

You might also like