xsd - schemagen.exe 生成具有 <xs :complexType> 的不确定顺序的 xsd 架构

标签 xsd jaxb schemagen jaxb2-maven-plugin

我使用 org.codehaus.mojo jaxb2-maven-plugin 为我的类生成 xsd 模式。插件站点 http://mojo.codehaus.org/jaxb2-maven-plugin/faq.html告诉,该插件使用 JDK 实用程序 schemagen.exe 来生成。
问题是生成的 xsd 中的顺序未确定,取决于您运行插件的机器。

public class One {
    public String field1;
}

public class Two {
    public String field2;
}

并生成方案:
<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>

但是当我的同事运行 generation 时,他得到了另一个命令:
<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>

我们用
  • JDK 1.6.0_26
  • jaxb2-maven-plugin 1.3
  • jaxb-impl 版本 2.1.12(由插件使用)

  • 有没有办法控制这个顺序?

    最佳答案

    我遇到了同样的问题,最终在生成的 XSD 上应用了 XSLT 转换,使用以下 XSL 代码对内容进行排序:

    <?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>
    

    我使用 xml-maven-plugin 进行了 XSLT 转换:
    <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>
    

    关于xsd - schemagen.exe 生成具有 <xs :complexType> 的不确定顺序的 xsd 架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13136825/

    相关文章:

    java - 如何忽略 JAX-RS 2.0 客户端中的意外字段?

    java - 使用 jaxb-ri-2.2.7 的 ant schemagen 任务中的 ClassCastException

    xsd - 带有枚举的 JAXB SchemaGen 在不同的包中

    java - JAXB JXC 为枚举生成模式而不管 @XmlTransient

    xml - XML Schema 1.0 中是否有 <assert> 的替代方案

    java - JAXB 和无命名空间的 XML

    wcf - 在 WSDL 中更改 schemaLocation

    java - JAXB 中的编码/解码空值

    复杂类型的 XML 架构限制 : Complete Redefinition?

    xml - 在 DTD 中,为什么命名空间作为 URL 给出?