5/5/11

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 documents define the structures that XML content can contain, including elements, child elements and data types. One of the most common uses for an XSD is to check XML data using a Validator, making sure that all of your XML conforms to the set structure. You can generate XSDs from a sample XML document using a number of programs, and the process is typically fast and easy.
    • 1

      Prepare your XML document. Make sure your structures are correctly formed, using an Internet Validator to check for syntax errors. Your XML file is going to form the basis for the XSD, and the XSD will typically be used to determine whether future XML is properly structured, so it's essential that you make sure the original XML is built the way you want it.

    • 2

      Find a software program to download or a Web service to use for generating an XSD from your XML file. There are many such programs available, some free and some commercial. Stylus Studio is a commercial program but you can download a free trial of it. Oxygen XML Editor is a free option that has many advanced functions for dealing with XML, including XSD generation. There are also online services that can generate XSD from XML, such as the Flame-Ware tool.

    • 3

      Pass your XML content to your chosen XSD generation tool. If you're using a desktop program, open the XML file in it and choose from the menu options to generate your XSD. If you're using an Internet service, enter the XML or the file path to it on your computer, upload it and follow the instructions to generate your XSD. Save the XSD document on your computer.

    • 4

      Check the structure of your XSD document. If you plan on using the XSD to validate future XML content for a project, this is the time to make sure the XSD has the structure you need it to. You cannot assume that a generated XSD will be correct, as the XSD is only a reflection of the original XML, so check that it outlines the elements, attributes, tree structure and data types you require.

    • 5

      Test your XSD by validating some XML data using it. Use one of the on-line Validators that takes a reference to an XSD and checks XML content against it. Include a reference to the XSD in your test XML using the following syntax, added to the root element:

      <root_element xmlns:xsi="w3.org/2001/XMLSchema-instance"

      xmlns="yourdomainhere.com"

      xsi:schemaLocation="yourdomainhere.com your_schema_document.xsd">

      Most XSD validation tools do not actually visit the XSD location specified in the link, but allow you to upload it along with your XML.

  • No comments: