How to Link XSD to XML
XML Schemas are a way of defining the structure that one or more XML documents should conform to. Just as with HTML pages conforming to the rules laid out in one of the many Document Type Definitions (DTDs), XSDs lay out definitions that XML content can observe. XSDs are often used to validate an XML document, or section of markup, in the same way that the W3C Validators for HTML and CSS are used. To this end, an XML document can include a link to the XSD laying out the structure that it has been built according to.
Instructions
-
-
1
Work out the purpose of your XML content. To create your Schema, you will need to consider the data that your XML is intended to model, as well as what that data is going to be used for. With this in mind, work out an abstract definition of the items within your data in the same way that you might do for a database. Taking the time to do this before actually building your XSD will almost certainly save you time and stress later on.
-
2
Define your XSD. Translate your data modeling into an XML Schema Definition (XSD). This should involve defining elements and optionally attributes for these. Make sure that all of the data you intend to model using the XML is accommodated by the XSD rules. The XML Schema should structure the elements in "tree" form, which means that some of the elements will be contained within others. That is, some elements are "children" of other elements. Within your XSD you can also dictate whether the elements occur in a particular order, how many child elements they can have, whether they can be empty and data types, as well as default values for both elements and attributes.
-
-
3
Create some XML according to the Schema. If you do not already have one, build an XML document that conforms to your XSD, even if this is just a test document to begin with. If you find that parts of your XML are not accommodated by the Schema definition, reconsider the structure of your XSD, bearing in mind whatever you ultimately intend using the XML for. Different technologies handle XML differently, and you may find some structures to be problematic when you come to use the XML within other programs.
-
4
Link to the Schema within your XML. In your XML document, add attributes similar to the following in the root element:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Referencing the same namespace as in your XSD "schema" element's "targetNamespace" attribute:
xmlns="http://www.yourdomainhere.com"
Again referencing the namespace, but paired with a hint of the location for the Schema document:
xsi:schemaLocation="http://www.yourdomainhere.com http://www.yourdomainhere.com/yourschema.xsd"
You should change the domain and Schema location to suit your own.
-
5
Validate your XML according to the Schema. Use one of the online Validators to check that your XML markup conforms to the XSD you created. If it does not, and you feel the XML is structured the way you want, consider making changes to the XSD document. When you validate your XML, make sure you are using a validation tool that actually tests your markup against a Schema, as many do not, merely checking that the document contains well-formed XML.
-
1
Tips & Warnings
The syntax within XML Schema Definitions can seem difficult at first. Use a text editor that highlights the code so that you can read it a little more easily.
Remember that an XSD document should also be written in XML. Make sure you validate your XML Schema Definition before you use it to validate your XML content.
References
Resources
- Photo Credit Bare Tree image by Mich from Fotolia.com