Now that the XML Schema is complete, it can be associated with an XML file. We’ll want to associate the schema with an XML document in order to valiate the contents of the document. Before we talk about validating documents, let’s look at an example schema declaration. The following code associates an XML document with the XML Schema Language document for the Encoded Archival Description (EAD) XML standard:
<ead xmlns="http://ead3.archivists.org/schema/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation="http://ead3.archivists.org/schema/ ead3.xsd">
Exiting code block.
The ead
refers to the document element of the XML document being checked - with Encoded Archival Description, the root element will always be <ead>
. After that, we see the default namespace declaration, which tells us what the default namespace is for any elements that don’t have a declared namespace.
NOTE: The default namespace isn’t always declared as part of the root element, so you may not always see this information included in XML documents.
Next is the attribute for the XML Schema Instance namespace, which is needed in order to use the schemaLocation
attribute. The final piece of the schema reference indicates what namespace is used for the schema, and finally, the location of the schema itself is included in schemaLocation
.
To associate an XML file with an XML Schema Language document, or XSD file, we need to add some information to the root element of our XML document. Specifically, we'll include information for an XML processor indicating that this document follows a schema, and where the schema can be found. We’ll be associating jobs.xsd with the file job_postings.xml, and then validating the document against the XML Schema we created.
Checking the validity of an XML document using a schema
As mentioned in the introduction to the course, the process of checking to see if an XML document follows the rules laid out in a schema is called validation. This is an important step in the process of making an XML document, as it ensures the elements and attributes are being used as intended. While XML editors and some code editors make it possible to check the validity of XML right inside the editor, not all code editors offer that option. For our purposes today, we’ll be using the website XML Validation to validate job_postings.xml against the XML Schema we created.
Let’s open job_postings.xml and associate it with jobs.xsd, then check the validity of job_postings.xml using the XML Validation website.
NOTE: If you're working through the materials using oXygen XML Editor, follow the instructions on the page Validating an XML document using oXygen XML Editor to validate your XML.