Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare 2 xsd schema files for equivalent functionality

Tags:

xsd

I would like to compare 2 XSD schemas A and B to determine that all instance documents valid to schema A would also be valid to schema B. I hope to use this to prove that even though schema A and B are "different" they are effectively the same. Examples of differences this would not trigger would be Schema A uses types and Schema B declares all of it's elements inline.

I have found lots of people talking about "smart" diff type tools but these would claim the two files are different because they have different text but the resulting structure is the same. I found some references to XSOM but I'm not sure if that will help or not.

Any thoughts on how to proceed?

like image 351
Bob Stuart Avatar asked Nov 28 '11 01:11

Bob Stuart


People also ask

How do I compare two XSDS?

xsd to it. To compare, right click on Version 1.0. 0, invoke Compare with Version command and at the prompt, select Version 1.1. 0 to compare.

How do you reference XSD?

Reference the XSD schema in the XML document using XML schema instance attributes such as either xsi:schemaLocation or xsi:noNamespaceSchemaLocation. Add the XSD schema file to a schema cache and then connect that cache to the DOM document or SAX reader, prior to loading or parsing the XML document.

Is XSD and schema same?

XML Schema Definition or XSD is a recommendation by the World Wide Web Consortium (W3C) to describe and validate the structure and content of an XML document. It is primarily used to define the elements, attributes and data types the document can contain.

How do I add XSD to XSD?

If you are giving Absolute path, then you need to write <xs:include schemaLocation="file:D:/workspace/Test/res/header. xsd" /> Remember to add the 'file:' before adding the location.


2 Answers

Membrane SOA Model - Java API for WSDL and XML Schema

package sample.schema;

import java.util.List;
import com.predic8.schema.Schema;
import com.predic8.schema.SchemaParser;
import com.predic8.schema.diff.SchemaDiffGenerator;
import com.predic8.soamodel.Difference;

public class CompareSchema {

  public static void main(String[] args) {
    compare();
  }

  private static void compare(){
    SchemaParser parser = new SchemaParser();

    Schema schema1 = parser.parse("resources/diff/1/common.xsd");

    Schema schema2 = parser.parse("resources/diff/2/common.xsd");

    SchemaDiffGenerator diffGen = new SchemaDiffGenerator(schema1, schema2);
    List<Difference> lst = diffGen.compare();
    for (Difference diff : lst) {
      dumpDiff(diff, "");
    }
  }

  private static void dumpDiff(Difference diff, String level) {
    System.out.println(level + diff.getDescription());
    for (Difference localDiff : diff.getDiffs()){
      dumpDiff(localDiff, level + "  ");
    }
  }
}

After executing you get the output shown in listing 2. It is a List of differences between the two Schema documents.

ComplexType PersonType has changed:   Sequence has changed:
    Element id has changed:
      The type of element id has changed from xsd:string to tns:IdentifierType.

http://www.service-repository.com/ offers an online XML Schema Version Comparator tool that displays a report of the differences between two XSD that appears to be produced from the Membrane SOA Model.

like image 188
Mads Hansen Avatar answered Nov 09 '22 04:11

Mads Hansen


My approach to this was to canonicalize the representation of the XML Schema.

Unfortunately, I can also tell you that, unlike canonicalization of XML documents (used, as an example, to calculate a digital signature), it is not that simple or even standardized.

So basically, you have to transform both XML Schemas to a "canonical form" - whatever the tool you build or use thinks that form is, and then do the compare.

My approach was to create an XML Schema set (could be more than one file if you have more namespaces) for each root element I needed, since I found it easier to compare XSDs authored using the Russian Doll style, starting from the PSVI model.

I then used options such as auto matching substitution group members coupled with replacement of substitution groups with a choice; removal of "superfluous" XML Schema sequences, collapsing of single option choices or moving minOccurs/maxOccurs around for single item compositors, etc.

Depending on what your XSD-aware comparison tool's features are, or you settle to build, you might also have to rearrange particles under compositors such as xsd:choice or xsd:all; etc.

Anyway, what I learned after all of it was that it is extremely hard to build a tool that would work nice for all "cool" XSD features out there... One test case I remember fondly was to deal with various xsd:any content.

I do wonder though if things have changed since...

like image 32
Petru Gardea Avatar answered Nov 09 '22 03:11

Petru Gardea