3

如标题所示,如何导入未定义 targetNamespace 的模式?

我有以下案例:

文件:Data.xml

<?xml version="1.0"?>
<lib:library xmlns:lib="http://java2s.com/ns/library">
  <lib:book id="b0836217462">
    <lib:title>Being a Dog Is a Full-Time Job</lib:title>
    <lib:authors>
      <person id="CMS">
        <name>Charles M Schulz</name>
      </person>
    </lib:authors>
  </lib:book>
</lib:library>

文件:Schema.xsd

<?xml version="1.0"?>
<xs:schema targetNamespace="http://java2s.com/ns/library"
  elementFormDefault="qualified" attributeFormDefault="unqualified"
  xmlns:lib="http://java2s.com/ns/library"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import schemaLocation="another.xsd" />
  <xs:element name="library">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="book" type="lib:bookType" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="bookType">
    <xs:sequence>
      <xs:element name="title" type="xs:string" />
      <xs:element name="authors">
        <xs:complexType>
          <xs:sequence>
            <xs:element ref="person" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="id" type="xs:ID" use="required" />
  </xs:complexType>
</xs:schema>

文件:另一个.xsd

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="person" type="personType" />
  <xs:complexType name="personType">
    <xs:sequence>
      <xs:element name="name" type="xs:string" />
    </xs:sequence>
    <xs:attribute name="id" type="xs:ID" use="required" />
  </xs:complexType>
</xs:schema>

一切似乎都还好,但是当我尝试为这些 XSD 生成 Jaxb 类时,出现以下异常:

org.xml.sax.SAXParseException: undefined element declaration 'person'
    at com.sun.xml.xsom.impl.parser.ParserContext$1.reportError(ParserContext.java:176)
    at com.sun.xml.xsom.impl.parser.NGCCRuntimeEx.reportError(NGCCRuntimeEx.java:170)
    at com.sun.xml.xsom.impl.parser.DelayedRef.resolve(DelayedRef.java:125)
    at com.sun.xml.xsom.impl.parser.DelayedRef.run(DelayedRef.java:100)
    at com.sun.xml.xsom.impl.parser.ParserContext.getResult(ParserContext.java:131)
    at com.sun.xml.xsom.parser.XSOMParser.getResult(XSOMParser.java:226)
    at com.sun.tools.xjc.ModelLoader.createXSOM(ModelLoader.java:519)
    at com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl.bind(SchemaCompilerImpl.java:236)
    at com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl.bind(SchemaCompilerImpl.java:85)
    at org.apache.cxf.tools.wsdlto.databinding.jaxb.JAXBDataBinding.initialize(JAXBDataBinding.java:411)
    at org.apache.cxf.tools.wsdlto.WSDLToJavaContainer.generateTypes(WSDLToJavaContainer.java:603)
    at org.apache.cxf.tools.wsdlto.WSDLToJavaContainer.processWsdl(WSDLToJavaContainer.java:248)
    at org.apache.cxf.tools.wsdlto.WSDLToJavaContainer.execute(WSDLToJavaContainer.java:142)
    at org.apache.cxf.tools.wsdlto.WSDLToJavaContainer.execute(WSDLToJavaContainer.java:300)
    at org.apache.cxf.tools.common.toolspec.ToolRunner.runTool(ToolRunner.java:103)
    at org.apache.cxf.tools.wsdlto.WSDLToJava.run(WSDLToJava.java:113)
    at org.apache.cxf.tools.wsdlto.WSDLToJava.run(WSDLToJava.java:86)
    at org.apache.cxf.maven_plugin.wsdl2java.WSDL2JavaMojo.generate(WSDL2JavaMojo.java:380)
    at org.apache.cxf.maven_plugin.AbstractCodegenMoho.execute(AbstractCodegenMoho.java:257)
    at org.apache.cxf.maven_plugin.wsdl2java.WSDL2JavaMojo.execute(WSDL2JavaMojo.java:477)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)

添加targetNamespace到文件another.xsd并在文件中对其进行限定可以Schema.xsd解决问题,但是文件中的元素人员Delta.xml将位于该目标名称空间中,这不是我想要的。

我的问题基本上是:为什么 JAXB(或 XJC)没有名称空间时无法识别 person 元素?

对于 Jaxb 生成,我使用 cxf codegen 插件,它使用以下内容:版本 hudson-jaxb-ri-2.1-2 of JAXB XJC

4

1 回答 1

0

您遇到的问题与“导入 [ing an] xsd with no namespace”无关。所列的 XSD 非常好。问题很可能与您的设置有关,因为您的person对象的 XSD 未解决/找到并因此加载。要确保它不是您的 JAXB 版本(一种非常遥远的可能性),请从命令行调用 xjc/wsimport 并从那里获取它......

于 2013-01-08T12:35:42.380 回答