SDL 3
SDL 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)) {
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 ($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">
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:sequence>
<xs:attribute name="status" type="xs:string" use="optional" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Output: