5

I used org.codehaus.mojo jaxb2-maven-plugin to generate xsd schema for my classes. Plugin site http://mojo.codehaus.org/jaxb2-maven-plugin/faq.html tell, that plugin uses JDK utility schemagen.exe to do generation. Problem is that order in generated xsd is not determined, and depends on machine you run plugin.

public class One {
    public String field1;
}

public class Two {
    public String field2;
}

And generated scheme:

<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:complexType name="two">
    <xs:sequence>
      <xs:element name="field2" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>  

  <xs:complexType name="one">
    <xs:sequence>
      <xs:element name="field1" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

</xs:schema>

But when my co-worker runs generation, he get another order:

<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:complexType name="one">
    <xs:sequence>
      <xs:element name="field1" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>


  <xs:complexType name="two">
    <xs:sequence>
      <xs:element name="field2" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType> 

</xs:schema>

We use

  • JDK 1.6.0_26
  • jaxb2-maven-plugin 1.3
  • jaxb-impl version 2.1.12 (used by plugin)

Is there a way to control this order?

Dmitry
  • 61
  • 1

1 Answers1

0

I had the same issue, and ended up applying an XSLT transform on the generated XSDs, that sorts the content, with the following XSL code :

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:saxon="http://saxon.sf.net/">

<!-- Indent the result (this is not strictly necessary, and forces to use Saxon) -->
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" saxon:indent-spaces="4"/>
<xsl:template match="xs:schema">
    <!-- Copy the schema node with its attributes, but without its children -->
    <xsl:copy>
        <xsl:copy-of select="@*"/>

        <!-- Copy the namespace nodes (sorted) -->
        <xsl:for-each select="./xs:import">
            <xsl:sort select="@namespace"/>
            <xsl:apply-templates select="."/>
        </xsl:for-each>

        <!-- Copy the element nodes (sorted) -->
        <xsl:for-each select="./xs:element">
            <xsl:sort select="@name"/>
            <xsl:apply-templates select="."/>
        </xsl:for-each>

        <!-- Copy the simpleType nodes (sorted) -->
        <xsl:for-each select="./xs:simpleType">
            <xsl:sort select="@name"/>
            <xsl:apply-templates select="."/>
        </xsl:for-each>

        <!-- Copy the complexType nodes (sorted) -->
        <xsl:for-each select="./xs:complexType">
            <xsl:sort select="@name"/>
            <xsl:apply-templates select="."/>
        </xsl:for-each>

        <!-- Copy all the other nodes (if there are) -->
        <xsl:for-each select="./*[not(self::xs:import or self::xs:element or self::xs:simpleType or self::xs:complexType)]">
            <xsl:copy-of select="."/>
        </xsl:for-each>
    </xsl:copy>   
</xsl:template>

<xsl:template match="xs:import">
    <xsl:copy-of select="."/>
</xsl:template>

<xsl:template match="xs:simpleType">
    <xsl:copy-of select="."/>
</xsl:template>

<xsl:template match="xs:complexType">
    <xsl:copy-of select="."/>
</xsl:template>

<xsl:template match="xs:element">
    <xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>

I did the XSLT transform with the xml-maven-plugin :

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>xml-maven-plugin</artifactId>
    <version>1.0.2</version>
    <executions>
        <execution>
            <goals>
                <goal>transform</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <transformationSets>
            <transformationSet>
                <dir>path/to/xsd</dir>
                <stylesheet>path/to/sortXSD.xsl</stylesheet>
                <outputDir>path/to/xsd</outputDir>
            </transformationSet>
        </transformationSets>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>net.sf.saxon</groupId>
            <artifactId>saxon</artifactId>
            <version>8.7</version>
        </dependency>
    </dependencies>
</plugin>
Mossroy
  • 565
  • 4
  • 11