Comparing XML Schemas with DiffDog
DiffDog includes a powerful tool to compare XML Schemas that XML developers and others can use to update existing XML data files as XML Schemas evolve. This Tech Note takes a look at an example scenario for this feature.
Before we drop into this advanced functionality, let’s take a quick look at two XML Schemas using the DiffDog File Compare feature. Of course, just like in previous versions, DiffDog users can compare XML Schemas as .xsd documents and display differences in a color-coded, XML-aware format.
This is a good way to identify and manage differences in XML Schemas, especially when you want to review revisions to industry-standard XML Schemas that evolve over time.
What goes further in DiffDog is an additional XML Schema Differencing option that graphically displays two XML Schemas side by side, identifies identical elements automatically, and lets users map differences and generate XSL transformations to update XML data files.
Here’s our first view when we open the same two XML Schemas shown in the file comparison above, using the XML Schema Differencing feature.
The root elements of the two XML Schemas are automatically connected. We can click the Compare button in the toolbar to automatically connect identical elements in the two XML Schemas.
(Of course we could also select Compare XML Schemas from the right click context menu, or choose Start Comparison from the Diff and Merge menu, or press the F5 keyboard shortcut – DiffDog gives you many options to perform the same task, so you can work the way you like.)
Next, we can map elements with different names in the two XML Schemas by manually connecting the pointer arrows between them. In this example most of the changes to the version of the XML Schema on the right simply give elements new names that will be more clear when the XML Schema and its data files are distributed through our enterprise.
When all the elements are mapped, we can generate an XSLT file to transform existing XML data files based on the XML Schema on the left to reflect revisions in the newer version on the right. This feature is designed to rescue XML developers from the tedious tasks of writing and debugging XSL transformations by hand.
Here is an example of an original XML data file based on the XML Schema on the left side, as viewed in Altova XMLSpy® 2014:
The output file after applying the XSL transformation we created with DiffDog appears below. Note the substitution of the author element for writer, email for feedback, and so on.
If there are many existing XML files that need to be transformed, the Project Management features of XMLSpy can help us automate the process. We can add external folders to an XMLSpy project.
Using the XMLSpy properties dialog for each project folder, we can assign default values to assign an XML Schema for validation, the XSL transformation, and the destination of the output.
Now we can select the input folder in the XMLSpy Project helper window and transform all the files in it with the single-keystroke F10 shortcut.
When we originally mapped the XML Schema elements in DiffDog, we left the publication element on the left side unconnected, since it had no corresponding element in the earlier version of the schema. That means when we transform XML input files using the XSLT, the resulting output will not contain the publication element. If publication is a required element, we can call on MapForce® 2014 for a quick solution.
One of the options in DiffDog is to generate a MapForce mapping rather than XSLT. When we choose this option, MapForce launches with our DiffDog mapping already loaded as a new MapForce design, as shown below.
It’s easy to enhance the mapping by adding a constant as a default value for the publication element.
Now we can save an XSL file from MapForce that reuses all the element mappings we originally designed in DiffDog and adds the constant. When we apply the new XSL to transform our original XML data file, we get a result that includes the default value for the publication element.
This Tech Note started by describing the XML Schema Comparison feature in DiffDog. Fleshing out a simple – but typical – real-world example quickly highlighted additional tasks easily completed by taking advantage of tight integration with XMLSpy and MapForce.
All three of these tools and more are available at substantial savings in the Altova Altova® MissionKit® 2014, the integrated suite of XML, database, and UML tools designed to meet the diverse development and data management needs of today’s software architects and XML developers. Click here to download a free trial!