我在使用JAXB2從XSD創建Java類時遇到了一個問題。使用帶有多級導入失敗JAXB的XSD到Java
請讓我知道,如果JAXB可以處理這個。
場景:
1)項目A有a.xsd
和b.xsd
用不同的名稱空間 b.xsd
使用a.xsd
與導入名稱空間標籤。
2)項目B具有c.xsd
並通過導入b.xsd
使用b.xsd
。
c.xsd
正在使用目錄在Maven JAR中查找作爲依賴項添加的b.xsd
。
ISSUE:
項目A建立罰款,但項目B拋出錯誤,因爲它找不到a.xsd
在內部使用b.xsd。
ERROR
[ERROR]錯誤在解析模式(一個或多個).Location [http://www.example.com/test2/test2.xsd {15,39}]。 org.xml.sax.SAXParseException; systemId:http://www.example.com/test2/test2.xsd; lineNumber:15; columnNumber:39; 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)
項目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片斷
<?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
謝謝@teppic。其實我之前沒有碰到過,我也這麼想過。基本上當我建立projecA有一個和B xsd,建立良好,問題出現在項目中有c xsd - >引用b xsd - >引用xsd。 a和b是作爲依賴關係提供的,可能是它在解析時無法從「c」級獲得「a」,但不確定是否有任何特定的方法來告知系統或目錄中的數據... – Dave