从XJC中导入的模式解析类型定义失败

G_H*_*G_H 15 jaxb xjc xmlcatalog maven jaxb-episode

我使用JAXB来使用这个API来方便地使用XJC(XML-to-Java)编译器通过命名引用从XML Schema生成的对象模型.它抽象了JAXB上下文的创建,并通过各种背景魔法和反射找到了ObjectFactory方法.它的基本要点是你总是定义一个通用模式,然后任何数字(也可能是0)模式"扩展"一般模式,每个模式产生自己的数据模型.通用模式带有可重用的定义,扩展它的定义使用它们来组成自己的模型.

我现在遇到了我想为多个项目重用通用模式的情况.一般类型定义应该在项目中保持相同,并且一些代码将针对从这些类生成的抽象类构建.所以我需要先为一些通用模式生成类,然后生成那些扩展并单独使用它们的类.我正在使用Maven进行构建过程.

我遇到的问题是从扩展模式中的通用模式解析类型定义.

假设我的通用模式名为"general.xsd",如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.foobar.com/general"
xmlns:gen="http://www.foobar.com/general"
elementFormDefault="qualified" attributeFormDefault="qualified">

    <!-- Element (will usually be root) -->
    <xs:element name="transmission" type="gen:Transmission" />

    <!-- Definition -->
    <xs:complexType name="Transmission" abstract="true">
        <xs:sequence>
            <!-- Generic parts of a transmission would be in here... -->
        </xs:sequence>
    </xs:complexType>

</xs:schema>
Run Code Online (Sandbox Code Playgroud)

接下来是一个绑定文件来做一些命名自定义并设置输出的包名称:

<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
    version="2.1">

    <!-- Bindings for the general schema -->
    <bindings schemaLocation="general.xsd" node="/xs:schema">

        <schemaBindings>
            <package name="com.foobar.models.general"/>
        </schemaBindings>

        <bindings node="//xs:complexType[@name='Transmission']">
            <!-- Some customization of property names here... -->
        </bindings>

</bindings>
Run Code Online (Sandbox Code Playgroud)

然后,我将在该项目的POM中使用下一个位来生成Java类:

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb21-plugin</artifactId>
    <version>0.8.0</version>
    <executions>
        <execution>
            <id>xjc-generate</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <schemaDirectory>${basedir}/src/main/resources/com/foobar/schemas</schemaDirectory>
                <schemaLanguage>XMLSCHEMA</schemaLanguage>
                <addCompileSourceRoot>true</addCompileSourceRoot>
                <episode>true</episode>
                <removeOldOutput>true</removeOldOutput>
            </configuration>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

如您所见,我正在使用JAXB2.1 Maven插件.我已经设置了为逐步编译生成一个剧集文件的选项.删除以前输出的选项是针对错误的解决方法; 它所做的就是确保首先清理所有内容,以便强制重新编译.

到现在为止还挺好.那个项目编译顺利.应该注意的是,除了生成的Java类之外,我还将模式打包到生成的jar文件中.所以这些都可以在classpath上找到!该sun-jaxb.episode文件应该是META-INF中的文件.

然后我开始使用模式,该模式将扩展上述内容,首先导入它.其中一个"子类型"看起来像这样(我称之为sub.xsd):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.foobar.com/sub"
xmlns:sub="http://www.foobar.com/sub"
xmlns:gen="http://www.foobar.com/general"
elementFormDefault="qualified" attributeFormDefault="qualified">

    <xs:import namespace="http://www.foobar.com/general" />

    <!-- Definition -->
    <xs:complexType name="SubTransmission">
        <xs:complexContent>
            <xs:extension base="gen:Transmission">
                <xs:sequence>
                    <!-- Additional elements placed here... -->
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

</xs:schema>
Run Code Online (Sandbox Code Playgroud)

再次,有一个绑定文件:

<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
    version="2.1">

    <!-- Bindings for sub type -->
    <bindings schemaLocation="sub.xsd" node="/xs:schema">

        <schemaBindings>
            <package name="com.foobar.models.sub"/>
        </schemaBindings>

    </bindings>

</bindings>
Run Code Online (Sandbox Code Playgroud)

这个项目的POM来自于XJC一代:

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb21-plugin</artifactId>
    <version>0.8.0</version>
    <executions>
        <execution>
            <id>xjc-generate</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <schemaDirectory>${basedir}/src/main/resources/com/foobar/schemas</schemaDirectory>
                <schemaLanguage>XMLSCHEMA</schemaLanguage>
                <addCompileSourceRoot>true</addCompileSourceRoot>
                <episode>false</episode>
                <catalog>${basedir}/src/main/resources/com/foobar/schemas/catalog.cat</catalog>
                <episodes>
                    <episode>
                        <groupId>com.foobar</groupId>
                        <artifactId>foobar-general-models</artifactId>
                        <version>1.0.0-SNAPSHOT</version>
                        <scope>compile</scope>
                    </episode>
                </episodes>
                <removeOldOutput>true</removeOldOutput>
            </configuration>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

最初,所有模式都在一个文件夹中,并且我schemaLocation在导入集中具有该属性general.xsd,该工作正常.但是现在事情在各个项目之间是分开的,我遇到了问题.第一个问题是无法找到其他架构.我已经通过schemaLocation<xs:import />元素中取出属性来解决这个问题,只保留namespace属性并添加一个目录文件(catalog.cat),您可以在上面的POM提取中看到它.其内容是:

PUBLIC "http://www.foobar.com/general" "classpath:/com/foobar/schemas/general.xsd"
Run Code Online (Sandbox Code Playgroud)

这似乎有效,因为我不再收到错误,指出无法找到架构.但由于某种原因,从导入的模式中解析实际的类型定义仍然失败.这是例外:

Error while parsing schema(s).Location [ file:/C:/NetBeans_groups/Test/SubModelBundle/src/main/resources/com/foobar/schemas/sub.xsd{...,...}].
org.xml.sax.SAXParseException: src-resolve: Cannot resolve the name 'gen:Transmission' to a(n) 'type definition' component.
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止所尝试的:

  • 使用目录文件.部分成功,因为现在可以找到导入的模式.
  • 让通用模式的编译生成一个剧集文件,并将其用于编译子模式.似乎没有什么区别,虽然这应该只在类型解决后发挥作用,所以我认为这不重要.
  • 使用不同的JAXP(注意:不是 JAXB,JAXP)实现.它确实使用了另一个,因为我可以在异常的堆栈跟踪中看到,但最终结果是相同的.
  • 使用maven-jaxb22-plugin而不是21.没有区别.

从网上看,似乎人们至少从2006年开始遇到这个问题,它可能与一些Xerces解析器问题有关.我希望这不是一个潜伏了6年的错误,没有任何人愿意修复它.别人有什么建议吗?也许有人遇到了同样的问题并找到了解决方案?我能想到的唯一解决方法是使用'svn:externals'将一般模式拖到子项目中,然后在那里重新生成类,但它很脏,只有在你可以连接到我们的svn repo时才能工作.

非常感谢您阅读这篇长篇文章.请记住,我已从现有项目中获取了上述所有内容,并替换了一些名称空间和其他内容以匿名,因此可能存在一些拼写错误.

G_H*_*G_H 5

此答案已编辑。之前,我有一个使用自定义目录解析器的解决方案。然而,我现在发现了真正的问题。解释如下。对于提供解决方案的 TL;DR 版本,请滚动到此答案的底部。


问题出在目录文件上。请注意它是如何有这一行的:

PUBLIC "http://www.foobar.com/general" "classpath:/com/foobar/schemas/general.xsd"
Run Code Online (Sandbox Code Playgroud)

那说什么?它表示如果http://www.foobar.com/general遇到公共 ID,则架构的系统 ID 为classpath:/com/foobar/schemas/general.xsd。到目前为止,一切都很好。如果我们从元素schemaLocation中取出属性<xs:import />,唯一剩下的就是公共 ID(命名空间 URN),并且目录文件告诉我们在哪里可以找到它的模式。

当该架构使用元素时就会出现问题<xs:include />。它们包括具有相同目标命名空间的模式文件。它们指定系统 ID(相对位置)。所以您希望将其用于解决方案。然而,记录对目录解析器的调用表明,请求是使用公共ID(命名空间)和系统ID(相对位置)进行解析的。这就是问题所在。由于目录文件中的绑定,公共 ID 被优先考虑。这让我们general.xsd再次直接看到文件。

举例来说,一般架构如下:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.foobar.com/general"
xmlns:gen="http://www.foobar.com/general"
elementFormDefault="qualified" attributeFormDefault="qualified">

    <!-- Including some definitions from another schema in the same location -->
    <xs:include schemaLocation="simple-types.xsd" />

    <!-- Remaining stuff... -->

</xs:schema>
Run Code Online (Sandbox Code Playgroud)

使用该模式的模式如下:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.foobar.com/sub"
xmlns:sub="http://www.foobar.com/sub"
xmlns:gen="http://www.foobar.com/general"
elementFormDefault="qualified" attributeFormDefault="qualified">

    <xs:import namespace="http://www.foobar.com/general" />

    <!-- Remaining stuff... -->

</xs:schema>
Run Code Online (Sandbox Code Playgroud)

当 XJC 解析最后一个模式时,会发生这种情况:

  1. 解析本地定义。
  2. 遇到对导入架构中的定义的引用。
  3. 检查导入,发现没有系统 ID,只有公共 ID ( http://www.foobar.com/general)。
  4. 检查目录。
  5. 查找公共 ID 与 的绑定classpath:/com/foobar/schemas/general.xsd
  6. 解析导入模式中的定义。
  7. 遇到对包含架构 (simple-types.xsd)中的定义的引用。
  8. 检查包括、查找系统 ID。
  9. 检查目录中的系统 ID,但公共 ID 是隐式的。
  10. 查找公共 ID 与 的绑定classpath:/com/foobar/schemas/general.xsd,该绑定优先于系统 ID。
  11. 解析包含的架构定义失败。

尝试解析的顺序的详细信息在 XML 目录的 OASIS 规范中进行了描述: https: //www.oasis-open.org/committees/entity/spec.html#s.ext.ent。这需要一些解释,但您会发现,如果首选解析方法是公共 ID,则即使存在系统 ID,在目录文件中绑定时这些方法也会优先。

那么,解决方案是指定系统 ID 是首选解析方法,而不是在导入中提供系统 ID,以便使用目录的公共 ID 绑定并依赖于包含中的相对系统 ID。在 OASIS XML 目录格式中,您可以使用 属性prefer="system"。在 OASIS TR9401 目录格式中,您可以使用OVERRIDE no. 显然默认是公开/是的。

所以我的目录文件就变成了:

OVERRIDE no
PUBLIC "http://www.foobar.com/general" "classpath:/com/foobar/schemas/general.xsd"
Run Code Online (Sandbox Code Playgroud)

现在常规目录解析器工作正常。我不再需要定制的了。但是,我没有想到在包含架构时公共 ID 仍用于解析,并且优先于系统 ID。我原以为公共 ID 仅用于导入,并且如果解析失败,仍会考虑系统 ID。仅向自定义解析器添加一些日志记录就揭示了这一点。


简短的回答:添加OVERRIDE no为 TR9401 目录文件中的第一个指令,或添加prefer="system"到 XML 目录文件的属性。不要schemaLocation<xs:import />指令中指定,而是将命名空间绑定到目录文件中正确的架构位置。确保<xs:include />使用所包含架构的相对路径。

另一个有趣的事情是:XJC 使用的目录解析器不仅可以处理classpath:URI,还maven:可以处理与 Maven 工件相关的 URI。如果您使用它作为构建工具,这非常有用。 http://confluence.highsource.org/display/MJIIP/User+Guide#UserGuide-Usingcatalogs