XML Well-Formed and Valid XML:
Understanding Well-Formed XML Documents:
A well-formed XML document adheres to the syntax rules defined by the XML specification.
To create a proper growing structure of your XML file there are certain very important points to follow and they are all framed under the heading of XML document well-formedness.
Key Rules for Well-Formed XML:
- Single Root Element: The document must have exactly one root element.
- Proper Tagging: All elements must have a start tag and a corresponding end tag.
- Case Sensitivity: XML tags have a particular way in which case they should be written; <Title> and <title> are two different tags.
- Proper Nesting: Tags must be nested correctly without overlapping.
- Attribute Quotation: Attribute values must be enclosed in quotes, either single (') or double (").
Example of a Well-Formed XML Document:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<title>XML Fundamentals</title>
<author>John Smith</author>
<price>29.99</price>
</book>
</bookstore>
It contains one element, which is named <bookstore>, all of the tags are properly constructed and closed, and all attributes are surrounded by quotes.
Ensuring XML Documents Are Well-Formed
To ensure an XML document is well-formed, follow these steps:
Check for a Single Root Element:
<!-- Incorrect: Multiple root elements -->
<bookstore></bookstore>
<library></library>
<!-- Correct: Single root element -->
<catalog>
<bookstore></bookstore>
<library></library>
</catalog>
Verify Proper Tagging
<!-- Incorrect: Missing end tag -->
<title>XML Fundamentals
<!-- Correct: Properly closed tag -->
<title>XML Fundamentals</title>
Ensure Proper Nesting
<!-- Incorrect: Overlapping tags -->
<book>
<title>XML Basics
<author>John Doe</title>
</author>
<!-- Correct: Proper nesting -->
<book>
<title>XML Basics</title>
<author>John Doe</author>
</book>
Attribute Quotation
<!-- Incorrect: Unquoted attribute value -->
<book id=123>
<!-- Correct: Quoted attribute value -->
<book id="123">
Valid XML and Schema Validation:
Valid XML goes beyond being well-formed.It has to have a structure and includes specific fields and data types, only allowed by the schema. Schemas ensure that an XML document is built using a recommended format for designing its structure.
Types of Schemas:
- DTD (Document Type Definition): An older schema language used to define the structure and allowable content of an XML document.
- XML Schema (XSD): A more powerful and flexible schema language that supports data types, namespaces, and complex structures.
Example of an XML Schema (XSD):
<!-- books.xsd -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="bookstore">
<xs:complexType>
<xs:sequence>
<xs:element name="book" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="price" type="xs:decimal"/>
</xs:sequence>
<xs:attribute name="id" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Example of a Valid XML Document:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="books.xsd">
<book id="001">
<title>XML Fundamentals</title>
<author>John Smith</author>
<price>29.99</price>
</book>
</bookstore>
Steps for Schema Validation:
- Create a Schema: Define the allowed structure and data types in a DTD or XSD file.
- Reference the Schema: Xi: schemaLocation or DOCTYPE should be used to refer XML to the schema.
- Validate the Document: To validate the document, use an XML parser that supports schema validation to verify if the document complies with the schema.