【问题标题】:XSD to Java using JAXB with multi-level import failingXSD 到 Java 使用 JAXB 多级导入失败
【发布时间】:2017-01-20 04:16:34
【问题描述】:

我在使用 JAXB2 从 XSD 创建 Java 类时遇到问题。

如果 JAXB 可以处理,请告诉我。

场景:

1) 项目 A 具有具有不同名称空间的 a.xsdb.xsd b.xsd 使用带有导入名称空间标签的 a.xsd

2) 项目 B 具有 c.xsd 并通过导入 b.xsd 来使用 b.xsd
c.xsd 正在使用目录在作为依赖项添加的 Maven JAR 中查找 b.xsd

问题

Project A 构建良好,但 Project B 抛出错误,因为它找不到 b.xsd 内部使用的 a.xsd

错误

[错误] 解析架构时出错。位置 [http://www.example.com/test2/test2.xsd{15,39}]。 org.xml.sax.SAXParseException;系统ID:http://www.example.com/test2/test2.xsd;行号:15;列号:39; src-resolve:无法将名称“addme:address”解析为(n)“元素声明”组件。 在 com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203) 在 com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)

项目 A

a.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.com/address" xmlns="http://www.example.com/address"
    elementFormDefault="qualified">
    <xs:element name="address">

        <xs:complexType>
            <xs:sequence>
                <xs:element name="street" type="xs:string" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

b.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.example.com/test2"
    xmlns="http://www.example.com/test2"
    xmlns:addme="http://www.example.com/address"
    elementFormDefault="qualified">

    <xs:import namespace="http://www.example.com/address" schemaLocation="http://www.example.com/address/address.xsd"/>

    <xs:element name="test2">
       <xs:complexType>
            <xs:sequence>
                <xs:element ref="addme:address" />
            </xs:sequence>
        </xs:complexType>
     </xs:element>

</xs:schema>

项目 B

c.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.example.com/customer"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.example.com/customer"
    xmlns:test="http://www.example.com/test2" elementFormDefault="qualified">

    <xs:import namespace="http://www.example.com/test2"
        schemaLocation="http://www.example.com/test2/test2.xsd" />

    <xs:element name="customer">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="test:test2" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

项目 B 的目录文件

REWRITE_SYSTEM "http://www.example.com/test2" "maven:com.test.projectA:projectA:jar::!"

项目 A POM sn-p

<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <properties>
        <xsd.build.dir>${basedir}/src/main/resources</xsd.build.dir>
        <generated.source.location>${basedir}/target/generated-sources/src</generated.source.location>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics</artifactId>
            <version>0.6.4</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.13.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <generateDirectory>${generated.source.location}</generateDirectory>
                    <schemaDirectory>${xsd.build.dir}</schemaDirectory>
                    <episode>true</episode>
                    <addIfExistsToEpisodeSchemaBindings>true</addIfExistsToEpisodeSchemaBindings>
                    <plugins>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-basics</artifactId>
                            <version>0.6.4</version>
                        </plugin>
                    </plugins>
                    <args>
                        <arg>-XtoString</arg>
                        <arg>-Xequals</arg>
                        <arg>-XhashCode</arg>
                    </args>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>attach-sources</id>
                        <phase>DISABLE_FORKED_LIFECYCLE_MSOURCES-13</phase>
                    </execution>
                </executions>
            </plugin>

        </plugins>

    </build>
</project>

**PROJECT B POM** 

        <?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" .>

    <properties>
        <xsd.build.dir>${basedir}/src/main/resources</xsd.build.dir>
        <generated.source.location>${basedir}/target/generated-sources/src</generated.source.location>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.test.projectA</groupId>
            <artifactId>projectA</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics</artifactId>
            <version>0.6.4</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.13.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <generateDirectory>${generated.source.location}</generateDirectory>
                    <schemaDirectory>${xsd.build.dir}</schemaDirectory>
                    <catalog>src/main/resources/catalog.cat</catalog>
                    <useDependenciesAsEpisodes>true</useDependenciesAsEpisodes>
                    <plugins>
                        <plugin>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-basics</artifactId>
                            <version>0.6.4</version>
                        </plugin>
                    </plugins>
                    <args>
                        <arg>-XtoString</arg>
                        <arg>-Xequals</arg>
                        <arg>-XhashCode</arg>
                    </args>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>attach-sources</id>
                        <phase>DISABLE_FORKED_LIFECYCLE_MSOURCES-13</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>

    </build>
</project>

【问题讨论】:

  • 我看不到 http://www.example.com/address 的目录条目。它可能试图从 b.xsd 中指定的位置加载它,即http://www.example.com/address/address.xsd
  • 感谢@teppic。实际上我之前尝试过没有运气,我也是这么想的。基本上,当我构建具有 a 和 b xsd 的 projecA 时,构建良好,问题出现在具有 c xsd ->references b xsd -> 引用 a xsd 的项目中。 a 和 b 作为依赖项提供,可能在解析时无法从“c”级别获取“a”,但不确定是否有任何特定方法可以告诉系统或目录中......

标签: java maven xsd jaxb2


【解决方案1】:

您的目录文件需要重写两个 xsd 的位置:

REWRITE_SYSTEM "http://www.example.com/test2/test2.xsd" "maven:com.test.projectA:projectA:jar::!/b.xsd"
REWRITE_SYSTEM "http://www.example.com/address/address.xsd" "maven:com.test.projectA:projectA:jar::!/a.xsd"

请注意,这些是重写规则,而不是 PUBLIC 规则。每个条目的左侧是 xsds 中使用的systemLocation,而不是命名空间。 PUBLIC 规则对您不起作用,因为您的 xsd 指定了 systemLocation,并且 xjc 中存在一个错误,在指定 systemLocation 时阻止 PUBLIC 规则工作。

还要检查您的 xsd 是否被复制到已发布 jar 的根目录,并且它们的名称与目录中使用的名称匹配。

参考:Using-Catalogs

【讨论】:

  • 如果我使用你提到的完整路径,它也不会找到 b.xsd。 "example.com/test2/test2.xsd" "maven:com.test.projectA:projectA:jar::!/b.xsd" 它只是像我以前那样"example.com/test2" "maven:com.test.projectA:项目A:jar::!”我的意思是只有没有 xsd 的命名空间
  • 我已经检查过这适用于您的示例代码(已发布的 maven 项目和目录中的 maven url)。检查您的系统位置是否与目录中的 url 完全匹配,并且 xsd 已复制到已发布的 jar 到目录中指定的路径。
猜你喜欢
  • 1970-01-01
  • 2011-06-11
  • 2015-06-28
  • 2012-02-11
  • 1970-01-01
  • 2011-04-27
  • 2011-01-29
  • 1970-01-01
  • 2015-08-08
相关资源
最近更新 更多