Compare XML with HTML. Highlight their similarities and differences with examples
XML (eXtensible Markup Language) and HTML (HyperText Markup Language) are both markup languages that define rules for encoding documents in a format that is both human-readable and machine-readable. Although they share some similarities, they serve distinct purposes and have different characteristics. Below are their similarities and differences, along with examples.
### Similarities
1. **Markup Language**: Both XML and HTML use markup to annotate text and define the structure of the document.
2. **Tags**: Both use opening and closing tags to enclose elements. For example:
```html
<title>My Document</title> (HTML)
```
```xml
<name>John Doe</name> (XML)
```
3. **Hierarchical Structure**: Both have a tree-like structure wherein elements can be nested inside one another.
4. **Text-Based Format**: Both are text-based, meaning they can be edited in any text editor.
### Differences
| Feature | XML | HTML |
|------------------------|---------------------------------------|----------------------------------------|
| **Purpose** | Designed to store and transport data. | Designed for displaying data in web browsers. |
| **Data Structure** | Strictly defines structure; must be well-formed. | More lenient with structure; browsers can render invalid HTML. |
| **Tags** | Custom, user-defined tags. | Predefined tags with specific meanings (e.g., `<html>`, `<head>`, `<body>`). |
| **Attributes** | Attributes are always treated as strings and must be quoted. | Attributes can sometimes be omitted for boolean attributes (e.g., `<input type="checkbox" checked>`). |
| **Closing Tags** | All elements must have a closing tag or be self-closing. | Some tags are self-closing (e.g., `<br>`, `<img>`), while others don't require a closing tag. |
| **Data Type** | Can store data of any kind including complex and hierarchical structures. | Primarily used for text and visual content in a web page. |
| **Validation** | Can be validated against a schema (like DTD or XSD). | Can be validated but often ignored in practice for rendering purposes. |
| **Namespaces** | Supports namespaces, allowing disambiguation of similar tags. | Does not support namespaces. |
### Examples
#### XML Example
```xml
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="1">
<title>XML Developer's Guide</title>
<author>John Doe</author>
<price>29.99</price>
</book>
<book id="2">
<title>Learning XML</title>
<author>Jane Doe</author>
<price>39.99</price>
</book>
</books>
```
#### HTML Example
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Web Page</title>
</head>
<body>
<h1>Welcome to My Web Page</h1>
<p>This is a simple paragraph.</p>
<img src="image.jpg" alt="Example Image" />
</body>
</html>
```
### Summary
In summary, XML is aimed at structuring and transporting data with a focus on data interchange, while HTML is focused on the presentation of that data in web browsers. While they share a common syntax in using tags, their purposes and rules may vary significantly.