简体   繁体   English

使用JAXB的XSD到Java的多级导入失败

[英]XSD to Java using JAXB with multi-level import failing

I am facing an issue while creating Java classes from XSD using JAXB2. 使用JAXB2从XSD创建Java类时遇到问题。

please let me know if JAXB can handle this. 请让我知道JAXB是否可以处理此问题。

Scenario: 场景:

1) Project A has a.xsd and b.xsd with different name-spaces b.xsd uses a.xsd with import name-space tag. 1) 项目A具有不同名称空间的a.xsdb.xsd b.xsd使用具有导入名称空间标签的a.xsd

2) Project B has c.xsd and uses b.xsd by importing b.xsd . 2) 项目Bc.xsd并使用b.xsd通过导入b.xsd
Catalog is being used by c.xsd to find the b.xsd in the Maven JAR which is added as a dependency. c.xsd使用目录在Maven JAR中查找b.xsd ,并将其作为依赖项添加。

ISSUE : 问题

Project A builds fine but Project B throws error as it could not find a.xsd which is internally used by b.xsd. 项目A会很好,但项目B抛出错误,因为它找不到a.xsd在内部使用b.xsd。

ERROR 错误

[ERROR] Error while parsing schema(s).Location [ http://www.example.com/test2/test2.xsd {15,39}]. [错误]解析模式时出错。位置[ http://www.example.com/test2/test2.xsd {15,39}]。 org.xml.sax.SAXParseException; org.xml.sax.SAXParseException; systemId: http://www.example.com/test2/test2.xsd ; systemId: http : //www.example.com/test2/test2.xsd ; lineNumber: 15; lineNumber:15; columnNumber: 39; columnNumber:39; src-resolve: Cannot resolve the name 'addme:address' to a(n) 'element declaration' component. src-resolve:无法将名称“ addme:address”解析为一个(n)“元素声明”组件。 at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203) at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134) 在com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)处com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)

Project A 项目A

a.xsd 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>

Project B 项目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>

catalog file for project B 项目B的目录文件

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

Project A POM snippet 投影一个POM片段

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

Your catalog file needs to rewrite the locations of both xsds: 您的目录文件需要重写两个xsds的位置:

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"

Note that these are rewrite rules, not PUBLIC rules. 请注意,这些是重写规则,而不是PUBLIC规则。 The left hand side of each entry is the systemLocation used in the xsds, not the namespace. 每个条目的左侧是systemLocation使用的systemLocation,而不是名称空间。 PUBLIC rules won't work for you because your xsds specify a systemLocation , and there's a bug in xjc that prevents PUBLIC rules from working when a systemLocation is specified. PUBLIC规则将不会为你工作,因为你的XSD指定systemLocation ,并有在XJC防止错误PUBLIC规则从当工作systemLocation指定的。

Also check that your xsds are being copied to the root of the published jar, and that their names match the ones used in the catalog. 还要检查您的xsds是否已复制到已发布jar的根目录中,并且其名称与目录中使用的名称匹配。

Reference: Using-Catalogs 参考: 使用目录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM