Validate an XML document
XMLSpear supports validation against a schema (xsd) or a document type definition (dtd). You turn on the validation by pressing the green validation button on the toolbar. If you have not choosen the type of validation(DTD or xsd) then you will be prompted to choose it. Every change that you make in the document will now immediately be checked against the schema. If the location of the schemas are not or incorrectly hinted in the xml document, you can manually give them. Please pay special attention to the method of using the file tree for assigning schemas.Resolving the location of schemas
When the xml is validated, the parser asks for the schema which is associated with the parsed element (or attribute). If the element belongs to a namespace, then the schema that is bound to that namespace is used. Sometimes the xml documents contains hints where to look for a schema. This is done by a schemaLocation attribute. The schemaLocation attribute contains a list of pairs that associate namespaces with schemas. For example:
xsi:schemaLocation="http://example.com/product product.xsd
http://example.com/order order.xsd"
By the way: you can also include a noNamespaceSchemaLocation attribute to define the schema which should be used for elements (or attributes) which do not belong to a namespace.
If the parser asks for a schema that is not "hinted" in the XML or if schema can not be loaded from the hinted location, then XMLSpear will come with a popup that asks you to locate the schema.Manually binding schemas
As mentioned above, schemas are normally resolved by looking to the schemaLocation or noNamespaceSchemaLocation attribute. But in many situations the xml is transferred from one to another place, and the schemalocation still points to a location that is not valid in the target environment.You could solve that by:
- changing the schemaLocation in the xml, so that it points to a valid schema file. Please keep in mind that if you have used a relative path, as in the example above, then the location is relative to the location of the xml file.
- placing the schema in the mentioned location
But XMLSpear gives you also another very convenient way, without changing the actual XML or moving schemas to other places.
You can add extra schema mappings that will be used in the validation.
- Locate the schema you want to use in the file tree. The file tree can is the left-most window of XMLSpear.
- Now right-click the schema and choose "Assign schema".
By the way:
Instead of using the file tree and "assign schema", you can also use the plus sign in the SchemaMappings window. However, using the file tree is much more convenient. You can organize your schemas in folders to further optimize this way of binding schemas. Most of the time you will first assign the schemas and then start the validation by pushing button. In fact this method of mapping schemas is very similar to using XML catalogs, but it is more dynamic and specific to the particular XML instance that you are validating.
Instead of using the file tree and "assign schema", you can also use the plus sign in the SchemaMappings window. However, using the file tree is much more convenient. You can organize your schemas in folders to further optimize this way of binding schemas. Most of the time you will first assign the schemas and then start the validation by pushing button. In fact this method of mapping schemas is very similar to using XML catalogs, but it is more dynamic and specific to the particular XML instance that you are validating.
Refreshing a changed schema
All schemas that are used in the validation are cached im memory. Sometimes you may want to change a schema and see the effect in the validation. However, this will have no effect because of the active caching. There are several way to force the use of the changed schema.- If you have not manullay mapped schemas, then the easiest way is to just throw away all mappings. This will also empty the cache. All the schemas will autmatically be remapped because they are explicitly mentioned in the XML document.
- - Click the SchemaMappings tab which is located south of the XML panel.
- - Click on the trash icon (located at the far right in the toolbar) to remove all mappings.
Note:
If the validation is active, then the source will immedatialy be revalidated causing all schemas to be remapped and reloaded. - If you have done a lot of manually schemamappins, then you might want to refresh only the particular schema you changed. We use almost the same method as above but instead of pressing the trash icon:
- - Select the row with the particular schema and then click the edit button on the toolbar.
- - A popup window appears. In fact we do not want to edit anything but are only using this function to force a refresh of the schema. So just click OK to close the window.
- Yoy can can remap the particular schema using the file tree
- - Locate the schema you want to use in the file tree. The file tree can is the left-most window of XMLSpear.
- - Now right-click the schema and choose "Assign schema".
- - If the schema was already cached, you will get a popup telling you that. Just click OK.