Search code examples
liquid-xml

Can I automatically associate an XSD schema with an XML document


I'm editing a lot of XML files using Liquid XML, I can associate a schema with the document by adding a schemaLocation attribute, but this is not practical as I have lots of files and I can't leave the attribute in as the upstream processor doesn't like it!

Is their a way I can associate a schema with a group of files?


Solution

  • There are a few ways to associate XSD schemas with an XML document.

    Schema Location Attributes

    If the XML Document contains a schema location attributes (xsi:noNamespaceSchemaLocation or xsi:schemaLocation attributes), then the schemas pointed to are loaded and used for validation and intellisense.

    Namespace

    The namespaces declared in the XML file are used to lookup schemas from the Schema Library. If a namespace in your XML file matches an entry in the schema library then all the schemas for that entry are loaded. You can also add your own entries to the Schema Library (more about this below).

    Manually associating schemas

    You can manually associate schemas with your XML File. Use the "Xml Document to Schema Mappings" form to select existing entires in the Schema Library, or "Add Schema file" to select a single XSD file to associate with the XML document. These settings are then remembered then next time the XML file is opened. This is particularly useful if your XML file does not contain a target namespace, as automatic matching is therefore not possible.


    Manually associating a schema with an XML File

    You can control the schemas that are associated with your XML File using the "Xml Document to Schema Mappings" form.

    enter image description here The form show all the schemas that have been added to the Schema Library. At the top of the list are the schemas that have been found in your XML document (ie via xsi:noNamespaceSchemaLocation or xsi:schemaLocation attributes).

    Checked - Explicitly included schema. The schema is used regardless of whether the namespaces appear in the XML document. This can be useful when associating schemas with XML documents that do not contain any namespace information.

    Unchecked - Explicitly exclude schema. The schema will not be used regardless of whether its namespaces match the XML Document. This is useful when you have several versions of the same schema, and they all have the same namespace, or where the schema is so large intellisense slows down the XML Editor to an unacceptable degree.

    Indeterminate - Automatic schema selection. If the namespace in the schema entry match the ones in your XML file then this entry will be used. This is the default state for all entries.


    Adding your own Schema Library Entry

    If you have a set of schemas that are not in the standards library you can add your own entry.

    enter image description here

    Note you can add several schema files that may not necessarily be associated with each other, say for example FIXML contains annotations that in your documents contain XHTML, you could also add the XHTML schema. If you did this it would be a good idea to de-select the XHTML namespaces from the trigger namespaces listed (so this standard is not used when an XHTML document is opened, only when a FIXML document is opened).


    And Finally the Answer

    If your XML documents are all in a given namespace your best bet is to create a Schema Library Entry. This will be automatically associated with any XML documents that contain that namespace, so you will get validation and intellisense automatically.