At this point, we've created a well-formed XML document that follows the rules of XML syntax. However, what if you want to make sure that others who work with the XML document you've created are adding elements in the right order, or including all the elements the document needs?
Let's consider the contents of job_postings.xml. As you already know, it has a specific structure. Each <midwest-job-listing> element contains <job> elements, which then contain other elements such as <title>
and <salary>
. It wouldn't make much sense to change the nesting of the elements. For example, the <salary>
element would make no sense if it was not contained within a <job>
element. It might also be confusing to people using the human resources website if each job had the information listed in a different order, such as having <availability>
located towards the end of one job, yet at the beginning of another.
As long as you're the only author working on the document, you can make sure that you maintain the proper structure for the document. However, what happens if others are working on the document? What happens if others want to re-purpose the documents for their own needs, and want to be sure it has a consistent structure? In order to accomplish this, we will need to create rules to help ensure our XML markup is being used correctly, and that our XML document is valid. These rules will live in a document called a schema.
Schemas
Schemas are a type of XML document that contain rules for how a specific XML markup language can be used. There are many different languages available for writing schemas, but the three most popular are the Document Type Definition (DTD), XML Schema Definition (XSD), and RELAX NG schema languages. Each one offers different features, and the choice of a specific schema language to work with depends on what features an XML author needs for their document.
Schemas can be used to:
- provide users of an XML document with a list of elements and attributes that can be used in said document
- indicate where elements and attributes can be used in a document, and the specific order elements need to appear in
- provide information about a document that is both human- and machine-readable
The process of checking to see if an XML document follows the rules laid out in a schema is called validating. This is an important step in the process of making an XML document, as it ensures that the elements and attributes in a document are being used as the original author of the document intended.
There are a number of different ways we can validate a document. XML editors will detect if an XML document refers to a specific schema, and use that schema to indicate any errors in a document. If we're using a code editor to work on our XML, however, we might want to use one of the many online validation options to check an XML document against a schema. For our purposes today, we'll be using the site XML Validation once again to validate our document. We won't be creating a schema of our own today — instead, we'll make use of a premade schema based on the contents of job_postings.xml. This premade schema, jobs.xsd, was created using the XML Schema language.
Before checking to make sure the XML written in job_postings.xml is valid, you'll need to associate the file with its schema. This way, XML processors will know what kind of schema is being used, and where to find the schema. After that, you can check the validity of job_postings.xml using the XML Validation site.