How to Link XSD to XML

How to Link XSD to XML thumbnail
XML data is stored in a tree structure.

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.

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.

Related Searches:

References

Resources

  • Photo Credit Bare Tree image by Mich from Fotolia.com

Comments

You May Also Like

  • How to Generate an XSD From an XML File

    If you use XML data in your applications, it can be useful to have an XSD to check your structures against. XSD...

  • How to Convert XML to Xsd Online

    XML means eXtensible Markup Language whereas XSD means XML Schema definition. XML documents transfer data to and from web services. XSD defines...

  • How to Check XML With Xsd

    XML Schema Definitions, or XSDs, are used to specify the structure of XML data. An XSD defines the elements, attributes, data types...

  • How to Convert an XSD to an XML Tool

    The XSD schema created in your Visual Studio applications is the layout and "instructions" for the XML data. Visual Studio has a...

  • How to Create XSD VB

    There are two paths you can take to create XSD files from Visual Basic. If you have an existing XML document and...

  • How to Convert a XML File to XSD

    XML is the extension for the Extensible Markup Language file type. These are data files which use tags to define objects and...

  • XSD Validation Rules

    XSD Validation Rules. An XSD, or XML Schema Definition, is an XML document that describes some other XML data. The main purpose...

  • Xsd to XML Conversion Tools

    XSD files are Extensible Markup Language (XML) schemas used to help define XML documents. You can convert XSD to XML with XML...

  • How to Create an Xsd From XML

    An Extensible Markup Language (XML) file is a hierarchical data format that can contain any number of elements. Some programs use an...

Related Ads

Featured